Reputation: 839
I'm trying to add some text to a listBox in a form, from another form, but I get red underlines in the code that should add to the listBox. Isn't it possible to pass data to a form in a class from another class that I'm trying to do? I'm using VisualStudio 2013
I have this code I'm using in the MainForm class:
// Local object of SearchResultForm
SearchResultForm frmSearchResult = new SearchResultForm();
frmSearchResult.Show();
frmSearchResult.listBox.IsEnabled == false;
Error message: inaccessible due to its protected level
Upvotes: 0
Views: 1749
Reputation:
Add a Parameter To your class SearchResultForm class here my class is showvideolbx
public class showvideolbx
{
public ListBox SVListBox { get; set; }
}
Assign ListBox Id to Class Parameter
showvideolbx lbbox = new showvideolbx();
lbbox.SVListBox = lbxSongsList;
Then Now You call the Create class and then ListBox Parameter as
showvideolbx svlbox;
if (svlbox.SVListBox.IsEnabled == false)
svlbox.SVListBox.IsEnabled = true;
It will Work please try once.
Upvotes: 0
Reputation: 33428
Looks like your listBox
is a private
member of SearchResultForm
; that means it's not accessible from outside the SearchResultForm
class. You can make make the listBox
variable public
or internal
, but a better way do to it is to create a mutator method in the SearchResultForm
class something like:
public void AddItemToListBox(string text)
{
listBox.Items.Add(text);
}
and then from your main form you will do:
// Local object of SearchResultForm
SearchResultForm frmSearchResult = new SearchResultForm();
frmSearchResult.Show();
frmSearchResult.AddItemToListBox("test");
This way you can update the listBox without exposing your SearchResultForm
class implementation details.
Upvotes: 3