C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Next: Right-click on the Form in the Solution Explorer, and select "View Code." You will see a C# code file.
Here: Create a new List at the class level. This is what we will use for binding to the ListBox.
Info: We see a List of strings at the class level. In the constructor, we add 3 elements to the List.
DataSource: At the final point in the constructor, we assign the DataSource from the listBox1 to the new List.
DataSourceExample that creates List: C#
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;
}
}
}
Click event handler: C#
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;
}
Info: We can add items to the ListBox by clicking the top button, and then remove the selected item by clicking the bottom button.
Example that uses DataSource: C#
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;
}
}
}
Button 1: Button1 is the Add button, so it always sets the Enabled property of Add to true.
Button 2: Button2 is the Remove button, so it is disabled when there are no items in the ListBox.
ButtonExample that sets Enabled property: C#
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;
}
}
SelectionMode: You can use this to eliminate the selection by setting it to None. This is useful for read-only displays.
IntegralHeight: The control can show partial rows. If the size of your Form cannot fit six elements, it will display only five.
HorizontalScrollbar: This property lets you specify if you want to be able to scroll horizontally on long items.
Tooltip on toolTip1: This property allows you to specify the tool tip. Note that you must have a toolTip1 in your Form first.
Anchor: You should always Anchor controls like the ListBox. Even better, use the TableLayoutPanel control and Anchor inside of it.
TableLayoutPanelTip: You can do this by modifying the Items property through the appropriate dialog box.