listbox control operations

  • Thread starter Thread starter Carlos
  • Start date Start date
C

Carlos

Hi,

I am traversing a list box and comparing its entries
with a string. I am not able to find a method that
allows me to use the current index to retrieve the value.
can you help? here is what I got

if ( listBox1.Items.Count > 0 )

{

for (int x = 1; x <= listBox1.Items.Count ; x++)

{

if ( listBox1.GetItemText(x)== textBox1.Text ) //does not
work..

MessageBox.Show("found "+textBox1.Text);

else

listBox1.Items.Add("Item " +
x.ToString());

}

}

else

listBox1.Items.Add(textBox1.Text);



Thanks
 
Thanks scott, I already tried that but it gives me an argument outof range
exception. the x index is valid though... (1) at the time of testing...

Scott B (Was New Guy) said:
if ( listBox1.Items[x].ToString() == textBox1.Text)

Carlos said:
Hi,

I am traversing a list box and comparing its entries
with a string. I am not able to find a method that
allows me to use the current index to retrieve the value.
can you help? here is what I got

if ( listBox1.Items.Count > 0 )

{

for (int x = 1; x <= listBox1.Items.Count ; x++)

{

if ( listBox1.GetItemText(x)== textBox1.Text ) //does not
work..

MessageBox.Show("found "+textBox1.Text);

else

listBox1.Items.Add("Item " +
x.ToString());

}

}

else

listBox1.Items.Add(textBox1.Text);



Thanks
 
never mind, the subscript should be one less.
Thanks, I know C, C++, but wait did they change that
in C#?..no they did not.

Carlos said:
Thanks scott, I already tried that but it gives me an argument outof range
exception. the x index is valid though... (1) at the time of testing...

Scott B (Was New Guy) said:
if ( listBox1.Items[x].ToString() == textBox1.Text)

Carlos said:
Hi,

I am traversing a list box and comparing its entries
with a string. I am not able to find a method that
allows me to use the current index to retrieve the value.
can you help? here is what I got

if ( listBox1.Items.Count > 0 )

{

for (int x = 1; x <= listBox1.Items.Count ; x++)

{

if ( listBox1.GetItemText(x)== textBox1.Text ) //does not
work..

MessageBox.Show("found "+textBox1.Text);

else

listBox1.Items.Add("Item " +
x.ToString());

}

}

else

listBox1.Items.Add(textBox1.Text);



Thanks
 
Back
Top