【问题标题】:Edit & Remove items in a check list box using buttons使用按钮编辑和删除复选框中的项目
【发布时间】:2012-11-11 01:55:20
【问题描述】:

我有一个将项目添加到检查列表框的按钮。

private void btnDelivery_Click(object sender, EventArgs e)
{
    deliveryForm.deliverytrips = new DeliveryTrips();
    deliveryForm.ShowDialog();
    if (deliveryForm.deliverytrips != null)
    {
        DeliveryTrips newApp = deliveryForm.deliverytrips;
        theDelivery.addDeliveryTrip(newApp);
    }
    updateList();
}

private void updateList()
{
    clbSummary.Items.Clear();
    List<String> listOfDelivery = theDelivery.listDeliveryTrips();
    clbSummary.Items.AddRange(listOfDelivery.ToArray());
}

如何使用按钮进行编辑,我已添加到检查列表框中或从检查列表框中删除它?

现在我有这个用于编辑项目

        int index = clbSummary.SelectedIndex;



        DeliveryTrips selected = theDelivery.getDeliveryTrips(index);


        deliveryForm.deliverytrips = selected;



        deliveryForm.ShowDialog();


        updateList();

但仅在选中且未选中时编辑项目,与删除按钮相同,仅在选中且未选中时删除项目。

谢谢

【问题讨论】:

  • 您的方法的问题在于,您实际上有两种方法可以在CheckedListBox 中指示选择:选择和检查。我倾向于将控件更改为常规的ListBox,或者决定一种检测选择的方法并坚持下去。
  • CheckedListBox 有一个 CheckedItems 枚举。
  • SidHolland 宁愿保留checkedlistbox,因为它是我将添加到checkedlistboxbeucse 的所有项目的摘要,稍后我会将某些项目移动到listbox 所以它希望将一些选中的项目移动到不同的listbox 而不是将一个选定的项目移动到不同的listbox @LarsTech 我已经尝试使用 CheckedItems 但我在它下面得到了一个 req 行

标签: c# .net visual-studio-2010 user-interface checklistbox


【解决方案1】:

删除是最简单的部分。如果您的列表支持选定的单个项目(SelectionModeOne),您可以执行类似的操作

private void DeleteButton_Click(object sender, EventArgs 
{
    clbSummary.Items.RemoveAt(clbSummary.SelectedIndex);
}

现在,如果您支持多项选择(SelectionModeMultiSimple/MultiExtended - 适用于标准列表,而不是 CheckboxLists),以下代码将删除整个选择

private void DeleteButton_Click(object sender, EventArgs e)
{
    for(int i = clbSummary.SelectedIndices.Count - 1; i >= 0; --i)
    {
        clbSummary.Items.RemoveAt(clbSummary.SelectedIndices[i]);
    }
}

这里,颠倒顺序很重要,否则在items中的删除会移动你clbSummary的内容,删除的item越多,偏移量就会越大。

如果你想删除 Checked 项,也是一样的,但是你使用CheckedIndices

private void DeleteButton_Click(object sender, EventArgs e)
{
    for (int i = clbSummary.CheckedIndices.Count - 1; i >= 0; --i)
    {
        clbSummary.Items.RemoveAt(clbSummary.CheckedIndices[i]);
    }
}

要进行编辑,我建议创建一个表单来编辑项目的内容,或者如果它只是一个字符串,也许一个简单的输入对话框就足够了(我确实使用对 Microsoft.VisualBasic 的引用来简化它以使用InputBox)。通常,您的项目可能对应于比字符串更复杂的对象,因此可能需要正确的 Editor(专门用于编辑项目的表单)

private void EditButton_Click(object sender, EventArgs e)
{
    string content = clbSummary.SelectedItem.ToString();
    string newValue = Interaction.InputBox("Provide new value", "New Value", content, -1, -1);
    int selectedIndex = clbSummary.SelectedIndex;
    clbSummary.Items.RemoveAt(selectedIndex);
    clbSummary.Items.Insert(selectedIndex, newValue);
}

【讨论】:

  • 太棒了,谢谢我已经有一个论坛可以使用,让我可以添加和编辑项目然后更新它们。现在只剩下一个问题,如果选择了一个项目,它就可以工作,我只需要更改它,而不是选定的项目,它会对我尝试使用Int index = clbSummary.CheckedItems;的检查项目执行它,但我只是在下面得到一条红线代码,也许我错过了什么?
  • CheckedItems 是一个Collection,它包含许多项目。您必须从这个Collection 中选择一个,所以要进行编辑,我建议使用SelectionMode One,然后编辑SelectedItem
猜你喜欢
  • 2013-08-13
  • 2014-04-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-16
  • 2013-12-18
  • 1970-01-01
相关资源
最近更新 更多