日期:2014-05-17  浏览次数:21072 次

comboBox值得变化,关联其他textbox,怎么不对

private void comboBox1_TextUpdate(object sender, EventArgs e)
        {           
            OleDbCommand command = new OleDbCommand();
            command.Connection = conn;
            command.CommandText = string.Format("select 窗口,X坐标,Y坐标 from click where 窗口={0}", comboBox1.Text);
            command.CommandType = CommandType.Text;
            OleDbDataReader reader = command.ExecuteReader();
            if (reader.Read())
            {
                textBox1.Text = reader["X坐标"].ToString();
                textBox2.Text = reader["Y坐标"].ToString();
            } 
        }

------解决方案--------------------
ComboBox的三个事件:TextUpdate、TextChanged和SelectedIndexChanged
TextUpdate                      是说当ComboBox变化时发生,只要是手动输入就会触发
TextChanged                  是说当ComboBox发生变化后,无论是手动输入,还是下拉框选择数据,都会触发
SelectedIndexChanged   是说当ComboBox的选中的item变化后触发,手动输入数据是不会触发的!

你comboBox的值内容变化,一定不是你手动输入的,因此不会出发TextUpdate事个件,我验证了用TextChanged和SelectedIndexChange一定会触发事件里面的代码引起TextBox值的变化。
               
------解决方案--------------------
string strSql = string.Format("select 窗口,X坐标,Y坐标 from click where 窗口='{0}'", comboBox1.Text);