2016-04-05 31 views
0

您好,我有一個列表框中的項目列表 Landen,Lucie,812-692-5576,Jig是Up 我需要搜索名稱或電話或Ratios .... 。 我已經輸入這個代碼,但他與搜索字符串ATT任何地方行在列表框中搜索,用逗號隔開

 int x = 0; 

     string match = textBox1.Text; 

     if (textBox1.Text.Length != 0) 
     { 
      bool found = true; 
      while (found) 
      { 
       if (listBox1.Items.Count == x) 
       { 
        listBox1.SetSelected(lastMatch, true); 
        found = false; 
        textBox2.Text = listBox1.SelectedItem.ToString(); 
       } 
       else 
       { 
        listBox1.SetSelected(x, true); 
        match = listBox1.SelectedItem.ToString(); 
        if (match.Contains(textBox1.Text)) 
        { 
         lastMatch = x; 
         found = false; 
        } 
        x++; 
        textBox2.Text = listBox1.SelectedItem.ToString(); 
       } 
      } 

太感謝你了

回答

0

我從你的代碼讀出什麼,你需要一個邏輯搜索List內容突出顯示第一個匹配列表項並更新TextBox值SelectedListItem。

這段代碼做了我所解釋的。

for(int i=0;i<listBox1.Items.Count;++i) 
{ 
    if(listBox1.Items[i].ToString().Contains("searchString")) 
    { 
      // match 
      listBox1.SetSelected(i, true); 
      textBox2.Text = listBox1.SelectedItem.ToString();    
    } 
} 
+0

'listBox1.SelectedItem'將在'textBox2.Text = listBox1.SelectedItem.ToString執行()的時間空;'因爲您在該行之後分配selectedItem –

+0

@un-lucky感謝您突出顯示這一點,我應該首先進行選擇。現在更新。 –

+0

太棒了!現在它的正確答案 –

0

您可以像下面通過的foreach實現這一目標:

bool itemFound = false;  
object currentObject = null; // remains null if no match found 
foreach (var item in listBox1.Items) 
{ 
    if (item.ToString().Contains(txtbox1.Text)) 
    { 
     currentObject = item; // will give you the current item 
     textBox2.Text=item.ToString();     
     break; // Break; if you need to check for the first occurance 
    } 
} 
if (currentObject != null) 
    listBox1.SelectedItem = currentObject; 
else 
    textBox2.Text = "Item not found"; 
1

創建一個類來保存您的信息。

public class Card 
{ 
    public string Name { get; set; } 
    public string Phone { get; set; } 
    public string Note { get; set; } 

    public override string ToString() 
    { 
     return string.Format("{0}, {1}, {2}", Name, Phone, Note); 
    } 
} 

根據需要創建它的實例。

var card = new Card { Name = "Landen, Lucie", Phone = "812-692-5576", Note = "Jig Is Up" }; 

使用該類填充您的ListBox

var allTheCards = new List<Card>(); 
// populate the cards 

listBox1.DataSource = allTheCards; 

現在你可以搜索特定的領域。您必須在代碼中進行其他更新以適應使用您自己的類而不僅僅是一個字符串。

Card match = listBox1.SelectedItem; 

if (match.Name.Contains(textBox1.Text) 
    || match.Phone.Contains(textBox1.Text) 
    || match.Note.Contains(textBox1.Text)) 
{ 
    ... 
} 

擴展您的評論和以什麼TAW建議,從文件中讀取數據時,你仍然可以使用此解決方案。只需循環查看結果並使用結果填充Card類。

這裏是一個可能的方式做到這一點,使用LINQ:

listBox1.DataSource = (from lines in File.ReadAllLines("someFile.txt") 
         let parts = lines.Split(',') 
         select new Card 
            { 
             Name = string.Format("{0} {1}", parts[1], parts[0]), 
             Phone = parts[2], 
             Note = parts[3] 
            }).ToList(); 
+0

非常感謝,但我從文本文件加載我的列表框 –

+1

您可以通過將它們放在','上來加載該文本文件行中的類實例。只需添加一個構造函數即可:'public Card(string input){var p = input.Split(','); Name = p [0];電話= P [1];注= P [2];}' – TaW

相關問題