In Windows Forms, ListBox control is used to show multiple elements in a list, from which a user can select one or more elements and the elements are generally displayed in multiple columns. In ListBox, you can set the height of the elements present in the ListBox using the ItemHeight Property of the ListBox. The maximum height of the ListBox item is 255 pixels. You can set this property in two different ways:
1. Design-Time: It is the easiest way to set the height of the elements present in the ListBox as shown in the following steps:
- Step 1: Create a windows form as shown in the below image:
Visual Studio -> File -> New -> Project -> WindowsFormApp

- Step 2: Drag the ListBox control from the ToolBox and drop it on the windows form. You are allowed to place a ListBox control anywhere on the windows form according to your need.

- Step 3: After drag and drop you will go to the properties of the ListBox control to set the height of the elements present in the ListBox.
Output:

2. Run-Time: It is a little bit trickier than the above method. In this method, you can set the height of the elements present in the ListBox control programmatically with the help of given syntax:
public virtual int ItemHeight { get; set; }
Here, the value of this property is of System.Int32 type. The height of the element is always considered in pixels. It will throw an ArgumentOutOfRangeException if the value of this property was set to less than 0 or more than 255 pixels. The following steps show how to set the height of the elements present in the ListBox dynamically:
- Step 1: Create a list box using the ListBox() constructor is provided by the ListBox class.
// Creating ListBox using ListBox class constructor ListBox lstbox = new ListBox();
- Step 2: After creating ListBox, set the ItemHeight Property of the ListBox provided by the ListBox class.
// Setting the height of the items lstbox.ItemHeight = 13;
- Step 3: And last add this ListBox control to the form using Add() method.
// Add this ListBox to the form this.Controls.Add(lstbox);
Example:
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;namespaceWindowsFormsApp28 {publicpartialclassForm1 : Form {publicForm1(){InitializeComponent();}privatevoidForm1_Load(objectsender, EventArgs e){// Creating and setting the// properties of the labelLabel lb =newLabel();lb.Location =newPoint(243, 80);lb.Text ="Select post";// Adding label control to the formthis.Controls.Add(lb);// Creating and setting the// properties of ListBoxListBox lstbox =newListBox();lstbox.Location =newPoint(246, 104);lstbox.ItemHeight = 13;lstbox.Items.Add("Intern");lstbox.Items.Add("Software Engineer");lstbox.Items.Add("Project Manager");lstbox.Items.Add("HR");// Adding listbox control to the formthis.Controls.Add(lstbox);}}}chevron_rightfilter_noneOutput:

Recommended Posts:
- C# | Remove all elements in a collection from a HashSet
- How to set the Font of the ListBox in C#?
- C# | How to get the Remaining Elements of the Tuple?
- C# | Get the number of elements actually contained in the ArrayList
- C# | Check if SortedSet and a specified collection share common elements
- C# | Check if HashSet and the specified collection contain the same elements
- C# | Check if a HashSet and a specified collection share common elements
- C# | Number of elements in HashSet
- C# | Get the number of elements contained in the Stack
- C# | Get the number of elements contained in the Queue
- C# | Remove all elements from the Hashtable
- C# | Remove all elements from a HashSet
- C# | Check if SortedSet and the specified collection contain the same elements
- C# | Removing all the elements from the List
- C# | Count the total number of elements in the List
- C# | Copying the elements of ArrayList to a new array
- C# | Get the number of elements in the SortedSet
- C# | Remove all elements from the SortedSet
- C# | Remove elements from a SortedSet that match the predicate
- C# | Remove elements from a HashSet with conditions defined by the predicate
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

