Pyromancer Pyromancer - 2 months ago 6
C# Question

Search items in the listbox using index

I am doing a project that uses listbox, I can add items, delete items, update items but I can't search,
this is my code for search

string search = Person.listperson[listBox1.SelectedIndex].lastname;
foreach (String s in search)
{
if (s.Equals(textBox6.Text))
{
//show searched items
MessageBox.Show("Success!");
}
}


can you help me with this?
thanks :)

I have here a code for search,
But it does not apply in the button, how can I apply this on the button?

private void textBox6_TextChanged(object sender, EventArgs e)
{
int index = listBox1.FindString(this.textBox6.Text);
if (0 <= index)
{
listBox1.SelectedIndex = index;
}
}

Answer

Try something like this, add a click event to your button and put your code in it. This works for me.

private void button1_Click(object sender, EventArgs e)
{
    int index = listBox1.FindString(textBox6.Text);
    if (index > -1)
    {
        listBox1.SelectedIndex = index;
    }
}