How to sort elements in the ListBox in C#? - GeeksforGeeks

17 Jul, 2019
In Windows Forms, ListBox control is used to show multiple elements in a list, from which a user can select one or more elements and the elements are generally displayed in multiple columns. In ListBox, you are allowed to sort the elements present in the ListBox using Sorted Property of the ListBox. This property always sorts the string type elements in the alphabetical order and integer type elements in ascending order. The default value of this property is false. You can set this property in two different ways:
1. Design-Time: It is the easiest way to sort the elements present in the ListBox as shown in the following steps:
Step 1: Create a windows form as shown in the below image:Visual Studio -> File -> New -> Project -> WindowsFormApp
Step 2: Drag the ListBox control from the ToolBox and drop it on the windows form. You are allowed to place a ListBox control anywhere on the windows form according to your need.
Step 3: After drag and drop you will go to the properties of the ListBox control to sort the elements of the ListBox.Output:
Output:
2. Run-Time: It is a little bit trickier than the above method. In this method, you can sort the elements present in the ListBox control programmatically with the help of given syntax:
public bool Sorted { get; set; }
Here, the value of this property is of System.Boolean type. If you want to sort the elements present in the Listbox, then set the value of this property to true. Otherwise, false. The following steps show how to set the Sorted property of the ListBox dynamically:
Step 1: Create a list box using the ListBox() constructor is provided by the ListBox class.// Creating ListBox using ListBox class constructor
ListBox lstbox = new ListBox();

// Creating ListBox using ListBox class constructor
ListBox lstbox = new ListBox();

Step 2: After creating ListBox, set the Sorted property of the ListBox provided by the ListBox class.// Sorting the elements of the listbox
lstbox.Sorted = true;

// Sorting the elements of the listbox
lstbox.Sorted = true;

Step 3: And last add this ListBox control to the form using Add() method.// Add this ListBox to the form
this.Controls.Add(lstbox);
Example:using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;  namespace WindowsFormsApp28 {  public partial class Form1 : Form {      public Form1()    {        InitializeComponent();    }      private void Form1_Load(object sender, EventArgs e)    {        // Creating and setting the         // properties of the label        Label lb = new Label();        lb.Location = new Point(243, 80);        lb.Text = "Select post";          // Adding label control to the form        this.Controls.Add(lb);          // Creating and setting the        // properties of ListBox        ListBox lstbox = new ListBox();        lstbox.Location = new Point(246, 104);        lstbox.Sorted = true;        lstbox.Items.Add("Intern");        lstbox.Items.Add("Software Engineer");        lstbox.Items.Add("Project Manager");        lstbox.Items.Add("HR");          // Adding listbox control to the form        this.Controls.Add(lstbox);    }}}Output:Before sorting:After sorting:
// Add this ListBox to the form
this.Controls.Add(lstbox);

Example:
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;  namespace WindowsFormsApp28 {  public partial class Form1 : Form {      public Form1()    {        InitializeComponent();    }      private void Form1_Load(object sender, EventArgs e)    {        // Creating and setting the         // properties of the label        Label lb = new Label();        lb.Location = new Point(243, 80);        lb.Text = "Select post";          // Adding label control to the form        this.Controls.Add(lb);          // Creating and setting the        // properties of ListBox        ListBox lstbox = new ListBox();        lstbox.Location = new Point(246, 104);        lstbox.Sorted = true;        lstbox.Items.Add("Intern");        lstbox.Items.Add("Software Engineer");        lstbox.Items.Add("Project Manager");        lstbox.Items.Add("HR");          // Adding listbox control to the form        this.Controls.Add(lstbox);    }}}
Output:
Before sorting:
After sorting:
C#
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
C# | Method Overriding
C# Dictionary with examples
Difference between Ref and Out keywords in C#
C# | Delegates
Top 50 C# Interview Questions & Answers
Introduction to .NET Framework
C# | Constructors
Extension Method in C#
C# | Class and Object
C# | Abstract Classes