Results 1 to 9 of 9

Thread: Making a listbox highlight items as you type?

  1. #1

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    Making a listbox highlight items as you type?

    Hi!
    How can i make the listbox highlight the item as the user types the item text?
    ie, if there is an item called "Apple" in the list and the user clicks on the listbox and starts typing "app..." it should highlight apple.

    I thought this would be enabled by default but I guess it's not. It jumps to the item whose first character matches the last key stroke. Is this set through one of the extended listbox styles?
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  2. #2
    Hyperactive Member
    Join Date
    Mar 2002
    Location
    Boston, MA
    Posts
    391

    Re: Making a listbox highlight items as you type?

    Hi,

    Have you tried to set the AutoCompleteSource and AutoCompleteMode? It doesn't highlight but makes suggestions based upon what's in the box. Make sure the DropDownStyle property is set to DropDown and not DropDownList.
    Last edited by wy125; Jul 30th, 2008 at 05:05 PM.

  3. #3

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    Re: Making a listbox highlight items as you type?

    Hi,
    thanks for your reply. I'm talking about a ListBox control however, not a ComboBox those properties you mentioned are for combobox
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  4. #4
    Hyperactive Member
    Join Date
    Mar 2002
    Location
    Boston, MA
    Posts
    391

    Re: Making a listbox highlight items as you type?

    Haha yes sorry. Apparently, I'm not drinking enough coffee.

    There may be an easier way to do this but try the code below and see if that helps you out. The code is for the KeyPress event of the ListBox. It assumes there is string declared in your class named inputText. You will probably want to add additional code for handling other types of key presses. Hopefully this is what you had in mind...

    Code:
            private void listBox1_KeyPress(object sender, KeyPressEventArgs e)
            {
                // flag that the event has been handled.  This prevents the autoselect.
                e.Handled = true; 
    
                // check if the user hit the backspace key
                if (e.KeyChar == (char)Keys.Back)
                {
                    if (inputText.Length > 0)
                        inputText = inputText.Substring(0, inputText.Length - 1);
                }
                else
                {
                    inputText += e.KeyChar.ToString();
    
                    // search the listbox items
                    int i = listBox1.FindString(inputText);
    
                    
                    if (i != ListBox.NoMatches)
                        listBox1.SelectedIndex = i;
                   
    
                }
                
            }
    Last edited by wy125; Jul 30th, 2008 at 09:42 PM.

  5. #5

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    Re: Making a listbox highlight items as you type?

    haha yea sometimes more coffee helps.
    Thanks for the tip, I'll try it out if i don't find anything else, because I'm almost certain the listbox itself should support that, but alas no one seems to know how

    Quote Originally Posted by wy125
    Haha yes sorry. Apparently, I'm not drinking enough coffee.

    There may be an easier way to do this but try the code below and see if that helps you out. The code is for the KeyPress event of the ListBox. It assumes there is string declared in your class named inputText. You will probably want to add additional code for handling other types of key presses. Hopefully this is what you had in mind...

    Code:
            private void listBox1_KeyPress(object sender, KeyPressEventArgs e)
            {
                // flag that the event has been handled.  This prevents the autoselect.
                e.Handled = true; 
    
                // check if the user hit the backspace key
                if (e.KeyChar == (char)Keys.Back)
                {
                    if (inputText.Length > 0)
                        inputText = inputText.Substring(0, inputText.Length - 1);
                }
                else
                {
                    inputText += e.KeyChar.ToString();
    
                    // search the listbox items
                    int i = listBox1.FindString(inputText);
    
                    
                    if (i != ListBox.NoMatches)
                        listBox1.SelectedIndex = i;
                   
    
                }
                
            }
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  6. #6
    Hyperactive Member
    Join Date
    Mar 2002
    Location
    Boston, MA
    Posts
    391

    Re: Making a listbox highlight items as you type?

    Yeah it would be a lot easier if the control itself supported that functionality somehow. If you do find such a solution make sure you post it.

  7. #7
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Making a listbox highlight items as you type?

    I don't think the listbox does have this feature in windows forms. wy's method should work for you.

  8. #8

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    Re: Making a listbox highlight items as you type?

    grr i thoguht there'd be a nice way
    thanks wy124... i changed yoru code a bit to do the same thing. It clears the "search" after 2.5 seconds, and it also keeps this in the list boxs tag....

    Code:
            struct listMatch
            {
                public string text;
                public int lastTick;
            }
    
            private void listBox_KeyPress(object sender, KeyPressEventArgs e)
            {
                ListBox lst = (ListBox)sender;
                listMatch match;
    
                if (lst.Tag == null  ||  !(lst.Tag is listMatch) )
                {
                    match.text = string.Empty;
                    match.lastTick = Environment.TickCount;
                }
                else
                    match = (listMatch)lst.Tag;
    
                if (Environment.TickCount - match.lastTick > 2500)
                    match.text = string.Empty;
               
                //Backspace
                if (e.KeyChar == (char)Keys.Back && match.text.Length > 0)
                    match.text = match.text.Substring(0, match.text.Length - 1);
                else if (char.IsLetter(e.KeyChar))
                {
                    match.text += e.KeyChar;
                    int index = lst.FindString(match.text);
                    if (index != ListBox.NoMatches)
                    {
                        lst.SelectedItems.Clear();
                        lst.SelectedIndex = index;
                    }
                }
                match.lastTick = Environment.TickCount;
                e.Handled = true;
                lst.Tag = match;
            }
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  9. #9
    Frenzied Member Andrew G's Avatar
    Join Date
    Nov 2005
    Location
    Sydney
    Posts
    1,587

    Re: Making a listbox highlight items as you type?

    If you make a hidden textbox (outside the screen with visible set to true), then use something like this, you can kinda make it work. I guess you'll need to add a timer to remove the text in the textbox after a second or so to make it function a bit better.
    Code:
            public Form1()
            {
                InitializeComponent();
                listBox1.MouseUp += new MouseEventHandler(listBox1_MouseUp);
                textBox1.KeyPress += new KeyPressEventHandler(textBox1_KeyPress);
            }
    
            void textBox1_KeyPress(object sender, KeyPressEventArgs e)
            {
                if (e.KeyChar == (char)Keys.Back)
                {
                    textBox1.Text = "";
                }
            }
    
            void listBox1_MouseUp(object sender, MouseEventArgs e)
            {
                textBox1.Focus();   
            }
    
            private void textBox1_TextChanged(object sender, EventArgs e)
            {
                if (textBox1.Text != "")
                {
                listBox1.SelectedIndex = listBox1.FindString(textBox1.Text);
                }
            }

    Edit: Updated code slightly
    Last edited by Andrew G; Aug 6th, 2008 at 10:53 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width