簡體   English   中英

如何將列表框的行/索引限制為指定的數字?

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

如何以編程方式將列表框中的行/索引數量從動態數量限制為常量(如5)? 例如,用戶將數據從文本框輸入到列表框直到第五行。 如果他們再次嘗試,程序將拒絕用戶輸入的數據,這將阻止列表框增加動態行大小。

我嘗試使用Selected Index和Selected Items屬性,例如:

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

但似乎屬性需要選擇列表框索引。

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

插入時,您可以檢查ListBox1中的項目數是否小於5,而只是在項目成立時才添加項目,而不是刪除。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM