ListBox
This stores several text items. It can interact with other controls. In the example program it interacts with 2 Buttons—through the Button
Click event handler.
Please create a new Windows Forms C# project, and then open the Toolbox and double-click on the ListBox
item. This will insert a new ListBox
into your Windows Forms designer.
Here we create a new List
at the class
level—this will be used as a data source. In the Form1
constructor, we add 3 elements to the List
.
DataSource
from the listBox1
to the new List
.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 WindowsFormsApplication11 { public partial class Form1 : Form { List<string> _items = new List<string>(); // <-- Add this public Form1() { InitializeComponent(); _items.Add("One"); // <-- Add these _items.Add("Two"); _items.Add("Three"); listBox1.DataSource = _items; } } }
Here we double-click on each button in the Designer. You will be taken to the C# code view and a new event handler will have been inserted.
button1_Click
and button2_Click
), we manipulate the List
and refresh the ListBox
.private void button1_Click(object sender, EventArgs e) { // The Add button was clicked. _items.Add("New item " + DateTime.Now.Second); // <-- Any string you want // Change the DataSource. listBox1.DataSource = null; listBox1.DataSource = _items; } private void button2_Click(object sender, EventArgs e) { // The Remove button was clicked. int selectedIndex = listBox1.SelectedIndex; try { // Remove the item in the List. _items.RemoveAt(selectedIndex); } catch { } listBox1.DataSource = null; listBox1.DataSource = _items; }
We see the code for the Form.cs
file, which acts on the ListBox
and 2 Buttons in the Designer. We add and remove items to the ListBox
with the buttons.
ListBox
by clicking the top button, and then remove the selected item by clicking the bottom button.using System; using System.Collections.Generic; using System.Windows.Forms; namespace WindowsFormsApplication11 { public partial class Form1 : Form { List<string> _items = new List<string>(); public Form1() { InitializeComponent(); _items.Add("One"); _items.Add("Two"); _items.Add("Three"); listBox1.DataSource = _items; } private void button1_Click(object sender, EventArgs e) { // The Add button was clicked. _items.Add("New item " + DateTime.Now.Second); // Change the DataSource. listBox1.DataSource = null; listBox1.DataSource = _items; } private void button2_Click(object sender, EventArgs e) { // The Remove button was clicked. int selectedIndex = listBox1.SelectedIndex; try { // Remove the item in the List. _items.RemoveAt(selectedIndex); } catch { } listBox1.DataSource = null; listBox1.DataSource = _items; } } }
Enabled
You can change the Enabled
state of your Add and Remove
buttons by setting the Enabled
property. This example demonstrates setting Enabled
.
Enabled
property of Add to true.Remove
button, so it is disabled when there are no items in the ListBox
.private void button1_Click(object sender, EventArgs e) { // The Add button was clicked. // ... button2.Enabled = true; } private void button2_Click(object sender, EventArgs e) { // The Remove button was clicked. // .... if (listBox1.Items.Count == 0) { button2.Enabled = false; } }
In Visual Studio, it is possible to type the items you want to be present in the ListBox
using the designer. You can do this by modifying the Items property.
We added a ListBox
and had other controls interact with it. We should bind its DataSource
. Adding items manually is harder to do correctly and is more complex.