【问题标题】:How can I remove item (entry from textbox) in listbox如何删除列表框中的项目(文本框中的条目)
【发布时间】:2013-06-14 09:36:32
【问题描述】:

在我的表单中有 TextBox1 和 ListBox1、buttonAdd、buttonRemove

buttonAdd => 好的,我可以做到。 buttonRemove:当你删除一个部分时: - 从文本框中删除条目:勾选列表框中的一项应该被删除,如果有,如果没有,则没有找到该消息 - 删除列表框中的选中项

这是我的想法:

     private void butonRemove_Click(object sender, EventArgs e)
    {
        if (textbox1.Text != "")
        {
            int i = 0;
            while (i <= listbox1.Items.Count)
            {
                string Item_remove = textbox1.Text;
                if (listbox1.Items[i].ToString().Contains(Item_remove))
                {
                    DialogResult conf_remove;
                    conf_remove = MessageBox.Show("Do you wwant to remove: " + listbox1.Items[i].ToString(), "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                    if (conf_remove == DialogResult.Yes)
                    {
                        listbox1.Items.RemoveAt(i);
                        break;
                    }
                    else if (conf_remove == DialogResult.No)
                        i++;
                }
                else
                {
                    MessageBox.Show("Not found");
                    break;
                }
            }

            textbox1.Text = "";
            textbox1.Focus();
        }
        else if (listbox1.SelectedIndex < 0)
            MessageBox.Show("Please select item to remove");
        else
            listbox1.Items.Remove(listbox1.SelectedItem);

}

请帮我解决一下,谢谢

【问题讨论】:

  • 这是 WPF 应用程序,对吗?
  • 能否请您发布一些您拥有的代码(xaml + cs)以便更好地理解问题?

标签: c# textbox data-entry listboxitems remove-method


【解决方案1】:

这是删除项目的代码。

private void buttonRemove_Click(object sender, EventArgs e) {
        if (listBox1.SelectedIndex == -1) { // Not Selected Anything
            MessageBox.Show("Select an item to delete");
        }
        else {
            listBox1.Items.RemoveAt(listBox1.SelectedIndex); // Remove item
        }
    }

【讨论】:

  • 从 textbox1 输入项目时如何删除
【解决方案2】:

这样的?

void buttonRemove_Click(object sender, EventArgs e) 
{
    string matchcode = TextBox1.Text;

    ListItem item = this.ListBox1.Items.FindByText(matchcode);

    if (item != null)
    {
        //found
        this.ListBox1.Items.Remove(item);
    }
    else
    {
        //not found
        MessageBox.Show("is not found");
    }
}

【讨论】:

    【解决方案3】:

    这就是您要查找的内容,它也可以控制您需要的所有验证。

    if (ListBox.SelectedIndex == -1)
            {
                if (ListBox.NumberOfItems == 0) // if no index, need to select a row!
                {
                    MessageBox.Show("No items to remove!");
                }
                else
                {
                    MessageBox.Show("Please select an item first!");
                }
            }
            else
            {
                 ListBox.Items.RemoveAt(lstStorage.SelectedIndex);
            }
    

    这将删除已选择的项目,并进行您需要的验证。

    我还注释了一些代码以向您展示部分的含义。 如果您需要有关其工作原理的更多解释,请询问:)

    【讨论】:

    • 我想了解更多关于从文本框条目中删除的案例,如果有多个类似的条目,例如 THE THE THE man 或 woman,您需要检查您要删除哪些条目。如果列表框中没有条目,则找不到消息
    • 不幸的是,我不完全理解您的要求,if 语句中的验证将验证列表框是否为空。要验证列表框中的文本,您将(为了让生活更轻松)通过将输入设置为小写来验证输入。如果您想限制用户可以输入到列表框中的内容,那么也许只需使用带有允许使用的术语的单选按钮。但是,要使文本框条目小写,您只需调用 textbox.text.ToLower 即可,这会使验证短语变得更加容易。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-07
    • 2010-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多