不要让我在列表框中添加比组合框中显示的更多的项目

时间:2016-05-27 09:23:09

标签: c#

enter image description here 我有一个组合框与物品 1 2 3 ... 40 ,如果我选择了值4,那么我应该能够在我的列表框中添加不超过4个值。这就是我想的但是没有工作。

 public Form1()
    {
        InitializeComponent();
    }

    private void add_Click(object sender, EventArgs e)
    {
        int allowedItemsCount = 0;
        Int32.TryParse(comboBox1.SelectedText, out allowedItemsCount);
        int currentItemsCount = listBox1.Items.Count;

        if (currentItemsCount < allowedItemsCount)
        {
            listBox1.Items.Add(textBox1.Text); 
        }
    }

    private void delete_Click(object sender, EventArgs e)
    {


        if (listBox1.SelectedItems.Count != 0)
        {
            while (listBox1.SelectedIndex != -1)
            {
                listBox1.Items.RemoveAt(listBox1.SelectedIndex);
            }
        }

    }

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        int x = 0;
        Int32.TryParse(comboBox1.SelectedText, out x);
        int count = listBox1.Items.Count;
        if (count > x)
        {
            listBox1.Items.Clear();
            int difference = count - x;
            for (int i = 0; i < difference; i++)
            {
                listBox1.Items.RemoveAt(listBox1.Items.Count - 1);
            }
        }
    }
}

以下是您要求的完整代码但无效...现在添加按钮无效。

3 个答案:

答案 0 :(得分:0)

删除额外物品:

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    listBox1.TrimExcess(Int32.Parse(comboBox1.SelectedText));
    //If you control the items on the combo so you know for sure the parse will work, ignore the tryparse
}

并且要将项目添加到ListBox(并且控制它没有太多项目:

private int addToListBox(object item)
{
    if(listbox1.items.count>=Int32.Parse(comboBox1.SelectedText)) return -1;
    listbox1.items.add(item);
    return 0;
 }

当您需要添加到该列表框时,请使用addToListBox而不是listbox1.items.add

答案 1 :(得分:0)

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    int x = 0;
    Int32.TryParse(comboBox1.SelectedText, out x);
    int count = listBox1.Items.Count;
    if (count > x)
    {
        listBox1.Items.Clear();
        int difference = count - x;
        for(int i = 0 ; i < difference ; i++)
        {
            listBox1.Items.RemoveAt(listBox1.Items.Count-1);
        }
    }
}

<强>更新

根据您的评论,请在添加按钮点击事件中编写此代码

int allowedItemsCount = 0;
Int32.TryParse(comboBox1.SelectedText, out allowedItemsCount);
int currentItemsCount = listBox1.Items.Count;

if(currentItemsCount <  allowedItemsCount)
{
    listBox1.Items.Add(textBox1.Text); // I assume your textbox id is TextBox1
}

答案 2 :(得分:0)

这将检查您是否可以向listbox1添加行,如果更改了行数,它将删除它。 按钮添加。

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
            {
                int x = 0;
                Int32.TryParse(comboBox1.SelectedItem.ToString(), out x);
                int count = listBox1.Items.Count;
                while(count > x){
                    listBox1.Items.RemoveAt(count - 1);
                    count = listBox1.Items.Count;
                }
            }
    private void button2_Click(object sender, EventArgs e)
    {
        int x = 0;
        Int32.TryParse(comboBox1.SelectedItem.ToString(), out x);
        if (listBox1.Items.Count < x)
        {
            listBox1.Items.Add(x); //add whatever you want
        }
    }