繁体   English   中英

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

[英]How to transfer the text from dynamically generated user control to a textbox

我有一个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)等来自UserControl所有textbox1文本(动态添加)

意味着当still-textbox1.text = first ,当动态UserControl添加3次时,它将包含3次combobox1oldcombobox1应该包含:

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;
      }
    }

对于您要从另一个对象访问的每个字符串,您应该在类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;
 }

它仍然是可怕的代码,但是可以正常工作。

我会通过事件来做到这一点。

创建一个从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

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

    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...
        }


     }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM