2011-01-05 72 views
1

我已經搜索了該網站,但無法找到答案。在C中搜索列表框並選擇結果#

我有一個名爲「CompetitorDetailsOutput」的列表框然後我有一個名爲「searchbox」的文本框和一個名爲「searchbutton」的按鈕列表框中的數據一直在改變,並從一個.txt文件中獲取數據來存儲數據以下列格式

string.Format("{0},{1},{2},{3},{4},{5},{6},{7},{8},{9},{10},{11},{12}", Name, CPSA, PostCode, Rank, Score1, Score2, Score3, Score4, Score5, Score6, Score7, Score8, TotalSingleScore); 

列表框然後顯示如下

string.Format("{0,-20}|{1,-10}|{2,-9}|{3,-7}|{4,2}|{5,2}|{6,2}|{7,2}|{8,2}|{9,2}|{10,2}|{11,2}|{12,3}", Name, CPSA, PostCode, Rank, Score1, Score2, Score3, Score4, Score5, Score6, Score7, Score8, TotalSingleScore) 

我想能夠搜索列表框如下: 用戶僅將數據輸入到「搜索框」並按下「搜索按鈕」 ,系統t母雞搜索列表框,如果它發現它選擇列表框中的項目,如果沒有,則選擇一個近距離匹配,如果沒有近似匹配,則顯示錯誤消息。

代碼是C#和軟件VS 2008專業

感謝

+0

喜。我沒有搜索功能的代碼,我確實嘗試了一些如果與其他一些事情,但沒有運氣:(但我可以告訴你如何填充列表框如果有幫助嗎?通過近距離匹配我的意思是像空間忽略所以用戶搜索是「你好世界」,沒有「你好世界」,但有「helloworld」,所以也許像搜索「你好世界」,然後搜索「helloworld」,如果沒有找到空間記錄 – HadlowJ 2011-01-05 23:16:41

回答

1

1 /創建要在
2 /添加您的物品爲對象搜索 的屬性的對象而不是作爲字符串
3.使用您想要在 中顯示的格式覆蓋ToString()列表框
4.使用Linq查詢您喜歡的對象。

var result = from o in ListBox.Items.OfType<yourClass>() 
      where o.Whatever == yourCriteria 
      select o; 
4

嘗試這樣的事情讓你的 '匹配' 的算法開始:

foreach (var item in ListBox.Items) 
{ 
    if (item.Text.Contains(searchArg)) 
    { 
     //select this item in the ListBox. 
     ListBox.SelectedValue = item.Value; 
     break; 
    } 
} 
+0

什麼我有一個SQL查詢有數據,如果數據匹配列表框,然後自動選擇它?http://stackoverflow.com/questions/31034566/how-to-auto-select-listbox-items-from-a-dropdownlist/31034726?noredirect = 1#comment50093972_31034726 – SearchForKnowledge 2015-06-24 19:01:00

1
private void FindAllOfMyString(string searchString) 
    { 
     // Set the SelectionMode property of the ListBox to select multiple items. 
     ListBox.SelectionMode = SelectionMode.MultiExtended; 

     // Set our intial index variable to -1. 
     int x = -1; 
     // If the search string is empty exit. 
     if (searchString.Length != 0) 
     { 
      // Loop through and find each item that matches the search string. 
      do 
      { 
       // Retrieve the item based on the previous index found. Starts with -1 which searches start. 
       x = ListBox.FindString(searchString, x); 
       // If no item is found that matches exit. 
       if (x != -1) 
       { 
        // Since the FindString loops infinitely, determine if we found first item again and exit. 
        if (ListBox.SelectedIndices.Count > 0) 
        { 
         if (x == ListBox.SelectedIndices[0]) 
          return; 
        } 
        // Select the item in the ListBox once it is found. 
        ListBox.SetSelected(x, true); 
       } 
      } while (x != -1); 
     } 
    } 

private void Srchbtn_Click(object sender, EventArgs e) 
    { 
     FindAllOfMyString(SrchBox.Text); 
    } 

http://msdn.microsoft.com/en-us/library/system.windows.forms.listbox.findstring(v=vs.71).aspx

+0

*匿名用戶發表評論:*我在'listbox.setselected'行之後添加了'break;',因爲代碼被卡在一個循環中,添加'break;'解決了這個問題。你希望'listbox'是單選,然後將'ListBox.SelectionMode = SelectionMode.One;'添加到'list盒子點擊事件。再見:) – Anne 2012-01-01 02:06:36