Home > Article > Backend Development > Listbox in C#
ListBox in C# is defined as adding a list of elements to the ListBox to operate on single or multiple elements. The difference between the drop-down box and the list box is drop-down box can select only one element at a time, but in the case of the list box, we can select single or multiple elements at a time. The ListBox provides different types of methods, properties, and events. This ListBox is specified under System. Windows.Forms package (namespace).
The ListBox class again contains 3 different types of collections in C#. They are
Prerequisites for ListBox in C#:
ListBox can be created in 2 ways:
It is very easy to create without any code initially. Steps to create a project
Step 1: Open Visual Studio
Click on File=>New=>Project
Select =>Windows Form Application, then
See the below image for a better understanding of the project structure:
Name the project and click OK then you will get Form1.cs(Design) tab like below
Step 2: Left side of the visual studio or From View, choose Toolbox; next, drag and drop then the required elements onto the Form1.cs(Design) as shown in the above image.
Step 3: After drag and drop select the properties from the right side of the Visual Studio and give some name to the Text property. This is used to write code in the 2nd method Run-Time.
Output:
This is not directly doing it as per the above method. We have written some programs to create ListBox. This is very simple; first, drag and drop all the required elements like ListBox, Label, TextField, Button, etc. If you double click on any of the dropped elements, we get some C# code that element action methods, we have to write our logic to what we want to do with those elements. Steps to create Run-Time project code to create ListBox
Step 1: Create ListBox control by using ListBox() constructor.
Syntax:
ListBox listBox = new ListBox();
Step 2: After creating the ListBox property, if we want to set the properties of the ListBox like Font, Font.Size, Color to elements, etc.
Syntax:
listBox.Location = new Point(200, 100); listBox.Size = new Size(100, 90); listBox.ForeColor = Color.Red;
Step 3: Add the elements to the ListBox.
Syntax:
listBox.Items.Add("A"); listBox.Items.Add("B"); listBox.Items.Add("C"); listBox.Items.Add("D");
Step 4: Add this ListBox to the Form.
Syntax:
this.Controls.Add(listBox);
Here are the following examples mentioned below
Code:
//importing C# required libraries using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; //namespace is project name namespace WindowsFormsApplication26 { //creating class extends from Form class public partial class Form1 : Form { //constrcutor public Form1() { //initializing components InitializeComponent(); //Creating list box and add some properties and values to the List Box listBox2.ForeColor = Color.Red; listBox2.Items.Add("Java"); listBox2.Items.Add("Python"); listBox2.Items.Add("C++"); listBox2.Items.Add("C"); listBox2.Items.Add("C#"); listBox2.Items.Add("Spring"); listBox2.Items.Add("JavaFX"); listBox2.SelectionMode = SelectionMode.MultiSimple; } //method for selectedIndex change operation private void listBox2_SelectedIndexChanged(object sender, EventArgs e) { } } }
Output:
Code:
//importing C# required libraries using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; //namespace is project name namespace WindowsFormsApp25 { //creating class extends from Form class public partial class Form1 : Form { //constrcutor public Form1() { //initializing components InitializeComponent(); } //saving the enter values into List box private void buttonSave_Click(object sender, EventArgs e) { //If user enter any values then if block executes if (this.textBoxName.Text != "") { NameList.Items.Add(this.textBoxName.Text); this.textBoxName.Focus(); this.textBoxName.Clear(); } //If user did not enter any values then else block executes else { MessageBox.Show("Please enter a name to add..","Error",MessageBoxButtons.OK,MessageBoxIcon.Information); this.textBoxName.Focus(); } } } }
Output:
Before entering a value:
Without entering any value try to click the save button:
After entering a value:
After entering a value and clicking the save button:
Code:
//importing C# required libraries using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; //namespace is project name namespace WindowsFormsApp25 { //creating class extends from Form class public partial class Form1 : Form { //constrcutor public Form1() { //initializing components InitializeComponent(); } //saving the enter values into List box private void buttonSave_Click(object sender, EventArgs e) { //If user enter any values then if block executes if (this.textBoxName.Text != "") { NameList.Items.Add(this.textBoxName.Text); this.textBoxName.Focus(); this.textBoxName.Clear(); } //If user did not enter any values then else block executes else { MessageBox.Show("Please enter a name to add..","Error",MessageBoxButtons.OK,MessageBoxIcon.Information); this.textBoxName.Focus(); } } //Removing the selected elements private void button2_Click(object sender, EventArgs e) { if (this.NameList.SelectedIndex >= 0) { this.NameList.Items.RemoveAt(this.NameList.SelectedIndex); } } //Setting List box selected values font private void button3_Click(object sender, EventArgs e) { if (fontDialog1.ShowDialog() == DialogResult.OK) { NameList.Font = fontDialog1.Font; } } } }
Output:
After adding 3 names:
Deleting selected element:
Change the font of the values:
C# List box is used to add multiple elements to perform any specific operation. List Boxes are used to select a single value or multiple values at a time. In C# List Box can be created using Design-Time and Run-Time methods.
The above is the detailed content of Listbox in C#. For more information, please follow other related articles on the PHP Chinese website!