User Control Question

wengwashere

Member
Joined
Apr 25, 2011
Messages
5
Programming Experience
3-5
I dynamically load a user control in a form (Form 1).

In my user control there are two things :

a Textbox (TxtBox1) and a Command Button

WHen the button is clicked, it opens a new form (Form 2) Form2.ShowDialog()

My question is, in Form 2, how can i set a value to TxtBox1 of the user control?
 
You should be able to reference Form1 from Form2 and your user control should exist in the Form1.Controls collection assuming you added it to Form1. So therefore something like the below code should work to find the control from Form2 (assuming it does not exist as a Global variable then you could just reference it):

VB.NET:
'in Form2
For each ctl as control in Me.Controls
   If typeof ctl is YourUserControl Then
      Dim yuc as YourUserControl = DirectCast(ctl, YourUserControl)
      yuc.Visible = False
      'or whatever code you want to run on your user control
   End If
Next ctl

There are a million other ways to go about referencing the control that exists in Form1 from Form2. But the controls collection of each form holds the controls that are found on the form and you can reference the controls of each form from the other form.
 
Back
Top