简体   繁体   English

如何将列表框的行/索引限制为指定的数字?

[英]How do I limit the rows/indexes of a Listbox to a specified number?

How do I restrict the amount of rows/indexes in a list box from a dynamic amount to a constant amount, like 5, programmatically? 如何以编程方式将列表框中的行/索引数量从动态数量限制为常量(如5)? For example, a user inputs data from a text box to a list box up to the fifth row. 例如,用户将数据从文本框输入到列表框直到第五行。 If they attempt again, the program would reject the data the user entered which would prevent the list box from increasing the dynamic row size. 如果他们再次尝试,程序将拒绝用户输入的数据,这将阻止列表框增加动态行大小。

I tried using Selected Index and Selected Items properties such as: 我尝试使用Selected Index和Selected Items属性,例如:

if (ListBox1.SelectedItems.Count != 0)
{
    while (ListBox1.SelectedIndex == 5)
    {
        ListBox1.Items.RemoveAt(ListBox1.SelectedIndex);
    }
}

but it seems that the properties require a list box index to be selected. 但似乎属性需要选择列表框索引。

if(ListBox1.Items.Count < 5){
     ListBox1.Items.Add("asd");
}

Instead of removing, while inserting, you can check if the number of items in ListBox1 are less than 5 and only add an item if it holds true. 插入时,您可以检查ListBox1中的项目数是否小于5,而只是在项目成立时才添加项目,而不是删除。

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

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