Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

finding an Item in listbox

Status
Not open for further replies.

tek1ket

MIS
Jan 25, 2009
70
IR
i have a listbox with Items in it, and also a textbox. i want when the user type something in textbox, at first the new Item should be checked with the available Items in listbox so i used this syntax
lstProject.Items.Contains(txtPrj.Text.ToString())
but it always returns Flase as a result.
so am i wrong about the syntax?
 
The item in the listbox exactly[/] matches the string you're getting from the textbox?

HarleyQuinn
---------------------------------
You can hang outside in the sun all day tossing a ball around, or you can sit at your computer and do something that matters. - Eric Cartman

Get the most out of Tek-Tips, read FAQ222-2244: How to get the best answers before posting.

 
this returns false because Items contains ListItems, not strings. something like this should work
Code:
var item = listProject.Items.FindByText(txtPrj.Text);
if(item != null)
{
   item.Selected = true;
}
this requires the text to be an exact match. otherwise the logic will look like this
Code:
foreach(var item in lstProject.Items)
{
   if(!item.Text.StartsWith(txtPrj.Text) continue;
   item.Selected = true;
   break;
}

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Jason, While I agree with the theory, you can successfully check strings against the Items (which I thought was an ObjectCollection?) using Contains() (this is admittedly based on the Forms ListBox and tested in a version prior to 3.0, but neither is specified by the OP).

A rather long winded (and overly simplistic) example but:
Code:
ListBox listBox1 = new ListBox();

            listBox1.Size = new System.Drawing.Size(100, 100);
            listBox1.Location = new System.Drawing.Point(100, 100);

            this.Controls.Add(listBox1);

            listBox1.BeginUpdate();

            listBox1.Items.Add("I'm in there");

            listBox1.EndUpdate();

            TextBox TextTest = new TextBox();

            TextTest.Size = new System.Drawing.Size(100, 100);
            TextTest.Location = new System.Drawing.Point(100, 50);

            this.Controls.Add(TextTest);

            TextTest.Text = "I'm in there";

            if (listBox1.Items.Contains(TextTest.Text.ToString()) == true)
            {
                MessageBox.Show("'" + TextTest.Text.ToString() + "' was in there");
            }

            TextTest.Text = "I'm not";

            if (listBox1.Items.Contains(TextTest.Text.ToString()) == true)
            {
                MessageBox.Show("'" + TextTest.Text.ToString() + "' was in there on the second run");
            }
            else
            {
                MessageBox.Show("But '" + TextTest.Text.ToString() + "' wasn't in there");
            }

HarleyQuinn
---------------------------------
You can hang outside in the sun all day tossing a ball around, or you can sit at your computer and do something that matters. - Eric Cartman

Get the most out of Tek-Tips, read FAQ222-2244: How to get the best answers before posting.

 
this one worked
lstProject.FindStringExact(txtPrj.Text.ToString());

thank you all
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top