请问,如何改变ListBox的选定项的值

解决方案 »

  1.   

    listBox1.Items[listBox1.SelectedIndex]
      

  2.   

    ListItem lst =ListBox1.SelectedItem;
      lst.Text = "3";
      lst.Value = "3"; 
      

  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.   

    前面的回答,只有4楼稍正确,但还是有异常
    我用的this.listBox2.Items[0] = "aaaaaaaa";
      

  5.   

    直接设置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";
        }
      

  6.   

     private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
            {
                listBox1.SelectedValue = "4";
            }
     private void button1_Click(object sender, EventArgs e)
            {
                listBox1.SelectedValue = "4";
            }还是不行,两个事件都不行
      

  7.   

    操作SelectedIndex也可以,为-1时表示不选中任何项