c# 在列表框中添加或删除数据

c# adding or deleting data in listbox

我有一个程序,我想在双击任何数据时删除数据。 我怎样才能做到这一点? 这是我的代码的一部分:

this.listBox1.MouseDoubleClick += new MouseEventHandler(listBox1_MouseDoubleClick);



void listBox1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            int index = this.listBox1.IndexFromPoint(e.Location);
            if (index != System.Windows.Forms.ListBox.NoMatches)
            {
                MessageBox.Show(index.ToString());
                listBox1.Items.Remove(listBox1.IndexFromPoint(e.Location));

            }
        }

Remove() 方法需要您要删除的对象作为参数。要使用索引,请使用 RemoveAt() 方法:

listBox1.Items.RemoveAt(index);