2

我有一个 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);
     }

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

combobox1,和combobox2_textbox1textbox2

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

still-textbox1, still-textbox2,still-textbox3still-textbox4

button2,它会将文本传输到组合框和文本框,它们是oldcombobox1、和oldcombobox2oldtextbox1oldtextbox2

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

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

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

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应该包含:

first, combobox1.text, combobox1.text,combobox1.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;
      }
    }
4

3 回答 3

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

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

于 2013-01-04T18:49:23.897 回答
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
于 2013-01-04T18:46:19.083 回答
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...
        }


     }
于 2013-01-04T18:49:07.857 回答