Home > DeveloperSection > Beginner > ListBox Control in C#.Net

ListBox Control in C#.Net


C# C# 
Ratings:
0 Comment(s)
 8436  View(s)
Rate this:

ListBox Control in C#.Net


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

{

    public partial class frmListBox : Form

    {

        public frmListBox()

        {

            InitializeComponent();

        }

        private void frmListBox_Load(object sender, EventArgs e)

        {

            // Add item in ListBox

            listBox1.Items.Add("C#");

            listBox1.Items.Add("J#");

            listBox1.Items.Add("VB");

            listBox1.Items.Add("Java");

        }

    private void 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:

private void frmListBox_Load(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:

private void frmListBox_Load(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:

private void frmListBox_Load(object sender, EventArgs e)

{

      //change ListBox ForeColor

      listBox1.ForeColor = Color.Red;

}

ListBox Control in C#.Net

 

 

   

 

 

 


Don't want to miss updates? Please click the below button!

Follow MindStick