Click here to Skip to main content
15,888,341 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more: , +
hi all
question is i want to user put name of the item in serch textbox and then click on search button and when that item finded show tell number of that name in an other textbox here is my code
C#
private void btAdd_Click(object sender, EventArgs e)
{
    tbTell.Text = tbTell.Text.ToString();

    tbName.Text += " | " + tbFamily.Text;
    listnames.Items.Add(tbName.Text+" | "+tbTell.Text);
    string number = tbTell.Text;
    tbName.Text = "";
    tbName.Focus();
}
private void btnSerch_Click(object sender, EventArgs e)
{

    listnames.FindString(tbSerch.Text);


}


but this code is not working for search
but working for add a name with last name and phone number in 1 item in the list

What I have tried:

i tried to use find string function
tried to write a function for myself
but nothing worked yet
Posted
Updated 23-Feb-17 5:57am
Comments
[no name] 23-Feb-17 9:55am    
Of course it "doesn't work", You aren't doing anything with the result of your search.

try this, corrected code and custom search.

private void btAdd_Click(object sender, EventArgs e)
       {
           string nameFamily = tbName.Text + " | " + tbFamily.Text;
           listnames.Items.Add(nameFamily + " | " + tbTell.Text);
       }

       private void btnSerch_Click(object sender, EventArgs e)
       {
           bool isExactNameSearch = true;
           string search  = tbSerch.Text.Trim();
           foreach (string item in listnames.Items)
           {
               var parts = item.Split('|');
               string name = parts[0].Trim();
               string number = parts[2];
               if (isExactNameSearch)
               {

                   if (name == search) // checks exact name
                   {
                       MessageBox.Show(number);
                       break;
                   }

               }
               else {
                   // contains search
                   if (name.Contains(search)) // checks exact name
                   {
                       MessageBox.Show(number);
                       break;
                   }

               }
           }


       }
 
Share this answer
 
Comments
Member 13019612 23-Feb-17 12:06pm    
its nice work bro
thanks a lot
u helped me so many :)
<3
but can i ask u something ?
i want to if its possible
when user write a part of name or family then search and show the number!
i mean when add in list like this :
john|winson|123-313-54|
and when user write in serch textbox like this : winson
then shown the 123-313-54 to the user!
but if its not possible then its not important matter
Karthik_Mahalingam 23-Feb-17 12:08pm    
welcome :)
Member 13019612 23-Feb-17 12:14pm    
please ask my second question :D
I'm sorry if i tired you
but i dont want to open an other topic for this
Karthik_Mahalingam 23-Feb-17 12:19pm    
yes it is possible just add one more condition to it.
Member 13019612 23-Feb-17 12:26pm    
how ? :(
According to ListBox.FindString Method (String) (System.Windows.Forms)[^]:
Quote:
Finds the first item in the ListBox that starts with the specified string.

Your ListBox looks like this:
C#
listnames.Items.Add(tbName.Text+" | "+tbTell.Text);

And you are doing nothing with your search results:
C#
listnames.FindString(tbSerch.Text);

Here is a Google Search with examples of how to do what you want: winform listbox text search[^]
 
Share this answer
 
v3
Comments
Member 13019612 23-Feb-17 9:57am    
can u tell me how to do it specifically?
Graeme_Grant 23-Feb-17 10:00am    
Have a look at the link that I provided.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900