Rabbia Annum
Rabbia Annum

Reputation: 41

How to access the indexes of all listbox items?

I am creating an application to search the user typed word from list box. I want to Show only that items in listbox which are matched with the character typed by the user. I am unable to find the exact syntax for this.

  private void textBox1_TextChanged(object sender, EventArgs e)
    {
        string a=textBox1.Text;
        for (int i = 0; i < listBox1.Items.Count; i++)
        {
            if(a[0]==listBox1.Items(i).char[0])//how to do this?
                    {........
                    }

        }
    }

Upvotes: 1

Views: 670

Answers (2)

MethodMan
MethodMan

Reputation: 18843

if you want to check the char of a do something like this also if you are not getting the "Text / String Value.. add the .ToString(); after listBox1.Items[i].ToString();

if(a[i]== listBox1.Items[i])
{
  //i is the incremented value here..
}

 foreach (char valchar in a)
 {
   // do your logic.. 'X' single quotes for Char
 }

 if you want to check for a string in a do 

 foreach (string valString in a)
 {
   // do your logic for a string check if valString = "X" for example "" double quotes for
 }

Upvotes: 1

Mahmoud Gamal
Mahmoud Gamal

Reputation: 79889

Like this:

 string a = textBox1.Text;
 for (int i = 0; i < listBox1.Items.Count; i++)
 {
     if( a[0] == listBox1.Items[i].Text)
     {
           //Do Something...
      }
 }

Upvotes: 0

Related Questions