How to add a list box control to a WinForm window?
To add a list box control in a WinForm window, you can follow the steps below:
- Open Visual Studio and create a new WinForm project.
- Open the form designer, locate the ListBox control in the toolbox, and drag it onto the form.
- Resize and reposition the list box control to fit the layout of the form.
- You can set the properties of a list box control in the properties window, such as name, background color, font, and so on.
- To add options in a list box, you can achieve it with the following code:
// 添加选项
listBox1.Items.Add("Option 1");
listBox1.Items.Add("Option 2");
listBox1.Items.Add("Option 3");
- You can handle the selection event of items in a list box by implementing the following code.
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
// 处理选项选择事件
MessageBox.Show("选中项:" + listBox1.SelectedItem.ToString());
}
By following the above steps, you can add a list box control to a WinForm window and customize and manipulate it.