通过文本框更改下一个组合框

时间:2014-11-30 19:01:24

标签: c# combobox textbox

我通过点击一个包含以下代码的按钮来创建一些文本框和组合框:

/////////////////add tbxcode
            TextBox textbox = new TextBox();
            textbox.Location = new System.Drawing.Point(448, (35 * count) + 2);
            textbox.Size = new System.Drawing.Size(100, 17);
            textbox.Name = "tbxcode_" + (count + 1);
            panel1.Controls.Add(textbox);
            /////////////////add cbxname
            ComboBox combobox = new ComboBox();
            combobox.Location = new System.Drawing.Point(303, (35 * count) + 2);
            combobox.Size = new System.Drawing.Size(140, 17);
            combobox.Name = "cbxname_" + (count + 1);
            combobox.DropDownStyle = ComboBoxStyle.DropDownList;


            DataSet ds = new DataSet();
            OleDbDataAdapter adp = new OleDbDataAdapter();
            adp.SelectCommand = new OleDbCommand();
            adp.SelectCommand.Connection = oleDbConnection1;
            adp.SelectCommand.CommandText = "select * from kala";
            adp.Fill(ds);
            combobox.DataSource = ds.Tables[0];
            combobox.DisplayMember = "name";
            combobox.ValueMember = "code";

            panel1.Controls.Add(combobox);

通过更改文本框值我想更改下一个组合框选择的项目! 我怎么能这样做?

1 个答案:

答案 0 :(得分:0)

首先将OnTextChanged事件添加到文本框

//this when you are adding the textbox
textbox.TextChanged += TextBox_TextChanged;

private void TextBox_TextChanged(object sender, EventArgs e)
{
     comboBox.Text = "Wanted Value";
}

编辑:

您应该在CreateChildControls方法中创建两个控件并使它们显示为false。单击该按钮时,您应该看到控件。我假设您在按钮点击时创建了两个控件,错误。您也可以在面板中添加它们,只需将它们显示为false,然后点击即可显示true

    protected override void CreateChildControls()
    {

    }