How to find item in a ListBox and selects it if found using WinForms in C#

1 Answer

0 votes
namespace WinFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            listBox1.Items.Add("c#");
            listBox1.Items.Add("java");
            listBox1.Items.Add("c");
            listBox1.Items.Add("c++");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            listBox1.ClearSelected();

            int index = listBox1.FindString(textBox1.Text);

            if (index < 0)
            {
                MessageBox.Show("Item not found.");
                textBox1.Text = String.Empty;
            }
            else
            {
                listBox1.SelectedIndex = index;
            }
        }
    }
}




/*
run:
 

  
*/

 



answered Jan 5, 2024 by avibootz

Related questions

1 answer 131 views
1 answer 155 views
1 answer 140 views
1 answer 146 views
1 answer 139 views
1 answer 167 views
1 answer 133 views
...