473,594 Members | 2,757 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.OK Only, "Title Of Box")
close()
EndSub
How do I Properly pass the variable to form2 ?
Thanks,

Miro


Aug 3 '06 #1
11 1776

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.OK Only, "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(ByVa l sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load
_text = "Hello"

Dim f2 As New Form2
Form2.Show()

End Sub

'form2

Private Sub Form2_Load(ByVa l sender As System.Object, ByVal e As
System.EventArg s) 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" <jmmgoalsteraty ahoodotcomwrote in message
news:B5******** *************** ***********@mic rosoft.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_drea ms" <sw**********@o 2.plwrote in message
news:11******** *************@7 5g2000cwc.googl egroups.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.OK Only, "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(ByVa l sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load
_text = "Hello"

Dim f2 As New Form2
Form2.Show()

End Sub

'form2

Private Sub Form2_Load(ByVa l sender As System.Object, ByVal e As
System.EventArg s) 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_drea ms" <sw**********@o 2.plwrote in message
news:11******** **************@ 75g2000cwc.goog legroups.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.SomeT ext)
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.OK Only, "Title Of Box")
close()
EndSub
How do I Properly pass the variable to form2 ?

Thanks,

Miro

Aug 4 '06 #10

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

Similar topics

4
4149
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 others. It would therefore seem prudent, before using this variable, to do something like: if (typeof (window.outerHeight) != "undefined") { do stuff that refers to this variable } else { work around the fact that the variable isn't defined }
9
2161
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 type="text/javascript"> function radioenable(value) { document.forms.search.elements.value.disabled=false;
2
2149
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 from several lookup forms. I set up the lookup forms to call the openform using me.name as the openargs.
7
1072
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. Couldn't I put the variable in the usercontrol and somehow set it so that it can be used by usercontrol and all the forms? If so, how?
3
1338
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 i come to form1 the value stored in the variable decalred in form1 gets lost. how can i retain those values?
11
16921
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 the following frame structure at the top level: FRAMESET CODE <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN"> <html><head><title>Server List</title></head> <frameset frameborder="1" border="1" framespacing="1" rows="10%,89%">
1
2009
by: Simon | last post by:
Dear reader, My AddNewRecord procedure looks as follows: Private Sub AddNewRecord (strFormName As String) On Error GoTo ErrorHandler:
3
1857
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 how can i create a variable in my usercontrol to list all forms in
6
10805
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 calendar set up to "become visible" and show either the date in the field i just clicked on or todays date if null. all of this worked fine when i had the date fields in the same table as the rest of the data; but now i have this info in a separate table...
1
2850
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 "Main\\_1Main.txt". Later on, when I wanted to access the variable "fileName" in the other class, namely, "DeleteOne". The compiler show an error that the variable is undeclared. What should I do to access the variable "fileName" in the the...
0
7947
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8255
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8010
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8242
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6665
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
3868
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2389
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1486
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1217
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.