Reorder Items in ListBox using C#

Today in C#, I will teach you how to create a program that reorder items in 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. 2. Next, add one TextBox named Texbox1 which serves as your input items in the Listbox. Create two ListBox named ListBox1 for your storage of input and ListBox2 for displaying the order list. Add also two Buttons named Button2 for adding an inputted item in the ListBox1 and to reorder the list of items in your ListBox1 and will be displayed in TextBox2. You must design your interface like this: design 3. Now put this code in your Button2 for adding an item in the ListBox.
  1.                 public void Button2_Click(System.Object sender, System.EventArgs e)
  2.                 {
  3.                         ListBox1.Items.Add(TextBox1.Text);
  4.                         TextBox1.Clear();
  5.                 }
For Button2, it is the button used to add items from your inputted TextBox and then display the items in the ListBox1. 4. Put this code in your Button1 for reordering an item in the ListBox.
  1.                 public void Button1_Click(System.Object sender, System.EventArgs e)
  2.                 {
  3.             ListBox2.Items.Clear();
  4.                         int tempNum = default(int);
  5.                         string tempString = default(string);
  6.                         do
  7.                         {
  8.                                 tempNum = ListBox1.Items.Count - 1;
  9.                                 tempString = (string) (ListBox1.Items[tempNum]);
  10.                                 ListBox2.Items.Add(tempString);
  11.                                 ListBox1.Items.RemoveAt(tempNum);
  12.                         } while (!(tempNum == 0));
  13.                 }
Button1 is used for reordering the list of items in the Listbox1 and then displayed in ListBox2. If items in the Listbox1 is ascending, then the output in ListBox2 is in descending order. Otherwise, if items in the Listbox1 is descending, then the output in ListBox2 is in ascending order. We have initialized variable tempNum as Integer and variable tempString as String. Then we have created a Do until loop. Inside this loop is that the tempNum will hold the value of an index that the number of items in indexes will be minus 1 in ListBox1. Then the tempString will have this indexes of tempNum. Then the value now of tempString will be added to ListBox2 and all the items in ListBox1 will be removed. Output: 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: 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