Thread: Listbox search

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    3

    Listbox search

    I recently made a Windows Application in Microsoft Visual C++ 2005 that contains a listbox. The problem i noticed is that when the listbox is selected, and the user presses a key, the listBox selects the first item that starts with said letter (Which works well); however, if the user presses 2 letters in a row, the listBox doesn't recognize the first letter and goes automatically to the first item that starts with the second letter. For instance, if i have the items 'abc' and 'bcd' and i press 'ab', the listBox selects the item 'bcd' instead of 'abc'. I searched through the net and didn't find a concrete answer. Could anyone help?

  2. #2
    Registered User Joelito's Avatar
    Join Date
    Mar 2005
    Location
    Tijuana, BC, México
    Posts
    310
    See LB_FINDSTRING or LB_FINDSTRINGEXACT.
    * PC: Intel Core 2 DUO E6550 @ 2.33 GHz with 2 GB RAM: Archlinux-i686 with xfce4.
    * Laptop: Intel Core 2 DUO T6600 @ 2.20 GHz with 4 GB RAM: Archlinux-x86-64 with xfce4.

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    3
    (sorry to bump this)

    I came up with a code to optain the key values and make the search, but it isn't working atm:

    Code:
    #pragma endregion
         String^ buffer;
    System::Void listBox1_KeyDown(System::Object^  sender, System::Windows::Forms::KeyEventArgs^  e) 
    		 {
    			 buffer = gcnew String("");
    			 if (e->KeyCode >= Keys::A && e->KeyCode <= Keys::Z){
    				 if (buffer->IsNullOrEmpty(buffer)){
    					 buffer = e->KeyData.ToString();
    				 }
    				 else 
    					 buffer += e->KeyData.ToString();
    			 }
    			 listBox1->BeginUpdate();
    		 }
    
    System::Void listBox1_KeyUp(System::Object^  sender, System::Windows::Forms::KeyEventArgs^  e) 
    		 {
    			 int index = listBox1->FindString(buffer);
    			 listBox1->EndUpdate();
    			 if(index > -1)
    				 listBox1->SelectedIndex = index;
    			 else
    				 buffer = "";
    		 }
    Does anyone know what do i need to change? Thanks a lot in advance

  4. #4
    Registered User Joelito's Avatar
    Join Date
    Mar 2005
    Location
    Tijuana, BC, México
    Posts
    310
    Oh! Visual C++ .NET .... sorry, I think that you were using pure API.
    * PC: Intel Core 2 DUO E6550 @ 2.33 GHz with 2 GB RAM: Archlinux-i686 with xfce4.
    * Laptop: Intel Core 2 DUO T6600 @ 2.20 GHz with 4 GB RAM: Archlinux-x86-64 with xfce4.

  5. #5
    Registered User
    Join Date
    Apr 2007
    Posts
    3
    Quote Originally Posted by Joelito View Post
    Oh! Visual C++ .NET .... sorry, I think that you were using pure API.
    Yea, my bad. Should've specified in the first post. Btw, anyone has a suggestion? I can't seem to make it work at all

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 09:40 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM