Get the Most Repeated Item in the ListBox

In this C# tutorial, I will teach you how to create a program that will get and display the most repeated item in the ListBox. Now, let's start this tutorial! 1. Let's start with creating a Windows Form Application for this tutorial by following the following steps in Microsoft Visual Studio: Go to File, click New Project, and choose Windows Application. 2. Next, add one TextBox named TextBox1 that will serve as your input, two buttons named Button1 for adding the inputted text in the ListBox and Button2 for getting and displaying the most repeated text in a ListBox, and one listbox named ListBox1 for displaying the list of inputted text/items in the textbox. You must design your layout like this: design 3. For Button1 as adding item to the listbox1. Put this code below.
  1.                 public void Button1_Click(System.Object sender, System.EventArgs e)
  2.                 {
  3.                 // The items inputted in TextBox1 will be added in ListBox1
  4.                         ListBox1.Items.Add(TextBox1.Text);
  5.                 //Clear the textbox
  6.                         TextBox1.Clear();
  7.                 }
4. For Button2 as getting and displaying the most repeated item and the number of times it occurs in the listbox, put this code below.
  1.                 public void Button2_Click(System.Object sender, System.EventArgs e)
  2.                 {
  3.                 // initialize global variables
  4.                         string ob1 = default(string);
  5.                         string ob2 = default(string);
  6.                         string ob3 = default(string);
  7.                         int num1 = default(int);
  8.                         int num2 = default(int);
  9.                 // create a foreach loop with a parameter of tempLoopVar_ob1 as String as Items of ListBox
  10.                        
  11.                         foreach (string tempLoopVar_ob1 in ListBox1.Items)
  12.                         {
  13.                 // ob1 variable will hold the list of items in ListBox1
  14.                                 ob1 = tempLoopVar_ob1;
  15.                 // we used again a foreach loop with a parameter of tempLoopVar_ob2 as String as Items of ListBox
  16.                 // nested loop
  17.                                 foreach (string tempLoopVar_ob2 in ListBox1.Items)
  18.                                 {
  19.                 // ob2 variable will hold the list of items in ListBox1
  20.                                         ob2 = tempLoopVar_ob2;
  21.                 // if the two variables hold the same item in the listbox, it will increment by 1
  22.                                         if (ob1 == ob2)
  23.                                         {
  24.                                                 num1++;
  25.                                         }
  26.                                 }
  27.                 // if variable num1 will be greater than num2, then num2 will be equal to num1 as ob3 variable will hold ob1
  28.                                 if (num1 > num2)
  29.                                 {
  30.                                         num2 = num1;
  31.                                         ob3 = ob1;
  32.                                 }
  33.                                 num1 = 0;
  34.                         }
  35.                 // Display the most repeated item in the listbox by variable ob3
  36.                 // Display also the number of times it was repeated
  37.                         MessageBox.Show(ob3 + " is repeated " + num2.ToString() + " times");
  38.                 }
Output: 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

Add new comment