2011-01-05 6 views
1

私はサイトを検索しましたが、回答が見つかりませんでした。ListBoxを検索してCで結果を選択

私は "CompetitorDetailsOutput"というリストボックスを持っています。上記の "searchbox"というテキストボックスと "searchbutton"というボタンがあります。リストボックスのデータはconstanly変更され、データを格納する.txtファイルからデータを取得します。 ユーザーは「検索ボックス」と押す「[検索」にデータを入力:

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) 

を次のように次の形式

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); 

にリストボックス、私は次のようにリストボックスを検索できるようにする表示、システムtヒントはリストボックスを検索します。リストボックス内の項目を選択した場合はそれを選択し、一致しない場合はエラーメッセージを表示します。

コードは、C#およびソフトウェアVS 2008 Proの

おかげ

ある
+0

をHI。私は検索機能のためのコードがありません、私はいくつかの他のものとstatments場合は試してみましたが、運がなかった:(しかし、私はリストボックスがどのように役立つ場合は人口を表示することができますか? 「hello world」はありませんが、「hello world」はありませんが、「helloworld」がありますので、「hello world」を検索してから、「helloworld」を検索してスペースがないレコードが見つからない場合は – HadlowJ

答えて

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クエリを持っています、そして、データがListBoxに合っていれば、自動的にそれを選択しますか?http://stackoverflow.com/questions/31034566/how-to-auto-select-listbox-items-from-a-dropdownlist/31034726?noredirect = 1#comment50093972_31034726 – SearchForKnowledge

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; 'を追加しました。 'listbox'を単一のselectにして、' ListBox.SelectionMode = SelectionMode.One; 'を' listに追加したいボックスのクリックイベント。さようなら:) – Anne

関連する問題