如何绑定事件comboBox -TextBox

时间:2012-12-09 14:48:14

标签: c# event-handling dynamic-controls

我有关于某事的计算并动态创建一个表格。

 comboboxAdet.Size = new System.Drawing.Size(100, 300);
            comboboxAdet.Location = new System.Drawing.Point(515, 5);
            comboboxAdet.Margin = new Padding(2, 3, 2, 3);
            comboboxAdet.SelectedIndexChanged += new EventHandler(combobox_SelectedindexChanged);
            /**************************************************/
            TextBox textSatirtoplam = new TextBox();
            textSatirtoplam.Text = satirhesapla(i);
            textSatirtoplam.Name = "labelSatirToplam" + i.ToString();
            textSatirtoplam.Size = new System.Drawing.Size(100, 300);
            textSatirtoplam.Location = new System.Drawing.Point(630, 5);
            textSatirtoplam.MouseClick += new MouseEventHandler(combobox_SelectedindexChanged);
            textSatirtoplam.Visible = true;

问题是当我更改comboxBoxSelected项时,文本框可以更改。我试过eventhandler但是失败了。这意味着如何到达文本框?

enter image description here

如果你帮助我,我会非常开心!谢谢 我希望很清楚。

1 个答案:

答案 0 :(得分:0)

如果我正确理解您的问题,您希望更新与发生SelectIndexChanged事件的行相同的文本框。

您可以使用委托并将闭包传递给您的事件以获取对文本框的引用。

var combox = new ComboBox();
// code left out

var txtBox = new TextBox();
// code left out

combox.SelectedIndexChanged += delegate(Object sender, EventArgs e)
{
    var destTextBox = txtBox;  // important assign closure to a local variable.
    var srcCombo = (ComboBox) sender;

    destTextBox.Text = srcCombo.SelectedText;
};
相关问题