0

I have a small problem, I have UserControl1 containing a Button and a TextBox. I've an instance of the user control on Form1 and I want when I click the button of user control, Form2 get opened and the text from the textbox of user control appear in TextBox1 if Form2.

NineBerry give me this example Link but I can't solve the problem yet using the link, any help please :)

Community
  • 1
  • 1
Rabeea qabaha
  • 526
  • 8
  • 23
  • Possible duplicate of [Handle event of a User Control's control in a form](http://stackoverflow.com/questions/14603960/handle-event-of-a-user-controls-control-in-a-form) – NineBerry Oct 16 '16 at 01:37
  • @NineBerry I cant do it like the example you put it, how i can do it in textbox text ?? – Rabeea qabaha Oct 16 '16 at 02:07
  • When do you want to pass text which is in user control to a textbox in the form? Does the form request to read text or does the control should notify the form from changes? Describe it a little more. – Reza Aghaei Oct 16 '16 at 05:03
  • @RezaAghaei thanks to be here, what i have is, `textbox1` and `Button1` in `UserControl1`, what i want is when i click `button1` in `UserControl1` `Form2` opened and the text from `textbox1` in `UserControl` appear in `textbox1` in `form2`,(i have 9 textboxes – Rabeea qabaha Oct 16 '16 at 12:19
  • *1)* First you should create an event for your `UserControl` like [this](http://stackoverflow.com/a/36130796/3110834). Then in your `Form1` you can subscribe for the event like any other event. **2** For reading value from `textBox1` of user control and setting `textBox1` of your form, the best option is having a property like what I have in example 2 of [this post](http://stackoverflow.com/a/38769212/3110834) for `Form2` and `UserControl1`. Then you can simply read value from user control in that event and set the value for the second form. You will find those posts really useful. – Reza Aghaei Oct 16 '16 at 12:27
  • By the way, if always you want to Open `Form2` and pass text of `TextBox1` to it, you don't need to create an consume the event. It's enough to handle `Click` event of `Button1` and then use either of examples 1 to 3 from [this post](http://stackoverflow.com/a/38769212/3110834). – Reza Aghaei Oct 16 '16 at 12:51
  • @RezaAghaei, i solved the problem, thanks again for helping:) – Rabeea qabaha Oct 16 '16 at 14:44
  • Great! You're welcome :) – Reza Aghaei Oct 16 '16 at 14:46
  • @RezaAghaei absolutely – Rabeea qabaha Oct 16 '16 at 14:50

1 Answers1

1

This how i Don it, Thanks for my Friend Reza Aghaei for helping.

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim frm As New SbillPrint
        frm.smoney_txt.Text = moneys_txt.Text
        frm.ShowDialog()
End Sub
Rabeea qabaha
  • 526
  • 8
  • 23
  • Future readers should know when you put a `TextBox` on a `Form`, it's modifier is `Friend` by default and the control is accessible from other classes and forms, so you can simply access the control using `YourFormInstance.YourTextBox.Text`. – Reza Aghaei Oct 16 '16 at 14:51