1.列表框(listBox)重要属性
-
Items:输入项目,一行一个
-
SelectionMode:一次可以选中一个/多个/通过组合键选中多个
-
MultiColumn:是否在列中水平显示
2.完整程序
1.Button展示列表框中选中的城市:
private void button1_Click(object sender, EventArgs e)
{
switch(listBox1.SelectedIndex)
{
case 0: button1.Text = listBox1.SelectedItem.ToString(); break;
case 1: button1.Text = listBox1.SelectedItem.ToString(); break;
case 2: button1.Text = listBox1.SelectedItem.ToString(); break;
default:button1.Text = "选中了其他城市。";break;
}
}
2.将文本框中的内容在列表框中添加/删除/清除
private void button1_Click(object sender, EventArgs e)
{
listBox1.Items.Add(textBox1.Text);
textBox1.Clear();
}
private void button2_Click(object sender, EventArgs e)
{
if (listBox1.SelectedIndex != -1)
{
listBox1.Items.RemoveAt(listBox1.SelectedIndex);
}
}
private void button3_Click(object sender, EventArgs e)
{
listBox1.Items.Clear();
}