How can we control another form from another form? I mean we control form A from from B. For example, if we click button OK on form A, the action will occur on form B.
Printable View
How can we control another form from another form? I mean we control form A from from B. For example, if we click button OK on form A, the action will occur on form B.
Gabriell
You can refer to controls in formB from formA as follows:
VB Code:
' cmdUpdate event is in form A private sub cmdUpdate_Click() frmFormB.txtCustid.Text = 123 End Sub
I personally don't like this technique of explicitly refering to a control on another form. You can alternatively add a public method in Form B and then invoke that method from form A
VB Code:
' Code in form B Public sub UpdateCustomerID(byval custid as string) txtCustid.Text = custid End sub ' code in form A ' cmdUpdate event is in form A private sub cmdUpdate_Click() frmFormB.UpdateCustomerID("123") End Sub
Send a instance of the first form to the second form when its opened. To do that rewrite the constructor of the second form to take a form as an argument. Something like this...
Then you have a reference to the form.Code:Dim baseForm as Form1
Public Sub New(frm as Form)
baseform = frm
InitializeComponent()
End Sub
or you could declare instances of your forms in a module as follow:Quote:
Originally posted by Gabriell
How can we control another form from another form? I mean we control form A from from B. For example, if we click button OK on form A, the action will occur on form B.
VB Code:
'in a module dim frm1 as new form1 dim frm2 as new form2 'in a form : frm2.button1.name ="blah"