删除列表框中的项目的语法是什么? [英] what is the syntax for delete an item in listbox?

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

问题描述



有人知道吗?



Can anyone know?

what is the syntax for delete an item in listbox?

推荐答案

亲爱的朋友,

我正在为您提供删除一项或多项的解决方案.

要选择多个项目,您需要设置.SelectionMode属性:

listBox1.SelectionMode = SelectionMode.MultiExtended;

接下来,假设您已通过编程方式将项目添加到ListBox中,而不是使用DataSource(如果ListBox使用数据源,则无法直接从Items集合中将其删除).

只是为了完成删除操作而以相反的顺序完成
.

您需要做的就是以相反的顺序使用.RemoveAt()方法(这很重要,请向后进行,否则它们将无法正确删除):-
Dear Friend,

I am giving you the solution to delete one or more than one item.

For multiple selection of Items, you need to set the .SelectionMode property:

listBox1.SelectionMode = SelectionMode.MultiExtended;

Next, I am assuming that you have added the Items to your ListBox programmatically rather than using a DataSource (you wouldn''t be able to remove them from the Items collection directly if the ListBox is using a DataSource).

It is done n the reverse order just in order to complete the operation of deletion successfuly
.

All you need to do is use the .RemoveAt() method, in reverse order (it is important to do it backwards, otherwise they won''t be removed properly):-
for (int i = listBox1.SelectedIndices.Count-1; i >= 0; i--)
{
 listBox1.Items.RemoveAt(listBox1.SelectedIndices[i]);
}


OR


OR

for (int i = 0; i < listBox1.SelectedItems.Count; i++)
      {
        listBox1.Items.Remove(listBox1.SelectedItems[i].ToString());
        i--;
      }


ListBox1.Items.Remove(ListBox1.SelectedItem);




Hi,

protected void remove_Click(object sender, EventArgs e)
    {
        if (ListBox1.SelectedItem.Text == null)
        {
            lblError.Text = "Please select an item for deletion.";
        }
        else
        {
            string remove = ListBox1.SelectedItem.Text;
            ListBox1.Items.Remove(remove);
        }
    }


这篇关于删除列表框中的项目的语法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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