1 Variable - 2 Forms

M

Miro

I am banging my head around something that I think I just dont understand
what I am reading in the helps.
Its gotta be so simple but I just cant figure out what im reading. ( vb
2003 )

Lets say i have Form1 and Form2.

Each form is blank. ( no text boxes or anything )
Very Simple Example: ( i just need a start and then i can modify it and
learn from it to do what i need to do )

On form1_Load
'I have a variable called cHello
Dim cHello As String = "Hello World"

' I call form two.
Dim frmSecondForm As Form = New Form2()
frmSecondForm .ShowDialog()

EndSub

On form 2's load, I want to
MsgBox( cHello ,MsgBoxStyle.OKOnly, "Title Of Box")
close()
EndSub


How do I Properly pass the variable to form2 ?


Thanks,

Miro
 
S

sweet_dreams

Miro napisal(a):
I am banging my head around something that I think I just dont understand
what I am reading in the helps.
Its gotta be so simple but I just cant figure out what im reading. ( vb
2003 )

Lets say i have Form1 and Form2.

Each form is blank. ( no text boxes or anything )
Very Simple Example: ( i just need a start and then i can modify it and
learn from it to do what i need to do )

On form1_Load
'I have a variable called cHello
Dim cHello As String = "Hello World"

' I call form two.
Dim frmSecondForm As Form = New Form2()
frmSecondForm .ShowDialog()

EndSub

On form 2's load, I want to
MsgBox( cHello ,MsgBoxStyle.OKOnly, "Title Of Box")
close()
EndSub


How do I Properly pass the variable to form2 ?


Thanks,

Miro

Hi Miro,

If you want to pass variable between multiple form you have to declare
it as Public:

'form1

Public _text As String

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
_text = "Hello"

Dim f2 As New Form2
Form2.Show()

End Sub

'form2

Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim TextFromForm1 As String

TextFromForm1 = Form1._text

MessageBox.Show(TextFromForm1)

End Sub

While working with vb.net you have to be aware that everything is
object here. So if want access objects property, method... you have to
specify this object like it was done here:
Form1._text

If you have problems working with multiple forms read this articles:
http://www.devcity.net/Articles/94/multipleforms.aspx
http://www.devcity.net/Articles/100/multipleforms2.aspx
http://www.devcity.net/Articles/102/multipleforms3.aspx
http://www.devcity.net/Articles/117/1/multipleforms4.aspx

And by the way, msgbox() is clasic vb function. In vb.net it is
recommended to use:
messagebox.show(text, caption, buttons, icon)

Hope this helps.

Regardsm
sweet_dreams
 
A

ag

pass it on Form2 thru constructor parameter wen calling form2

pseudo code:

dim myString as string="hello world"

dim f2 as new form2(myString)

----- now in form 2

dim myvar as string

sub new (byval x as string)
myvar=x
end sub

now on load event put the msgbox n display myvar
 
G

Guest

you could also make one variable Public so everything can see it, (VB 2005) ex:

Public str As String

Private Sub Form1_Load(..)

Dim frm As New Form2

str = "Hello"

frm.Show()

End Sub

'''form 2
Private Sub Form2_Load(...)

me.text = My.Forms.Form1.str

end Sub


''''''''''''''''''''''''

or you could make the variable reflect a property on form1

just my 2 cents, hope this helps
 
M

Miro

Thank you all,

I was soo close.
Maybe I just needed some sleep. :)

thanks agian,

Miro
 
M

Miro

I tried your solution,

It didnt work,
I had my variable declared as Public myText As String

but it didnt recognize it on the second form until I put

Public Shared myText as String

Is this a vb 2005 to vb 2003 difference or did you just miss that in the
example ?


Thank you for your example though... the links on the bottom, I actually
read the night before,
but in my case I was always missing the "shared" property.

Thanks again,

Miro
 
S

sweet_dreams

Miro napisal(a):
I tried your solution,

It didnt work,
I had my variable declared as Public myText As String

but it didnt recognize it on the second form until I put

Public Shared myText as String

Is this a vb 2005 to vb 2003 difference or did you just miss that in the
example ?


Thank you for your example though... the links on the bottom, I actually
read the night before,
but in my case I was always missing the "shared" property.

Thanks again,

Miro


Hi Miro,

I'm using VB 2005 and it works without Shared. But using Shared is also
good. So use Shared.

Regards,
sweet_dreams
 
M

Miro

Im 2003 and It does not recognize the formname.value if the "Shared"
property is not there.

So that looks like it is a difference in 2k3 and 2k5 version.

Thanks Sweet_Dreams
ps. "napisal(a)" is that Czech or Slovak ? - "Miro Wrote" :)

thanks agian.
 
S

sweet_dreams

Miro napisal(a):
Im 2003 and It does not recognize the formname.value if the "Shared"
property is not there.

So that looks like it is a difference in 2k3 and 2k5 version.

Thanks Sweet_Dreams
ps. "napisal(a)" is that Czech or Slovak ? - "Miro Wrote" :)

None of them. It's Polish :)
 
G

GhostInAK

Hello Miro,

My god, it's like the blind leading the blind.. Lets set things straight.

First, The new form instantiation shortcuts in 05 suck ass. M$: Take this
crap OUT.
M$ made it possible to reference a "default instance" of a form without explicitly
instantiating it, in 2005. This is more VB6 style than .NET, and it is misleading
and confusing to newbies. TAKE IT OUT.

So.. While you can simply do Form2.Show, and Form1.SomeField in 05, in 03
you must have explicit instances of Form1 and Form2 (you do in 05 also, but
the language hides the details from you).

This is also why Miro noticed that adding the Shared keyword to the field
made things work. Ya'll need to read up on variable scope.

Proper way to do this:
in Form2.vb: Add a private field (sSomeText), and a public property (SomeText)
in Form1.vb: Add to the Load event handler:
Dim tForm as Form2 = New Form2
tForm.SomeText = "blah blah"
tForm.Show

in Form2.vb: Add to Load event handler:
msgbox(me.SomeText)


Also, as stated earlier, you could overload the ctor for Form2 and pass in
the text there.

Enjoy,
-Boo
 
M

Miro

The problem wasnt with a text box
- If i understand your example.

The proplem is using a straight variable that I run some calculations on and
have random outcom and then pass it to another form.
Everywhere on the net they use a textbox.text example...

nowhere do they have a Dim myVariable as String = "X" and now pass that
between forms.

M.
 
G

GhostInAK

Hello Miro,

Variable, Property.. whatever.. it doesnt matter..
How about this example:

on form2 create a new sub:
Public Sub MyGodItsHideous(byval tCreatureName as string)
msgbox("When " & tCreatureName & " was a child we had to hang a chunk of
meat around its neck just to get the dog to play with it!")
End Sub


on Form1:

Dim tForm as Form2 = New Form2
tForm.MyGodItsHideous("Jennifer Lopez")


The point is, a form is just like any other class.

-Boo
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top