简体   繁体   English

如何限制列表框中值(条目)的最大数量

[英]How to limit maximum number of values (entries) in a listbox

I currently have two ListBoxes (ListBox1 and ListBox2) with 12 static values in ListBox1 (value1, value2, value3, etc.) that allows the user to transfer values between them using an add and remove button.我目前有两个 ListBoxes(ListBox1 和 ListBox2),ListBox1 中有 12 个静态值(value1、value2、value3 等),允许用户使用添加和删除按钮在它们之间传输值。 I also have a drop down box.我也有一个下拉框。 How do I enforce a max on ListBox2 when a certain selection is made on that drop down box?在该下拉框中进行特定选择时,如何在 ListBox2 上强制执行最大值? In other words, if I just wanted to allow a max of one entry from Listbox1 to be moved to Listbox2 when a value is selected in the drop down box.换句话说,如果我只想在下拉框中选择一个值时允许最多一个条目从 Listbox1 移动到 Listbox2。

 protected void MoveRight(object sender, EventArgs e)
{
    while (ListBox1.Items.Count > 0 && ListBox1.SelectedItem != null)
    {
        ListItem selectedItem = ListBox1.SelectedItem;
        selectedItem.Selected = false;
        ListBox2.Items.Add(selectedItem);
        ListBox1.Items.Remove(selectedItem);
    }
}
protected void MoveLeft(object sender, EventArgs e)
{
    while (ListBox2.Items.Count > 0 && ListBox2.SelectedItem != null)
    {
        ListItem selectedItem = ListBox2.SelectedItem;
        selectedItem.Selected = false;
        ListBox1.Items.Add(selectedItem);
        ListBox2.Items.Remove(selectedItem);
    }
}
private void BindData()
{
    ListBox1.Items.Add(new ListItem("01", "01"));
    ListBox1.Items.Add(new ListItem("02", "02"));
    ListBox1.Items.Add(new ListItem("03", "03"));
    ListBox1.Items.Add(new ListItem("04", "04"));
    ListBox1.Items.Add(new ListItem("05", "05"));
    ListBox1.Items.Add(new ListItem("06", "06"));
    ListBox1.Items.Add(new ListItem("07", "07"));
    ListBox1.Items.Add(new ListItem("08", "08"));
    ListBox1.Items.Add(new ListItem("09", "09"));
    ListBox1.Items.Add(new ListItem("10", "10"));
    ListBox1.Items.Add(new ListItem("11", "11"));
    ListBox1.Items.Add(new ListItem("12", "12"));

}

you can try with this code你可以试试这个代码

int YourMax = 10;
if(ListBox1.Items.Count < Yourmax)
{
   //Add item
       }

linkk : http://msdn.microsoft.com/fr-fr/library/system.windows.forms.listbox.objectcollection_members(v=vs.80).aspx链接: http : //msdn.microsoft.com/fr-fr/library/system.windows.forms.listbox.objectcollection_members( v=vs.80) .aspx

Note : you don't have property for this need注意:你没有这个需要的财产

all properties here : http://msdn.microsoft.com/fr-fr/library/aeb9t2b5(v=vs.80).aspx这里的所有属性: http : //msdn.microsoft.com/fr-fr/library/aeb9t2b5(v=vs.80).aspx

Use a for loop that iterates the lesser of your max and the number of items in the list.使用 for 循环迭代最大值和列表中的项目数中的较小者。

protected void MoveRight(object sender, EventArgs e)
{
    int max = 1;
    int iterations = ListBox1.Items.Count < max ? ListBox1.Items.Count : max
    for(int i = 0; i < iterations; i++)
    {
        ListItem selectedItem = ListBox1.SelectedItem;
        if(selectedItem == null)
            break;

        selectedItem.Selected = false;
        ListBox2.Items.Add(selectedItem);
        ListBox1.Items.Remove(selectedItem);
    }
}

Now you can move max into the class definition and manipulate it however you need to.现在您可以将max移动到类定义中并根据需要对其进行操作。

How do you want it handled in code-behind?您希望如何在代码隐藏中处理它? If you want to make it so that a maximum of one selected item is copied when the drop-down list is set to 1, you could do something like this:如果您希望在下拉列表设置为 1 时最多复制一个选定的项目,您可以执行以下操作:

protected void MoveRight(object sender, EventArgs e)
{
    int max = Convert.ToInt32(DropDownList1.SelectedValue);
    for(int i=0;i<max && ListBox1.Items.Count > 0 && ListBox1.SelectedItem != null; i++)
    {
        ListItem selectedItem = ListBox1.SelectedItem;
        selectedItem.Selected = false;
        ListBox2.Items.Add(selectedItem);
        ListBox1.Items.Remove(selectedItem);
   }

} }

Or, if you want the situation of 2 items selected in ListBox1 when the drop-down list is set to 1 to be a validation error, you could write a handler for the ServerValidate event of a CustomValidator :或者,如果您希望在下拉列表设置为 1 时将ListBox1选择的 2 个项目的情况作为验证错误,您可以为CustomValidatorServerValidate事件编写处理程序:

protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs e)
{
    // There's probably a simpler way to get a count of items selected
    e.IsValid = ListBox1.Items.Count(li=> li.Selected) <= Convert.ToInt32(DropDownList1.SelectedValue);
}

Or, if you want that to happen on the client side, you'll have to write some javascript.或者,如果您希望在客户端发生这种情况,则必须编写一些 javascript。

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

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