Listbox in C#

WBOY
Release: 2024-09-03 15:28:17
Original
962 people have browsed it

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

  1.  ListBox.ObjectCollection: This collection class holds all the elements of the ListBox control.
  2.  ListBox.SelectedObjectCollection: This collection class holds the collection of selected items in the ListBox control.
  3.  ListBox.SelectedIndexCollection: This collection class holds the collection of selected indexes; these elements are a subset of the indexes of the ListBox.ObjectCollection and this specifically selected indexes in the ListBox control.

Types of List Boxes in C#?

  1. Single Selected ListBox: ListBox can be able to select only a single element from the list.
  2. Multi Selected ListBox: ListBox can be able to select multiple elements from the list.

Prerequisites for ListBox in C#:

  • .Net libraries must be installed on your PC
  • Visual Studio set up

How to Create the ListBox in C#?

ListBox can be created in 2 ways:

  • Design-Time
  • Run-Time

1. Design-Time

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

Listbox in C#

Select =>Windows Form Application, then

See the below image for a better understanding of the project structure:

Listbox in C#

Name the project and click OK then you will get Form1.cs(Design) tab like below

Listbox in C#

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.

Listbox in C#

Listbox in C#

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.

Listbox in C#

Output:

Listbox in C#

2. Run-Time

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();
Copy after login

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;
Copy after login

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");
Copy after login

Step 4: Add this ListBox to the Form.

Syntax:

this.Controls.Add(listBox);
Copy after login

Examples of Listbox in C#

Here are the following examples mentioned below

Example #1 – Creating ListBox and adding elements

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)
{
}
}
}
Copy after login

Output:

Listbox in C#

Example #2 – User enters value added it to the list box by clicking the 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();
}
}
}
}
Copy after login

Output:

Before entering a value:

Listbox in C#

Without entering any value try to click the save button:

Listbox in C#

After entering a value:

Listbox in C#

After entering a value and clicking the save button:

Listbox in C#

Example #3 – Delete, Change the font of List Box values

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;
}
}
}
}
Copy after login

Output:

After adding 3 names:

Listbox in C#

Deleting selected element:

Listbox in C#

Listbox in C#

Change the font of the values:

Listbox in C#

Listbox in C#

Listbox in C#

Listbox in C#

Conclusion

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!

Related labels:
source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template