请问,如何改变ListBox的选定项的值 请问,如何改变ListBox的选定项的值 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 listBox1.Items[listBox1.SelectedIndex] ListItem lst =ListBox1.SelectedItem; lst.Text = "3"; lst.Value = "3"; this.listBox2.Items[0] = "aaaaaaaa";以上仅对item为string类型有效,因为listbox可以添加任意类型的item,所以你要根据具体情况来做 /// <summary> /// 取ListBox选中项id /// </summary> /// <returns>id数组</returns> private int[] GetIDS() { int[] id = new int[listBox1.SelectedIndices.Count]; for (int i = 0; i < listBox1.SelectedIndices.Count; i++) { id[i] = (int)listBox1.SelectedIndices[i]; } return id; } GetIDS()取出来的是选中项ID数组,你可以用循环遍历出来:int[] id=GetIDS(); textBox1.Clear(); for (int i = 0; i<id.Length; i++) { textBox1.Text += id[i].ToString(); } 前面的回答,只有4楼稍正确,但还是有异常我用的this.listBox2.Items[0] = "aaaaaaaa"; 直接设置SelectedValue 就可以了。 <asp:ListBox ID="ListBox1" SelectionMode="Multiple" runat="server"> <asp:ListItem Value="1">a</asp:ListItem> <asp:ListItem Value="2">b</asp:ListItem> <asp:ListItem Value="3">c</asp:ListItem> <asp:ListItem Value="4">d</asp:ListItem> </asp:ListBox> <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" /> protected void Button1_Click(object sender, EventArgs e) { ListBox1.SelectedValue = "4"; } private void listBox1_SelectedIndexChanged(object sender, EventArgs e) { listBox1.SelectedValue = "4"; } private void button1_Click(object sender, EventArgs e) { listBox1.SelectedValue = "4"; }还是不行,两个事件都不行 操作SelectedIndex也可以,为-1时表示不选中任何项 高分求助高手给写个解密算法,不胜感激! c#access数据库中添加记录 请问怎样实现DevExpress用代码实现横打?大家帮帮忙.. 如何用私钥解密码? 如何获取网页上的数据? 如何实现基于Internet的报表管理软件 winform中使用线程池的问题 BeginInvoke 如何用word文档开发网络聊天 有关事件和委托 DataGird中,我选中行时能不能击发一个事件?? OutLook编程问题 怎么设置DataGridView中第一个格的内容
lst.Text = "3";
lst.Value = "3";
以上仅对item为string类型有效,因为listbox可以添加任意类型的item,所以你要根据具体情况来做 /// <summary>
/// 取ListBox选中项id
/// </summary>
/// <returns>id数组</returns>
private int[] GetIDS()
{
int[] id = new int[listBox1.SelectedIndices.Count];
for (int i = 0; i < listBox1.SelectedIndices.Count; i++)
{
id[i] = (int)listBox1.SelectedIndices[i];
}
return id;
} GetIDS()取出来的是选中项ID数组,你可以用循环遍历出来:int[] id=GetIDS();
textBox1.Clear();
for (int i = 0; i<id.Length; i++)
{
textBox1.Text += id[i].ToString();
}
我用的this.listBox2.Items[0] = "aaaaaaaa";
<asp:ListBox ID="ListBox1" SelectionMode="Multiple" runat="server">
<asp:ListItem Value="1">a</asp:ListItem>
<asp:ListItem Value="2">b</asp:ListItem>
<asp:ListItem Value="3">c</asp:ListItem>
<asp:ListItem Value="4">d</asp:ListItem>
</asp:ListBox>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" /> protected void Button1_Click(object sender, EventArgs e)
{
ListBox1.SelectedValue = "4";
}
{
listBox1.SelectedValue = "4";
}
private void button1_Click(object sender, EventArgs e)
{
listBox1.SelectedValue = "4";
}还是不行,两个事件都不行