2017-02-18 49 views
0
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作爲輸出和清晰的休息列表框中

+0

你的問題不清楚,你的代碼也沒有多大幫助。當你點擊按鈕,你檢查,看看是否在文本框中的文本'textBox1'包含在''List'和lst1'如果是則清除它們的休息嗎?您可能想要修改您的問題以明確您要完成的任務。從你上次評論:_so當文本框的值爲7時,我的列表框必須顯示6-7-8-9-10作爲輸出並清除所有項目列表框中的剩餘項?你的問題不清楚。你可能想細讀...... [如何創建一個最小的,完整的,並且可驗證的示例](http://stackoverflow.com/help/mcve) – JohnG

+0

我的列表框中有4項如果文本框中給定值是存在於任何2日性格纔剛剛顯示整個值,其中第2個字符是在列表框中avaliableñ清晰其餘3 – user7388587

+0

你能不能簡單的通過'listbox.Items'循環,並刪除其中的第二個字符不會在文本框中輸入字符匹配的行? – JohnG

回答

0

所有項目使用您已發佈什麼,我不理解究竟你要實現的目標。使用兩(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"); 
    } 
}