如何从列表框中删除项目(文本框中的条目) [英] How can I remove item (entry from textbox) in listbox

查看:216
本文介绍了如何从列表框中删除项目(文本框中的条目)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 我的表单中有TextBox1  ListBox1,buttonAdd,buttonRemove 

buttonAdd =>好的,我可以做到。 buttonRemove: 删除部分: - 删除 entry 来自文本框:检查一项 应删除列表框项目,如果有clear,如果 ,消息 未找到 - 删除所选项目 列表框





这是我的想法:

  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( 您是否愿意删除: + listbox1.Items [i] .ToString(), 警告,MessageBoxButtons.YesNo,MessageBoxIcon.Warning);
if (conf_remove == DialogResult.Yes)
{
listbox1.Items.RemoveAt(i);
break ;
}
else if (conf_remove == DialogResult.No)
i ++;
}
else
{
MessageBox.Show( 未找到);
break ;
}
}

textbox1.Text = ;
textbox1.Focus();
}
else if (listbox1.SelectedIndex < 0
MessageBox.Show( 请选择要删除的项目);
else
listbox1.Items.Remove(listbox1.SelectedItem);





请帮我解决。谢谢大家

解决方案

请尝试

  if (listbox1.Items。包含(Item_remove))
{
// 要求确认
// 如果是,则
listbox1.Items.Remove(Item_remove);
}

else
{
MessageBox.Show( 未找到);
}


这是删除项目的代码。 

< pre lang = cs > private void buttonRemove_Click(object sender,EventArgs e){
if(listBox1.SelectedIndex == -1){// Not Selected Anything
MessageBox.Show(& quot;选择要删除的项目& quot;);
}
else {
listBox1.Items.RemoveAt(listBox1.SelectedIndex); //删除项目
}
} < / pre >


In my form have TextBox1 and ListBox1, buttonAdd, buttonRemove

buttonAdd => OK, I can do it. buttonRemove: When you delete a section: - Delete entry from textbox: Check one item in the listbox item should be deleted, if there are clear, if not, the message is not found - Delete the selected item in listbox



This is my idea:

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);



Please help me fix it. Thank everyone

解决方案

Please try this

if (listbox1.Items.Contains(Item_remove))
            {
            //ask for confirmation
            //if yes then
              listbox1.Items.Remove(Item_remove);
            }

        else
        {
            MessageBox.Show("Not found");
        }


Here's the code for remove item.

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


这篇关于如何从列表框中删除项目(文本框中的条目)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆