Как установить размер ListBox в C #?
В Windows Forms элемент управления ListBox используется для отображения нескольких элементов в списке, из которого пользователь может выбрать один или несколько элементов, и элементы обычно отображаются в нескольких столбцах. В ListBox вам разрешено устанавливать высоту и ширину ListBox в пикселях, используя свойство Size ListBox, что делает ваш ListBox более привлекательным. Вы можете установить это свойство двумя разными способами:
1. Design-Time: It is the easiest way to set the size of 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 size of the ListBox.

Output:

2. RunTime: It is a little bit trickier than the above method. In this method, you can set the size of the ListBox control programmatically with the help of given syntax:
public System.Drawing.Size Size { get; set; }Here, Size indicates the height and width of the ListBox in pixels. The following steps show how to set the size of 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 mylist = new ListBox();
- Step 2: After creating ListBox, set the Size property of the ListBox provided by the ListBox class.
// Setting the size of the ListBox mylist.Size = new Size(120, 95);
- Step 3: And last add this ListBox control to the form using Add() method.
// Add this ListBox to the form this.Controls.Add(mylist);
Example:
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;namespaceWindowsFormsApp25 {publicpartialclassForm1 : Form {publicForm1(){InitializeComponent();}privatevoidForm1_Load(objectsender, EventArgs e){// Creating and setting// the properties of ListBoxListBox mylist =newListBox();mylist.Location =newPoint(287, 109);mylist.Size =newSize(120, 95);mylist.ForeColor = Color.Purple;mylist.Items.Add(123);mylist.Items.Add(456);mylist.Items.Add(789);// Adding ListBox control to the formthis.Controls.Add(mylist);}}}Output:
