Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Creating Listbox in C#
3.
Creating a Listbox in C# at design time
4.
Creating a Listbox in C# at run time
5.
Constructors of ListBox in C#
6.
Properties of ListBox in C#
7.
FAQs
8.
Key Takeaways
Last Updated: Mar 27, 2024

Listbox in C#

Author Gaurav Joshi
1 upvote
Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

Introduction

Listbox in C# provides an interface for the user to display multiple list items. The user could select single or multiple elements from the list by clicking elements. These elements are generally displayed in multiple columns instead of a straight vertical list. These multiple columns could have images and other controls as well. In this article, we will create a Listbox in C# both at design time and run time.

Recommended topics, Palindrome in C#, singleton design pattern in c# and Ienumerable vs Iqueryable

Creating Listbox in C#

One can create a Listbox control in C# in two different methods. One method is to use a form designer and create Listbox control during the design phase. Another method is using the Listbox class to create Listbox control at run time.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Creating a Listbox in C# at design time

  1. Create a new project in Visual Studio Code.
  2. Select WindowsFormApp and Create new.
  3. New Form will appear on the screen like this.
  4. Select the list box control from the toolbox.
  5. Drag and Drop the list box into the form.
  6. After Drag and Drop, go to properties and modify based on your use.
  7. Add items like string.

Creating a Listbox in C# at run time

Creating a Listbox at run time is a little more complicated process than what we saw above. Here the steps are defined using the Listbox class, and each item is added using the Add() method. We also need to set up the properties defined in the list box class.

Code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
  
namespace WindowsFormsApp25 {
  
public partial class Form1: Form {
  
    public Form1()
    {
        InitializeComponent();
    }
  
    private void Form1_Load(object sender, EventArgs e)
    {
  
        // Create ListBox from class ListBox
        ListBox  list = new ListBox();

        // Set the properties of list
        list.Location = new Point(297, 119);
        list.Size = new Size(150, 115);
        list.ForeColor = Color.Black;

        // After Creating a list class add list items 
        list.Items.Add(“Item 1”);
        list.Items.Add(“Item 2”);
        list.Items.Add(“Item 3”);
        list.Items.Add(“Item 4”);
        list.Items.Add(“Item 5”);
  
        // Adding ListBox control to the form
        this.Controls.Add(list);
    }
  }
}

Output:-

Constructors of ListBox in C#

Constructor Description
ListBox() ListBox() helps us to create a new instance of the ListBox class.

Properties of ListBox in C#

Property Description
AutoSize AutoSize Property sets or gets a value to indicate the control resizes based on its Contents.
BackColor BackColor helps us set the background colour for Listbox control
BorderStyle BorderStyle property defines the border style property of Listbox control.
Font Font property helps us set the font of the text displayed in Listbox control.
ForeColor The ForeColor property helps us get or set the colour of the control's foreground.
Height Height property helps us get or set the height of the Listbox control.
Location Location property helps us set the coordinates for the upper-left corner of the ListBox control relative to the upper-left corner of its form.
Name Name property gets or sets the name of the Listbox control.
Size Size property gets us the height and width of the Listbox control.
Width The width property helps us get or set the control's width.
ColumnWidth ColumnWidth sets the width of the column in a multicolumn ListBox.
ItemHeight ItemHeight sets the height of an item in the ListBox.
Items Items property is used to get the items of the ListBox
SelectedIndex The SelectedIndex property helps us get the index of the currently selected item in a ListBox.
SelectedItem The SelectedItem property of Listbox helps us get the currently selected item of the list.
TopIndex TopIndex property helps us get the index of the top element or set the index to the top element of the Listbox control.
Sorted This property tells us whether the item in the Listbox is sorted alphabetically or not.

FAQs

  1. How to create a new ListBox in C#?
    We could develop Listbox in C# during run time and design time. Creating a ListBox in design time is much simpler compared to run-time. In run time, we use the Listbox class to create an instance of Listbox, and during the design phase, we drag and drop the  Listbox in the toolbar and add items.
     
  2. What are the uses of Listbox in C#?
    ListBox helps us by providing a user interface to display the list of items. These items could be selected single or multiple times. List Box in C# could have multiple columns, and these columns can contain controls and images.
     
  3. How to get the index of selected items in Listbox in C#?
    SelectedIndex property helps us to get the index of the currently selected item in a ListBox

Key Takeaways

In this article, we have discussed Listbox in C#. We have briefly explained how to create a Listbox control in C#. We have also briefly explained the properties and constructors in Listbox.

I hope this article must have helped you improve your learning in C#. To discover more about such content and practice some quality questions, please visit our Guided Path in  Coding Ninjas Studio.To become more confident in data structures and algorithms, try out DS and Algo courses. Till then, all the best for your future adventure and Happy Coding.
 

Live masterclass