如何将文本从动态生成的用户控件传输到文本框

时间:2013-01-04 18:33:59

标签: c#

我有一个Windows窗体,其中我有一个button1,当点击它时,动态添加到代码中的UserControl是:

    int c = 0;
    private void button1_Click(object sender, EventArgs e)
    {
        int v;
        v = c++;
        panel1.VerticalScroll.Value = VerticalScroll.Minimum;
        UserControl1 us = new UserControl1();
        us.Name = "us" + v;
        us.Location = new Point(50, 5 + (30 * v));
        us.Tag = btn;
        panel1.Controls.Add(us);
     }

UserControl包含4个控件2个组合框和2个文本框

combobox1combobox2textbox1textbox2

有4个文本框在同一表格上

still-textbox1still-textbox2still-textbox3still-textbox4

button2,它会将文本转移到oldcombobox1oldcombobox2oldtextbox1oldtextbox2

的组合框和文本框中

点击button1两次后,它会向表单添加两个UserControls。 我想以下列格式传输文本

oldcombobox1.text = still-textbox1.text + "," + combobox1.text(which is dynamically generated) + "," + combobox1.text (which is dynamically generated)等来自UserControl的所有combobox1文本(动态添加)

oldcombobox2.text = still-textbox2.text + "," + combobox2.text (which is dynamically generated) + "," + combobox2.text (which is dynamically generated)等来自UserControl的所有combobox2文本(动态添加)

oldtextbox1.text = still-textbox3 + "," + textboox1.text (which is dynamically generated) + "," + textbox1.text (which is dynamically generated)等来自textbox1的所有UserControl文字(动态添加)

表示still-textbox1.text = first时 当动态UserControl添加三次时,它将包含3次combobox1,那么oldcombobox1应包含:

firstcombobox1.textcombobox1.textcombobox1.text

我已经制作了这段代码,但它不起作用

  foreach (Control ctrl in panel1.Controls)
   {
     if (ctrl is UserControl)
     {
         UserControl1 myCrl = ctrl as UserControl1;
         oldcombobox1.text = still-textbox1.text + "," + myCrl.comboBox1.Text;
         oldcombobox2.Text =still-textbox2.text + "," + myCrl.comboBox2.Text;
         oldtextbox1.Text = still-textbox3.text + "," + myCrl.textBox1.Text;
         oldtextbox2.Text.Text = still-textbox4.text + "," + myCrl.textBox2.Text;
      }
    }

3 个答案:

答案 0 :(得分:1)

你应该为你想要从另一个对象访问的每个字符串添加你的类UserControl1(伟大的名字btw ;-))类似的东西,在这种情况下是textBox1的字符串:

public String FirstTextBoxText 
{
   get { return this.textBox1.Text; }
}

然后你可以在你的Form类中说:

 if (ctrl is UserControl)
 {
     UserControl1 myCrl = ctrl as UserControl1;
     // ...
     oldtextbox1.Text = still-textbox3.text + "," + myCrl.FirstTextBoxText;
 }

它仍然是可怕的代码,但它会起作用。

答案 1 :(得分:0)

我会用事件来做这件事。

创建一个继承自EventArgs的类: (我更喜欢VB,你可以翻译)

Public Class ControlEventArgs
  Inherits EventArgs

  Public Property Value1 As String = String.Empty
  Public Property Value2 As String = String.Empty
  Public Property Value3 As String = String.Empty
  Public Property Value4 As String = String.Empty

End Class

然后在您的控件中添加事件:

Public Event ValueSubmittal As EventHandler(Of ControlEventArgs)

在你的Button2_Click处理程序中:

RaiseEvent ValueSubmittal(me, new ControlEventArgs With {.Value1=comboBox1.Text, .Value2 = comboBox2.Text, .Value3 = textBox1.Text, .Value4 = textBox2.Text}

在动态创建控件的表单中,您需要连接事件处理程序:

AddHandler myNewControl.ValueSubmittal, AddressOf ValueSubmittalHandler

ValueSubmittalHandler:

Private Sub ValueSubmittalHandler(sender as Object, e As ControlEventArgs)
  formControl1.Text = e.Value1
  formControl2.Text = e.Value2
  '  etc...
End Sub

答案 2 :(得分:0)

您可以创建一个类级变量:

    private UserControl1 us1;
    private UserControl1 us2;

    private void button1_Click(object sender, EventArgs e)
    {
        int v;
        v = c++;
        panel1.VerticalScroll.Value = VerticalScroll.Minimum;

        if(us == null) 
        {
            //this is the first time the control is created
            us1 = new UserControl1();
            us1.Name = "us" + v;
            us1.Location = new Point(50, 5 + (30 * v));
            us1.Tag = btn;        
            panel1.Controls.Add(us1);
        }
        else if(us2 ==null)
        {
            us2 = new UserControl1();
            //whatever code you want to execute to change second one
            //you can access first control as us1.xxx
            panel1.Controls.Add(us2);

        }
        else
        {
           //3rd 4th etc...
        }


     }