listbox control operations

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
 
C

Carlos

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
 
C

Carlos

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top