0

我有一个列表框,在一个单独的类中我试图访问列表框的选定值,但它说它不能被访问,因为它不是公共的。我在访问标签时也遇到了同样的问题。

 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();
        }
    }
4

1 回答 1

2

选择表单上的 ListBox 并将 Modifier 属性从 Private 更改为 Public。

这是因为表单设计器默认将控件创建为私有。您可以查看 Designer 生成的代码并亲自查看。

示例Form1.Designer.cs代码...

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

在 Designer 中将 Modifier 属性更改为 public 后...

partial class Form1
{
...
    public System.Windows.Forms.ListBox listBox1;
}
于 2013-03-11T23:13:15.663 回答