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 visibility of the ListBox using Visible Property of the ListBox.
If the value of this property is set to true, then the Listbox control and its child control are visible on the screen and if the value of this property is set to false, then the Listbox control and its child control are not visible on the screen. The default value of this property is true. You can set this property in two different ways:
1. Design-Time: It is the easiest way to set the visibility 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 visibility of the ListBox.
Output:

2. Run-Time: It is a little bit trickier than the above method. In this method, you can set the visibility of the ListBox control programmatically with the help of given syntax:
public bool Visible { get; set; }
Here, the value of this property is of System.Boolean type. The following steps show how to set the visibility 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 lstbox = new ListBox();
- Step 2: After creating ListBox, set the Visible property of the ListBox provided by the ListBox class.
// Setting the visibility of the listbox lstbox.Visible = false;
- 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.Visible =false;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:
Before setting visibility to false:

After setting visibility to false:


