我有一个列表框,在一个单独的类中,我正在尝试访问列表框的选定值,但它说它无法访问,因为它不公开。访问标签时我也遇到同样的问题。
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();
}
}
答案 0 :(得分: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;
}