473,407 Members | 2,306 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

1 Variable - 2 Forms

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


Aug 3 '06 #1
11 1758

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/...pleforms4.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

Aug 3 '06 #2
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

Aug 3 '06 #3
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
--
-iwdu15
Aug 3 '06 #4
Thank you all,

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

thanks agian,

Miro

"iwdu15" <jmmgoalsteratyahoodotcomwrote in message
news:B5**********************************@microsof t.com...
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
--
-iwdu15

Aug 3 '06 #5
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
"sweet_dreams" <sw**********@o2.plwrote in message
news:11*********************@75g2000cwc.googlegrou ps.com...
>
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/...pleforms4.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

Aug 3 '06 #6

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

Aug 3 '06 #7
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.
"sweet_dreams" <sw**********@o2.plwrote in message
news:11**********************@75g2000cwc.googlegro ups.com...
>
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

Aug 3 '06 #8

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 :)

Aug 3 '06 #9
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
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

Aug 4 '06 #10
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.

"GhostInAK" <gh*******@gmail.comwrote in message
news:c7**************************@news.microsoft.c om...
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
>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


Aug 4 '06 #11
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
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.

"GhostInAK" <gh*******@gmail.comwrote in message
news:c7**************************@news.microsoft.c om...
>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
>>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

Aug 4 '06 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Chris Beall | last post by:
If you want your code to be bulletproof, do you have to explicitly check for the existence of any possibly-undefined variable? Example: window.outerHeight is defined by some browsers, but not...
9
by: Max | last post by:
I'm new with Javascript and can't seem to figure out what I'm doing wrong here as I'm not able to pass a simple variable to a function. In the head of doc I have: <script...
2
by: Bob Quintal | last post by:
Using Access 97, with the runtime parameter. I have a an application that needs a filter by form replacement, because that is unimplemented in runtime mode. I call the filter parameters form...
7
by: Just Me | last post by:
I have a project that contains a usercontrol, some forms and a module. The only thing in the module is one variable that is there so that it can be used by the control and all the forms. ...
3
by: Vijay | last post by:
I have 2 forms. in form1 i declared a public variable in which a value is stored. then i goto form2 . (for this is create an object of form2 in form1.) when i go to form2 there is call form1. when...
11
by: Rob | last post by:
I know, I know, don't use frames. Well, I'm stuck with these frames and I'm trying to add functionality without a complete redsign. You can look at this as a nostalgic journey. Anyway, I've got...
1
by: Simon | last post by:
Dear reader, My AddNewRecord procedure looks as follows: Private Sub AddNewRecord (strFormName As String) On Error GoTo ErrorHandler:
3
by: rGenius | last post by:
hi all, im planning to create a lookup usercontrol where i need to specify a custom property (a form) to be loaded up once a button is pressed. usercontrol contains: textbox button now...
6
by: viperRider | last post by:
Another question to people smarter than me! LOL I have a form with a variable - Dim cboOriginator as TextBox - that holds the info about a textbox i click on. when i click on it, i have a...
1
by: Autostrad | last post by:
Below; I have 2 classes (forms), Namely, "PrintOneEmployee" and "DeleteOne". In class "PrintOneEmployee"; I want to declare a variable with name of "fileName" to contain the name of the file...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.