Listbox Control in C# - C# ListBox Control
Listbox Control in C# - C# ListBox Control
- In C#, the List Box control provides us a user interface that will display the List of the items.
- From there, the users can select one or more items from the List.
- We can use the List Box to show the multiple columns, and these columns can contain images and other controls.
List Box Control Creation in C#
- We will follow the two approaches in Windows Form, for creating the list box.
- To create the List Box control, either we can use the List Box class for creating control or we can use the Forms Designer at the time of the designing at the run time.
Dynamically approach to create the List Box
- In Windows form list box class shows the List Box control and for the runtime execution to create the List Box, firstly, we will create the instance of the List Box class and set its properties and will add the List Box object to the Form Control.
Step 1
- We will create the instance of the List Box class, to create the Dynamic List Box in the first.
- To create the object of the List Box, we will write the below code:
Step 2
- We will set the properties of the List Box control, in the next step.
- In the property we will set the Location, width, height, background color, foreground color, name and font properties of the List Box, as shown below:
Step 3
- If the properties are set with the List Box control, in the next step we will add the List Box to the Form.
- For this we will use the Form.Controls.Add method which will add the List Box control to the Form control and will display it on the Form according to their location, and size.
Sample Code
Output
Creating an Application in C# using the List Box
Step 1
- First, we will create a new project for the creation of the project.
Step 2
- After this, a new window will display, where we select the Visual C#->Windows Form Application->Name of the project(Add Remove Creation) and click ok.
Step 3
- After this, a Form will create as shown in the below screenshot:
Step 4
- We will Drag and Drop the List Box from the toolbox, in the next.
- Here we are going to take two List Box(ListBox1 and ListBox2) and Five buttons(Add, Remove, Add All, Remove All and Finalize).
Step 5
- We will change the text and name of the button by right-clicking on the button->choose properties->change the text name as shown.
Step 6
- After clicking on the properties, the property window will open where we will change the name of the button and the text on the button as shown.
Step 7
- After this, we are going to show the data after clicking on the finalize button in the Grid View. For this, we will click on the Data->Grid View.
How to add the data in the listbox1 and import the data in ListBox
We are going to show that the listbox1 containing the data
- After clicking on the Add button, data will be added to the listbox2 and gets deleted from the listbox1.
Remove the data from the List Box
- Before removing the data, the whole data is in the listbox2 and the listbox1 does not contain any value. The output will look like as shown below.
- After clicking on the remove button, data is going to be deleted from the list box 2 and added to the listbox1, as shown.
Adding the whole data in one go in the List Box
Output
- After writing this code, the Add All button will work, as shown;
Remove all the data from the List Box
- Here, we will remove all the data from the listbox2 and add all the data in the listbox1.
- To remove all the data from the List box, we will write the following code.
Output
Showing the data of the List Box in the Grid View
- Here, we will use Grid View to show all the data of the ListBox2 after clicking on the finalize button.
- To show the data in the Grid View, we will write the following code.
Sample Code
Output
- After clicking on the Finalize button, confirmation message will display.
- After clicking on yes, the aggregate data is displayed in the Grid View as shown in the below screenshot.