简体   繁体   English

如何通过使用按钮将列表添加到其他表单上的列表框中

[英]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.

相关问题 如何从其他表单的列表中添加/删除项目? - How to add/remove items from a list from a different form? 如何从另一种形式的文本框中编辑列表框的内容 - How to edit the contents of a list box, from a text box in another form 绑定ItemsSource时如何向wpf列表框中添加按钮 - How to add button to wpf list box when ItemsSource is binded C# Windows 窗体使用列表和类向列表框添加、保存、显示和更新文本 - C# Windows Form add, save, display and update text to list box using list and class 如何使用C#将列值从SQL Server传输到另一种形式的已选中列表框? - How to transfer a column value from SQL Server to checked list box of another form using c#? 如何使用单选按钮REPOST排列列表框中的项目 - How to arrange items in a list box by using radio button REPOST 如何从窗口应用程序中的另一个列表框中添加列表框中的值 - How to Add value in the list box from another list box in window Application 如何通过Windows窗体中的按钮/文本框添加到C#中的列表 - How to add to a list in C# via button/textbox in a windows form 如何将索引值添加到来自另一种不同形式的列表视图中? - How to add an indexed value into a list view coming from another different form? 如何从下拉列表中将项目添加到列表框中 - How to add item(s) to a list box from a drop down
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM