using System; using System.Drawing; using System.Windows.Forms; namespace TEPTUT { public partial class Form1 : Form { public Form1() { InitializeComponent(); listBox1.Items.Add("Pakistan"); listBox1.Items.Add("United States"); listBox1.Items.Add("United Kingdom"); listBox1.Items.Add("India"); } } }
using System; using System.Drawing; using System.Windows.Forms; namespace TEPTUT { public partial class Form1 : Form { public Form1() { InitializeComponent(); String var1 = "Lahore"; String var2 = "Multan"; String var3 = "Islamabad"; String var4 = "Karachi"; listBox1.Items.Add(var1); listBox1.Items.Add(var2); listBox1.Items.Add(var3); listBox1.Items.Add(var4); } } }
using System; using System.Drawing; using System.Windows.Forms; namespace TEPTUT { public partial class Form1 : Form { public Form1() { InitializeComponent(); String[] city = new String[] { "Lahore", "Multan", "Islamabad", "Karachi"}; for (int x = 0; x < city.Length; x++) { listBox1.Items.Add(city[x]); } } } }
using System; using System.Drawing; using System.Windows.Forms; namespace TEPTUT { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { listBox1.Items.Add(textBox1.Text); } } }
using System; using System.Drawing; using System.Windows.Forms; namespace TEPTUT { public partial class Form1 : Form { public Form1() { InitializeComponent(); listBox1.Items.Add("A"); listBox1.Items.Add("B"); listBox1.Items.Add("C"); listBox1.Items.Add("D"); } private void button1_Click(object sender, EventArgs e) { MessageBox.Show(listBox1.SelectedItem.ToString()); } } }
using System; using System.Drawing; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { listBox1.Items.Add("Sunday"); listBox1.Items.Add("Monday"); listBox1.Items.Add("Tuesday"); listBox1.Items.Add("Wednesday"); listBox1.Items.Add("Thursday"); listBox1.Items.Add("Friday"); listBox1.Items.Add("Saturday"); listBox1.SelectionMode = SelectionMode.MultiSimple; } private void button1_Click(object sender, EventArgs e) { foreach (Object obj in listBox1.SelectedItems ) { MessageBox.Show(obj.ToString ()); } } } }
using System; using System.Drawing; using System.Windows.Forms; namespace TEPTUT { public partial class Form1 : Form { public Form1() { InitializeComponent(); listBox1.Items.Add("A"); listBox1.Items.Add("B"); listBox1.Items.Add("C"); listBox1.Items.Add("D"); } private void button1_Click(object sender, EventArgs e) { listBox1.Items.Clear(); } } }
using System; using System.Drawing; using System.Windows.Forms; namespace TEPTUT { public partial class Form1 : Form { public Form1() { InitializeComponent(); listBox1.Items.Add("A"); listBox1.Items.Add("B"); listBox1.Items.Add("C"); listBox1.Items.Add("D"); } private void button1_Click(object sender, EventArgs e) { listBox1.Items.RemoveAt(0); } } }
using System; using System.Drawing; using System.Windows.Forms; namespace TEPTUT { public partial class Form1 : Form { public Form1() { InitializeComponent(); listBox1.Items.Add("A"); listBox1.Items.Add("B"); listBox1.Items.Add("C"); listBox1.Items.Add("D"); } private void button1_Click(object sender, EventArgs e) { listBox1.Items.Remove("A"); } } }
using System; using System.Drawing; using System.Windows.Forms; namespace TEPTUT { public partial class Form1 : Form { public Form1() { InitializeComponent(); listBox1.Items.Add("A"); listBox1.Items.Add("B"); listBox1.Items.Add("C"); listBox1.Items.Add("D"); } private void button1_Click(object sender, EventArgs e) { listBox1.Font = new Font(Font.FontFamily, 12); } } }
using System; using System.Drawing; using System.Windows.Forms; namespace TEPTUT { public partial class Form1 : Form { public Form1() { InitializeComponent(); listBox1.Items.Add("A"); listBox1.Items.Add("B"); listBox1.Items.Add("C"); listBox1.Items.Add("D"); listBox1.BackColor = Color.LightCyan; } } }
using System; using System.Drawing; using System.Windows.Forms; namespace TEPTUT { public partial class Form1 : Form { public Form1() { InitializeComponent(); listBox1.Items.Add("The"); listBox1.Items.Add("Engineering"); listBox1.Items.Add("Projects"); listBox1.Font = new Font(Font.FontFamily, 15); listBox1.BackColor = Color.Black; listBox1.ForeColor = Color.White; } } }
There are several more events which you can try to make list box more interactive. These events are used to give innovative concepts.
using System; using System.Drawing; using System.Windows.Forms; namespace TEPTUT { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void listBox1_Click(object sender, EventArgs e) { MessageBox.Show("Warn ! Click is disable.."); } } }
This event occurs when user will click twice on the Listbox. Supposed you are wanted to perform any action when user will double click on the ListBox, then we will use this event. In the following code, we are changing the back color of ListBox when user will click twice.
using System; using System.Drawing; using System.Windows.Forms; namespace TEPTUT { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void listBox1_DoubleClick(object sender, EventArgs e) { listBox1.BackColor = Color.Black; } } }
To demonstrate double click event, we changed the color. You can code any functionality according to your needs.
This event occurs when user will hover mouse cursor on the Listbox. In the following code, we have changed the foreColor on mouse hover. When the mouse cursor will hover the color of font will be changed.
using System; using System.Drawing; using System.Windows.Forms; namespace TEPTUT { public partial class Form1 : Form { public Form1() { InitializeComponent(); listBox1.Items.Add("The Engineering Projects"); } private void listBox1_MouseHover(object sender, EventArgs e) { listBox1.ForeColor = Color.Bisque; } } }
This event occurs when user will remove the cursor from the Listbox. In the above code, we have changed the forecolor when a user hovers the mouse cursor. In the below code, we will change the forecolor again to black when a user removes mouse cursor.
using System; using System.Drawing; using System.Windows.Forms; namespace TEPTUT { public partial class Form1 : Form { public Form1() { InitializeComponent(); listBox1.Items.Add("The Engineering Projects"); } private void listBox1_MouseHover(object sender, EventArgs e) { listBox1.ForeColor = Color.Bisque; } private void listBox1_MouseLeave(object sender, EventArgs e) { listBox1.ForeColor = Color.Black; } } }
This event occurs when user will change the background color of the Listbox. In the following code, we are going to change the color of background when user will hover the mouse on ListBox. When color will change, BackColorChanged event will occur. In which we will show popup message.
using System; using System.Drawing; using System.Windows.Forms; namespace TEPTUT { public partial class Form1 : Form { public Form1() { InitializeComponent(); listBox1.Items.Add("The Engineering Projects"); } private void listBox1_MouseHover(object sender, EventArgs e) { listBox1.BackColor = Color.Bisque; } private void listBox1_BackColorChanged(object sender, EventArgs e) { MessageBox.Show("BackGround Color is changed !!"); } } }
This event occurs when user will change the foreground color of the Listbox. In the following code, we will be used mouse hover event to changed the foreColor and ForeColorChanged Event also. The forecolorchanged event will raise the message popup when a color of the foreground is getting changed.
using System; using System.Drawing; using System.Windows.Forms; namespace TEPTUT { public partial class Form1 : Form { public Form1() { InitializeComponent(); listBox1.Items.Add("The Engineering Projects"); } private void listBox1_MouseHover(object sender, EventArgs e) { listBox1.ForeColor = Color.Bisque; } private void listBox1_ForeColorChanged(object sender, EventArgs e) { MessageBox.Show("ForeGround Color is changed !!"); } } }
This event occurs when user will select any item in the Listbox. In the below code I have taken two list box. When user will select any value from the first list box then according to that value listbox2 will show it's valued.
using System; using System.Drawing; using System.Windows.Forms; namespace TEPTUT { public partial class Form1 : Form { public Form1() { InitializeComponent(); listBox1.Items.Add("The Engineering Projects"); listBox1.Items.Add("C# Tutorials"); } private void listBox1_SelectedIndexChanged(object sender, EventArgs e) { if (listBox1.SelectedIndex == 0) { listBox2.Items.Clear(); listBox2.Items.Add("Arduino Projects"); listBox2.Items.Add("Proteus Projects"); listBox2.Items.Add("Matlab Projects"); } else if (listBox1.SelectedIndex == 1) { listBox2.Items.Clear(); listBox2.Items.Add("C# Label"); listBox2.Items.Add("C# TextBox"); listBox2.Items.Add("C# ComboBox"); } } } }
If you wanted to test the above code, then place two list boxes and copied the above code.
We have tried to give basics to major concepts of C# ListBox. All the above codes are working fine and added after testing. If you will cover all the above codes then you will become able to play with ListBoxes. ListBoxes play very important roles in Point Of Sale applications. Thanks for reading our tutorial, moreover you can watch ListBox video.