articles

Home / DeveloperSection / Articles / ListBox Control in C#.Net

ListBox Control in C#.Net

Anonymous User15349 24-Jan-2011

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.

ListBox Control in C#.Net

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();
        }
    }
}


ListBox Control in C#.Net

Run The Project

 

ListBox Control in C#.Net

 

When you select given option then SelectedIndexChanged event will fire and
selected Item will show in the Label.

 

ListBox Control in C#.Net

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 Control in C#.Net

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 Control in C#.Net

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;
}


ListBox Control in C#.Net

 

 

 


Updated 04-Mar-2020
I am a content writter !

Leave Comment

Comments

Liked By