How to pass text from a dynamically created user control to a text field

I have a window form in which button1 , when the usercontrol is clicked, the code is dynamically added, this

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

that usercontrol contains 4 controls 2 combobox and 2 text fields

ie combobox1 combobox2 textbox1 textbox2

there are 4 text fields that are in the same form as

still-textbox1, still-textbox2, still-textbox3, still-textbox4

another button2 , there it will transfer text to comboboxes and texboxes, which is oldcombobox1 oldcombobox2 oldtextbox1 old textbox2

when button1 double-clicks, it will add two user controls to the form. I want to transfer text in this format

oldcombobox1.text = still-textbox1.text + "," + combobox1.text (which is dynamically generated) + "," + combobox1.text (which is dynamically generated), etc. all combobox1 from a user control that is dynamically added)

oldcombobox2.text = still-textbox2.text + "," + combobox2.text (which is dynamically generated) + "," + combobox2.text (which is dynamically generated), etc. all combobox2 from a user control that is dynamically added)

oldtextbox1.text = still-textbox3 + "," + textboox1.text (which is dynamically generated) + "," + textbox1.text (which is dynamically generated), etc. all text field1 from the user control, which is added dynamically)

means the file is still-textbox1.text = first and when dynamic user control is added three times, it will contain 3 times combobox1, then

oldcombobox1 should contain

Fisrt, combobox1.text, combobox1.text, combobox1.text

I made this code but it does not work

  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; } } 
+2
source share
3 answers

You should add something like this to your UserControl1 class (great name btw ;-)) for each line you want from another object, in this case the line textBox1 :

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

Then you can say in your Form class:

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

This is still terrible code, but it will work.

+1
source

I would do it with events.

Create a class that inherits from EventArgs: (I prefer VB, you can drag and drop)

 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 

Then in your control add an event:

 Public Event ValueSubmittal As EventHandler(Of ControlEventArgs) 

In your Button2_Click handler:

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

And in your form, where you dynamically create controls, you need to hook up an event handler:

 AddHandler myNewControl.ValueSubmittal, AddressOf ValueSubmittalHandler 

And the value of ValueSubmittalHandler:

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

You can create a class level variable:

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

All Articles