C#如何获取组合框(列表框)的值,就是选中了其中的一项,如何取得它的值,再将值转为string类型。

解决方案 »

  1.   

    历遍所有的Item,查看哪个为Select,然后取出值来,再ToString
      

  2.   

     for (int i=0;i<  ListBox1.Items.Count;i++)
                {
                    ListItem Item = ListBox1.Items[Index];
                    if (ListBox1.Items[Index].Selected==true)
                    {
                     }
                }
      

  3.   

    string 楼上= ListBox1.SelectedValue;
      

  4.   

    ListBox1.SelectedItem.Text;  这样吧
      

  5.   

    for (i = 0; i < listBox1.Items.Count; i++)
                {
                    if (listBox1.Items[i].Selected == true)
                    {
                        break;
                    }
                }
             值=ListBox1.Items[i].Text
      

  6.   

    这是一个组合框选中其中一项发生的事件 private void cb1_SelectedIndexChanged(object sender, EventArgs e)
            {
                string a = cb1.SelectedItem.ToString();//获取选中的当前项
                try
                {
                    string sql = string.Format("select 期号 from cch where 期号={0}",a);//取得项,执行SQL语句
                    SqlCommand command = new SqlCommand(sql, DBH.connection);//创建SqlCommand对象
                    DBH.connection.Open();//打开连接
                    SqlDataReader datareader = command.ExecuteReader();//执行查询
                    string qiuhao1 = "";//期号名称1
                    string qiuhao2 = "";//期号名称2,1和2数据一样的
                    while (datareader.Read())
                    {
                        qiuhao1 = (string)datareader[0];//把搜到的值赋给变量
                        qiuhao2 = (string)datareader[0];//把搜到的值赋给变量
                        cb2.Items.Add(qiuhao1);//再把变量的值添加到列表控件中
                        cb3.Items.Add(qiuhao2);//再把变量的值添加到列表控件中
                    }
                    datareader.Close();
                }
                catch (Exception ex)
                {
                    throw;
                }
                finally
                {
                    DBH.connection.Close();
                }
               
            }最主要的是“string a = cb1.SelectedItem.ToString();”这个语句,调试时,出现了“=”附近有语法错语SqlException,这样的提示。
    我要得到组合框的项值,然后执行SQl语句