简体   繁体   English

将通用列表项添加到Winforms的列表框C#

[英]Adding generic list items to Winforms' listbox c#

Hi Im trying to add the items to a generic list. 您好,我试图将这些项目添加到通用列表中。 Then on a button click the generic list items should be transferred to my listbox1. 然后在按钮上单击通用列表项,应将其转移到我的listbox1。 This works however, it keeps adding them every time I click the button. 但是,此方法有效,每次单击按钮时都会不断添加它们。

I tried using an if command, sometime like this 我尝试使用if命令,像这样的时候

 if (listBox1.Items.Count == 0)
 {
     // this contained everything I am now adding under initialize component)
 }

But this generated the error. 但这产生了错误。 index value can not be 0. So in essence this works as is. 索引值不能为0。因此从本质上讲,它按原样工作。 But I do not want keep adding the items every time I click. 但是我不想每次单击都继续添加项目。

public Form1()
{
    InitializeComponent();
    list.Add("Januari");
    list.Add("Februari");
    list.Add("March");
    list.Add("April");
    list.Add("May");
    list.Add("June");
    list.Add("July");
    list.Add("August");
    list.Add("September");
    list.Add("Oktober");
    list.Add("November");
    list.Add("December");
}

private IList<string> list = new List<string>();

private void button1_Click(object sender, EventArgs e)
{
    for (int index = 0; index < list.Count; index++)
    {
        listBox1.Items.Add(list[index]);
    }

    int mnr = Convert.ToInt32(textBox1.Text);
    string mnm = Convert.ToString(listBox1.Items[mnr - 1]);
    textBox2.Text = mnm;      
}

This is just an alternative to @Kami's answer, if you don't use the IList<string> list anywhere else, then just add the items directly to the listbox: 这只是@Kami答案的替代方法,如果您不在其他任何地方使用IList<string> list ,则只需将项目直接添加到列表框中即可:

public Form1()
{
    InitializeComponent();

    // This is only called once.
    InitializeListBox();
}

private void InitializeListBox()
{
    listBox1.Items.Add("Januari");
    listBox1.Items.Add("Februari");
    listBox1.Items.Add("March");
    listBox1.Items.Add("April");
    listBox1.Items.Add("May");
    listBox1.Items.Add("June");
    listBox1.Items.Add("July");
    listBox1.Items.Add("August");
    listBox1.Items.Add("September");
    listBox1.Items.Add("Oktober");
    listBox1.Items.Add("November");
    listBox1.Items.Add("December");
}

private void button1_Click(object sender, EventArgs e)
{
    int mnr = Convert.ToInt32(textBox1.Text);
    string mnm = Convert.ToString(listBox1.Items[mnr - 1]);
    textBox2.Text = mnm;
}

There are two things you can do here. 您可以在这里做两件事。

Firstly option is to add a call to clear() before you load the list. 首先,选择是在加载列表之前添加对clear()的调用。 This will remove the existing items and reinsert them. 这将删除现有项目并重新插入它们。

    private void button1_Click(object sender, EventArgs e)
    {
        listBox1.Items.Clear();

        for (int index = 0; index < list.Count; index++)
        {

            listBox1.Items.Add(list[index]);
        }

        int mnr = Convert.ToInt32(textBox1.Text);
        string mnm = Convert.ToString(listBox1.Items[mnr - 1]);
        textBox2.Text = mnm;
    }

Or alternatively, and probably the better solution is to move the loading into an init function. 或者,也许更好的解决方案是将加载移至init函数中。 As the data only needs to be loaded once per form, this is the better solution. 由于每个表格只需要加载一次数据,因此这是更好的解决方案。

    public Form1()
    {
        InitializeComponent();
        Init();
    }

    private void Init()
    {
        list.Add("Januari");
        list.Add("Februari");
        list.Add("March");
        list.Add("April");
        list.Add("May");
        list.Add("June");
        list.Add("July");
        list.Add("August");
        list.Add("September");
        list.Add("Oktober");
        list.Add("November");
        list.Add("December");

        listBox1.Items.Clear();

        for (int index = 0; index < list.Count; index++)
        {

            listBox1.Items.Add(list[index]);
        }
    }

    private IList<string> list = new List<string>();

    private void button1_Click(object sender, EventArgs e)
    {
        int mnr = Convert.ToInt32(textBox1.Text);
        string mnm = Convert.ToString(listBox1.Items[mnr - 1]);
        textBox2.Text = mnm;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM