如何将文本从动态生成的用户控件传输到文本框
我有一个Windows窗体,其中一个按钮1,当点击一个用户控件动态添加代码就是这个
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个combobox和2个文本框
即combobox1 combobox2 textbox1 textbox2
有4个文本框,它们是相同的forms
still-textbox1,still-textbox2,still-textbox3,still-textbox4
还有一个button2 ,它会将文本传输到combobox和texboxes,这是oldcombobox1 oldcombobox2 oldtextbox1 old textbox2
当button1单击两次时,它将在我希望以此格式传输文本的表单中添加两个usercontrol
oldcombobox1.text = still-textbox1.text +“,”+ combobox1.text(动态生成)+“,”+ combobox1.text(动态生成)等来自用户控件的动态添加的所有combobox1)
oldcombobox2.text = still-textbox2.text +“,”+ combobox2.text(动态生成)+“,”+ combobox2.text(动态生成)等来自用户控件的动态添加的所有combobox2)
oldtextbox1.text = still-textbox3 +“,”+ textboox1.text(动态生成)+“,”+ textbox1.text(动态生成)等所有动态添加的用户控件的textbox1)
表示当still-textbox1.text = first时和动态usercontrol添加三次时它将包含3次combobox1然后
oldcombobox1应该包含
最前一页,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; } }
你应该为你想从另一个对象访问的每个字符串添加你的类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; }
它仍然是可怕的代码,但它会工作。
我会用事件来做这件事。
创建一个inheritance自EventArgs的类:(我更喜欢VB,你可以进行traslate)
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
然后在您的Control中添加事件:
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... } }