none
Working with listbox and textbox

    Question

  • Hey guys I have a win form with a listbox with names and a textbox, what i'm trying to achieve is:

    when the user enters a name in the textbox it selects the matching name in the listbox. can anyone help or point me in the right direction. thanks in advance.

    Tuesday, April 25, 2017 3:21 AM

All replies

  • Hi Nathaniel.Ja,

    Thank you for posting here.

    For your question, please try the following code.

      public partial class Form1 : Form
        {
            List<string> _items = new List<string>(); 
            public Form1()
            {
                InitializeComponent();
                _items.Add("One"); 
                _items.Add("Two");
                _items.Add("Three");
                _items.Add("Four");
                _items.Add("Five");
    
                listBox1.DataSource = _items;
            }
    
            private void textBox1_TextChanged(object sender, EventArgs e)
            {
                int index = listBox1.FindString(textBox1.Text);
                listBox1.SetSelected(index, true);
            }
        }

    I hope this would be helpful.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, May 1, 2017 7:11 AM
    Moderator
  • Hi Nathaniel.Ja,

    Any update?

    If your question has been solved, please mark the reply answer.

    If you have something else, please feel free to contact us.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, May 18, 2017 2:47 AM
    Moderator