shariq_khan
shariq_khan

Reputation: 681

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

I have a windows form in which I have a button1 and when that is clicked a UserControl that is added dynamically to the code is 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 comboboxes and 2 textboxes

i.e combobox1, combobox2, textbox1 and textbox2

There are 4 textboxes which are on the same form

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

There is button2 and it will transfer the text to comboboxes and textboxes which are oldcombobox1, oldcombobox2, oldtextbox1 and oldtextbox2

When button1 is clicked twice it will add two UserControls to the form. I want to transfer the text in the following format

oldcombobox1.text = still-textbox1.text + "," + combobox1.text(which is dynamically generated) + "," + combobox1.text (which is dynamically generated) etc all the combobox1 text from the UserControl (which is added dynamically)

oldcombobox2.text = still-textbox2.text + "," + combobox2.text (which is dynamically generated) + "," + combobox2.text (which is dynamically generated) etc all the combobox2 text from the UserControl (which is added dynamically)

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

Means when the still-textbox1.text = first and when dynamic UserControl is added three times it will contain 3 times combobox1 then the oldcombobox1 should contain:

first, combobox1.text, combobox1.text, combobox1.text

I have made this code but it doesn't 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;
      }
    }

Upvotes: 2

Views: 832

Answers (3)

Nick Bray
Nick Bray

Reputation: 1963

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


     }

Upvotes: 0

Mithrandir
Mithrandir

Reputation: 25397

You should add to your class UserControl1 (great name btw ;-) ) something like this for every string you want to access from another object, in this case the string of 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;
 }

It's still horrible code, but it will work.

Upvotes: 1

Sam Axe
Sam Axe

Reputation: 33738

I would do this with events.

Create a class that inherits from EventArgs: (I prefer VB, you can 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

Then in your Control add the 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 the controls you need to hook up the event handler:

AddHandler myNewControl.ValueSubmittal, AddressOf ValueSubmittalHandler

And the ValueSubmittalHandler:

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

Upvotes: 0

Related Questions