SelectionMode | 组件中条目的选择类型:单选,多选 |
Row | 此组件显示多少行 |
Selected | 检测条目是否被选中 |
SelectedItem | 返回类型是ListItem,获得组件中被选择的条目 |
Count | 组件中条目的总数 |
SelectIndex | 组件中被选择条目的索引值 |
Items | 泛指组件中所有的条目,每个条目的类型都是ListItem |
1、动态添加列表框
protected
void
TextBox1_TextChanged(
object
sender, EventArgs e)

{
ListBox2.Items.Add(TextBox1.Text);
Button1.Text = "移除";
Button1.Enabled = true;
Button2.Text = "清空";
Button2.Enabled = true;
}
protected
void
Button2_Click(
object
sender, EventArgs e)

{
if (ListBox2.Items.Count > 0)

{
ListBox2.Items.Clear();
Button2.Enabled = false;
}
}
protected
void
Button1_Click(
object
sender, EventArgs e)

{
if (ListBox2.Items.Count > 0)

{
ListBox2.Items.Remove(ListBox2.SelectedItem); //移除选定项
}
else

{
Button1.Enabled = false;
}
}
2、ListBox两级联动
protected
void
ListBox3_SelectedIndexChanged(
object
sender, EventArgs e)

{
switch (ListBox3.SelectedValue)

{
case "fz":
ListBox4.Items.Clear();
ListBox4.Items.Add("鼓山");
ListBox4.Items.Add("西湖");
ListBox4.Items.Insert(1,"旗山");
break;
case "sh":
ListBox4.Items.Clear();
ListBox4.Items.Add("东方明珠");
ListBox4.Items.Add("浦东");
break;
case "hk":
ListBox4.Items.Clear();
ListBox4.Items.Add("维多利亚港");
break;
case "bj":
ListBox4.Items.Clear();
ListBox4.Items.Add("故宫");
ListBox4.Items.Add("长城");
break;
}
3、列表指定项位移与光标移动
protected
void
Button3_Click(
object
sender, EventArgs e)

{
if (((Button)sender).CommandName == "up" && ListBox5.SelectedIndex > 0 || ((Button)sender).CommandName == "down" && ListBox5.SelectedIndex < ListBox5.Items.Count - 1)

{
int index;
if (((Button)sender).CommandName == "up")

{
index = -1;
}
else

{
index = 1;
}
ListItem lt = new ListItem(ListBox5.SelectedItem.Text,ListBox5.SelectedItem.Value);
ListBox5.Items[ListBox5.SelectedIndex].Text = ListBox5.Items[ListBox5.SelectedIndex + index].Text;
ListBox5.Items[ListBox5.SelectedIndex].Value = ListBox5.Items[ListBox5.SelectedIndex + index].Value;
ListBox5.Items[ListBox5.SelectedIndex + index].Text = lt.Text;
ListBox5.Items[ListBox5.SelectedIndex + index].Value = lt.Value;
ListBox5.SelectedIndex = ListBox5.SelectedIndex + index;
}

}
protected
void
Button5_Click(
object
sender, EventArgs e)

{
ListBox5.SelectedIndex = 0;
}
protected
void
Button6_Click(
object
sender, EventArgs e)

{
ListBox5.SelectedIndex -= 1;

}
protected
void
Button7_Click(
object
sender, EventArgs e)

{
ListBox5.SelectedIndex += 1;
}
protected
void
Button8_Click(
object
sender, EventArgs e)

{
ListBox5.SelectedIndex = ListBox5.Items.Count - 1;
}
转载于:https://www.cnblogs.com/net123/archive/2007/12/27/1016845.html