passing variable between forms

  • Thread starter Thread starter Supra
  • Start date Start date
S

Supra

on form2 button event
form1.texbox1.text=( i don't know what inside buuton on form2)

regards,
 
Hello there
I know that if I have more than one form in the same project I will have to
create variables of any form I need to use.
I manage to open forms using the show(), now what I need to do is to set the
value of a control's property on the calling form.
As I can do that from the calling form to any control on the instance of
form2, how do I do the contrary?
Example:
I have form1, it contains a text box and a button.
I click on the button and an instance of form2 shows up.
I have a button on form2, I click on this button, and the text property of
the text box on form1 changes.
Any ideas on how do I do that?
 
Dear Supra and John
I know what you mean, and i have to declare a variable of form1 type.
But this would be appropriate if i am calling a new instance of form1. which
is not the case.
as fom1 is the caller for form2 and i need to modify a control on the
existing form1.

* when i added
form1.textbox1.text = "test"
the error "Reference to a non-shared member requires an object reference."
was generated.

* When I added
Dim frn As Form1
frn.textbox1.text = "test"
the runtime error "Object reference not set to an instance of an object."
was generated

* As for using
Dim frn As new Form1
frn.textbox1.text = "test"
nothing happens and when i show frm the control property is modified
nnormally, but again i want to modiy a property in the calling form not in a
freshly called instance.

thanks again and hope somebody can help.
Regards
Sameh
jcrouse said:
Yes...it's kind of like a file path. Just precede the controlname
(button1) with the form name(form1). Such as: "form1.button1.text = "OK""
You will also need to declare form1 on form2 if you reference it. Put a
statement such as: "Dim frm1 as New Form1" on form2.
 
I'm just learning .Net also and I know it sounds wrong but you NEED to declare form1 on form2 with a statement like

dim frm1 as new Form

then reference it as frm1. Even though it already exists

John
 
the idea is right.

here's what you can do.

'This is code in form1 for some button1 which is loading your form2 - note the Init metho
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Clic
Dim ofrm As New Form
ofrm.Init(Me
ofrm.Show(
End Su

'This is code in form2 - the Init method takes in a reference to type form1 - this will give you the handle to your calling for
Private m_oCallingForm As Form

Friend Sub Init(ByRef oCallingForm As Form1
m_oCallingForm = oCallingFor
End Su

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Clic
m_oCallingForm.Button1.Text = "Changed
End Su

hope this helps.

----- jcrouse wrote: ----

I'm just learning .Net also and I know it sounds wrong but you NEED to declare form1 on form2 with a statement like

dim frm1 as new Form

then reference it as frm1. Even though it already exists

John
 
Did not work
as this is not the instance that needs to be modified
Regards
Sameh
jcrouse said:
I'm just learning .Net also and I know it sounds wrong but you NEED to
declare form1 on form2 with a statement like:
 
Back
Top