Search Item in a ListBox in C#
Submitted by donbermoy on Wednesday, June 18, 2014 - 09:33.
In this tutorial, i will teach you how to create a program that searches an item inside the listbox using C#.
Now, let's start this tutorial!
1. Let's start with creating a Windows Form Application in C# for this tutorial by following the following steps in Microsoft Visual Studio: Go to File, click New Project, and choose Windows Application and name your project as Search Item in a ListBox.
2. Next, add only two Buttons named Button1 and labeled it as "Add Item" for adding an item from the textbox and displayed in ListBox and Button2 labeled as "Search Item" for searching an item in the ListBox. Insert a textbox named TextBox1 and ListBox named ListBox1. You must design your interface like this:
3. For button1_click as Add Item button, put this code below.
The code above gets the inputted string of the textbox to be added in the listview as we have the Items.Add function of the ListBox. After adding the item to the listbox, the textbox will now be cleared.
4. For button2_click as Search Item button, put this code below.
Output:
For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below.
Best Regards,
Engr. Lyndon Bermoy
IT Instructor/System Developer/Android Developer/Freelance Programmer
If you have some queries, feel free to contact the number or e-mail below.
Mobile: 09488225971
Landline: 826-9296
E-mail:[email protected]
Add and Follow me on Facebook: https://www.facebook.com/donzzsky
Visit and like my page on Facebook at: https://www.facebook.com/BermzISware
- public void Button1_Click(System.Object sender, System.EventArgs e)
- {
- ListBox1.Items.Add(TextBox1.Text);
- TextBox1.Clear();
- }
- public void Button2_Click(System.Object sender, System.EventArgs e)
- {
- //Initialized a variable named index as an integer and input as a string.
- int index;
- string input;
- //Use variable input as an InputBox.
- input = (string)(InputBox("Enter an item to search:"));
- // Use variable index to hold the value of variable input and will find the content of it
- index = System.Convert.ToInt32(ListBox1.FindString(input));
- // If index variable will not be equal to the Items inside of the ListBox it will display "Item is found"
- if (index != ListBox.NoMatches)
- {
- MessageBox.Show("Item is found!");
- }
- //otherwise, it will display "not found".
- else
- {
- MessageBox.Show("Item not found!");
- }
- }
Add new comment
- 1425 views