ListBox stores several text items. It can interact with other controls, including Button controls. We use this control in Windows Forms. In the example program it interacts with two Buttons—through the Button Click event handler.
Drag and drop ListBox from toolbox on the window Form.
Code:
using System;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
publicpartialclassfrmListBox : Form
{
public frmListBox()
{
InitializeComponent();
}
privatevoid frmListBoLoad(object sender, EventArgs e)
{
// Add item in ListBox
listBox1.Items.Add("C#");
listBox1.Items.Add("J#");
listBox1.Items.Add("VB");
listBox1.Items.Add("Java");
}
privatevoid listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
// selected value will show in Label
label3.Text ="Selected Language is "+listBox1.SelectedItem.ToString();
}
}
}
Run The Project
When you select given option then SelectedIndexChanged event will fire and
selected Item will show in the Label.
ListBox Control Properties
Sorted: ListBox can be sorted through ListBox sorted Property
Example:
privatevoid frmListBoLoad(object sender, EventArgs e)
{
//sorted ListBox Item
listBox1.Sorted = true;
}
ListBox Item is sorted.
BackColor: ListBox Color can be changed through ListBox BackColor Property.
Example:
privatevoid frmListBoLoad(object sender, EventArgs e)
{
//change ListBox BackColor
listBox1.BackColor = Color.CadetBlue;
}
ListBox BackColor will be change when Application Run.
ForeColor: ListBox Fore color is changed through ListBox ForeColor Property.
Example:
privatevoid frmListBoLoad(object sender, EventArgs e)
{
//change ListBox ForeColor
listBox1.ForeColor = Color.Red;
}
Anonymous User
14-Mar-2019Vey nice article.
Samuel Fernandes
19-Jul-2017This article is truly nice.