user2157179
user2157179

Reputation: 236

Access ListBox in class

I have a listbox and in a separate class I'm trying to access the selected value of the listbox but it says it cannot be accessed as it is not public. I am also having the same problem with accessing a label.

 public dataCollector(string i)
    {
        string tag = i;
    }
    public string dataCollector()
    {
        Form1 f = new Form1();
        string workingDirectory = Directory.GetCurrentDirectory();
        var xmlFile = XDocument.Load(workingDirectory + @"\modules.xml");

        var name = from d in xmlFile.Descendants("Name")
                   where d.Value == (String)f.selectionBox.SelectedItem
                   select d.Parent.Element(tag).Value;

        foreach (var item in name)
        {
            f.moduleName.Text = item.ToString();
        }
    }

Upvotes: 0

Views: 1593

Answers (1)

blins
blins

Reputation: 2535

Select the ListBox on your form and change the Modifier property from Private to Public.

This is happening because the Form Designer will create the Controls as Private by default. You can peek at the code generated by the Designer and see for yourself.

Example Form1.Designer.cs code...

partial class Form1
{
...
    private System.Windows.Forms.ListBox listBox1;
}

And after changing the Modifier property to public in the Designer...

partial class Form1
{
...
    public System.Windows.Forms.ListBox listBox1;
}

Upvotes: 2

Related Questions