[英]How to add list into list box on a different form from using a button
Im trying to add to a listbox in a different form by using a button and a for loop, however it says "Clothes" does not contain a definition for the list box, i have the list box set to public since that's what i found online but has not fixed the problem. 我试图通过使用按钮和for循环以不同的形式添加到列表框,但是它说“衣服”不包含列表框的定义,我将列表框设置为public,因为这是我在网上找到的但尚未解决问题。 Its like its not recognised even when i type Clothes dot and is not there on the drop down menu. 就像我无法识别它一样,即使我输入Clothes dot并且在下拉菜单上也没有。 I have also tried F.CustomersList
but this also does not work. 我也尝试过F.CustomersList
但这也不起作用。
private void LoadCity_Click(object sender, EventArgs e)
{
Form F = new Clothes();
if (F.ShowDialog() == DialogResult.OK)
{
for (int i = 0; i < _Mexico.Count; i++)
{
Clothes.CustomersList.Items.Add(_Mexico.ElementAt(i));
}
}
}
for this you have to create a List in the Click_Event and set it to the CustomersList Property of the other Form. 为此,您必须在Click_Event中创建一个List,并将其设置为另一个Form的CustomersList属性。
private Form custForm;
private void LoadCity_Click(object sender, EventArgs e)
{
if(custForm == null)
{
custform = new Clothes(); //Clothes should be a Form
}
List cust = new List();
for(int i = 0; i < _Mexico.Count; i++)
{
cust.add(_Mexico.ElementAt(i);
}
custform.CustomersList = cust;
}
The Property should look like this: 该属性应如下所示:
private List _CustomersList;
public List CustomersList
{
get
{
return _CustomersList;
}
set
{
_CustomersList = value;
//then do your stuff here
}
}
Apart from making no sense to put things in the list after you have closed the form with the list you have to change the initialization from your Form from 用列表关闭表单后,除了将所有内容放入列表中毫无意义外,还必须将Form的初始化更改为
Form F = new Clothes()
to 至
Clothes F = new Clothes()
because System.Windows.Forms.Form does not contain your public list. 因为System.Windows.Forms.Form不包含您的公共列表。 Then you have access to your Listbox through 然后,您可以通过以下方式访问列表框
F.CustomersList.Items.Add(_Mexico.ElementAt(i))
Just seen that Rufus already mentioned that in his comment, maybe you have to overthink your design. 刚刚看到Rufus已经在他的评论中提到了这一点,也许您不得不考虑一下您的设计。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.