通过特定项目标识列表框项目

  • 本文关键字:项目 标识 列表 c#
  • 更新时间 :
  • 英文 :

private void Form1_Load(object sender, EventArgs e){
        for (int i = 0; i < listBox1.Items.Count; i++)
        {
            lst.Add(listBox1.Items[i].ToString());
        }
        foreach (var item in lst) 
        {
            lst1.Add(item[2].ToString());
        }
}
private void button1_Click(object sender, EventArgs e) {
        if (lst1.Contains(textBox1.Text))
        {
           // *Need to find that particular item from listbox and clear rest of them*\
        }
 }

我的输入是

  • 1-2-3-4-5

  • 6-7-8-9-10

  • 1-9-4-2-3

  • 7-8-1-4-9

    所以当文本框有值7时然后,我的列表框必须显示 6-7-8-9-10 作为输出,并清除ListBox中的所有项目

    中的所有项目

使用您发布的内容,我不了解您到底要实现的目标。使用两个(2)Listlstlst1看起来很奇怪。没有更多有关您的最终目标的信息,我质疑您为什么要按照自己的方式这样做。

下面的代码删除了ListBox中的项目,其中第二个字符与文本框中的字符不匹配。希望这会有所帮助。

private void button1_Click(object sender, EventArgs e) {
  if (lst1.Contains(textBox1.Text)) {
    int index = lst1.IndexOf(textBox1.Text);
    string temp = listBox1.Items[index].ToString();
    MessageBox.Show("Character: " + textBox1.Text + " Found at index: " + index + " the string is: " + temp);
    listBox1.Items.Clear();
    listBox1.Items.Add(temp);
    // *Need to find that particular item from listbox and clear rest of them*\
  } else {
    MessageBox.Show("Not Found");
  }
}

最新更新