|
-
Sep 12th, 2002, 06:56 AM
#1
Thread Starter
Hyperactive Member
very new to vb.net , how do you pass variables from form 1 to form 2
Hi
I am very new to vb.net , it def seems the wat to go though ! I will stick at it even though I am having problems with the most basic of things just now.
My first problem is passing a variable that has been declared on form1 to form 2 , even though I have decleared the variable as public on form1 form 2 will not let me use it.
How do I do this ?
Also my second problem is this
I wanted to loop through all the controls that are text boxes on a form and set them to enabled = false
I used to do this
dim mycontrol as control
for each mycontrol in me
If TypeOf mycontrol Is TextBox Then mycontrol.Locked = True
if TypeOf mycontrol Is ComboBox Then mycontrol.Locked = True
next
but .net objects to this and highlights the me word
how can I do this in vb.net without locking all controls one by one
many thx for your help
-
Sep 12th, 2002, 07:45 AM
#2
Frenzied Member
i have no idea
Magiaus
If I helped give me some points.
-
Sep 12th, 2002, 07:54 AM
#3
To use a public variable that is inside a class (a form is a class) you have to specify the instance that you are using.
For example:
Dim frmForm1 as New Form1
Dim frmForm2 as New Form2
You can then say Msgbox frmForm1.Variable
Unlike VB6 which creates you a form automatically the first time you call it, you have to explicitly create them.
Use this code for your loop
VB Code:
Dim ctrl As Control
For Each ctrl In Me.Controls
ctrl.Enabled = False
Next
-
Sep 12th, 2002, 07:58 AM
#4
Frenzied Member
To use a public variable that is inside a class (a form is a class) you have to specify the instance that you are using.
For example:
Dim frmForm1 as New Form1
Dim frmForm2 as New Form2
You can then say Msgbox frmForm1.Variable
Unlike VB6 which creates you a form automatically the first time you call it, you have to explicitly create them.
Use this code for your loop
visual basic code:--------------------------------------------------------------------------------
Dim ctrl As Control
For Each ctrl In Me.Controls
ctrl.Enabled = False
Next
--------------------------------------------------------------------------------
your correct but in large application using the collection is easier and much cleaner
Magiaus
If I helped give me some points.
-
Sep 12th, 2002, 08:00 AM
#5
Frenzied Member
Magiaus
If I helped give me some points.
-
Sep 12th, 2002, 08:05 AM
#6
When this guy gets up to that level im sure he will come back and ask you for more info .
-
Sep 12th, 2002, 08:08 AM
#7
Frenzied Member
umm
ok, but i wasn't trying to be a jerk or anything and you never know he might be writing god code and not telling us
Magiaus
If I helped give me some points.
-
Sep 12th, 2002, 08:33 AM
#8
Thread Starter
Hyperactive Member
thanks for replying guys
What was suggested works when passing variables and also the loop through the controls works now.
Although when I pass my array created on frmMain to frmaccounts
it appears to have lost all its value and data
I declared the array on frmMain
public arrnew(3) as string
filled it up with strings
then on frmaccounts did this in the frmaccounts load section
dim frmdata as new frmmain()
messagebox.show(frmdata.arrnew(1))
it brings up a message box with nowt in it , so I changed the line to
messagebox.show(frmdata.arrnew.length)
and it brings up 4 , how can this be when I have declared the array as (3) and why cant I see the data in side the array ?
thx
-
Sep 12th, 2002, 08:38 AM
#9
Frenzied Member
0, 1, 2, 3 an array is zero based so an array with three element would be Dim a(2) As String
Magiaus
If I helped give me some points.
-
Sep 12th, 2002, 08:40 AM
#10
Frenzied Member
back in vb6 we were allowed the bad practice of doing things like Dim a(4-7) As String which would be an array with three elements that started with element 4 but i think everyone started at 1 not 4 or 0
Magiaus
If I helped give me some points.
-
Sep 12th, 2002, 08:43 AM
#11
Thread Starter
Hyperactive Member
ahhhh IC
thx , i will get the hang of this eventually , lol
do you know why the data in the array has disappeared between the forms ?
-
Sep 12th, 2002, 10:03 AM
#12
Re: thanks for replying guys
Originally posted by locutus
public arrnew(3) as string
filled it up with strings
then on frmaccounts did this in the frmaccounts load section
dim frmdata as new frmmain()
messagebox.show(frmdata.arrnew(1))
it brings up
dim frmdata as new frmmain() << that creates a NEW instance, not the old one you filled with data.
You need to remember that EVERYTHING is a class, and to use it you create a new copy of it. I am guessing that you load frmMain as your first form, if so then .Net is creating your copy of it on your behalf.
There are a few ways of going from here, but tell me what you are trying to do, and Ill reformat it to work.
-
Sep 12th, 2002, 10:19 AM
#13
Thread Starter
Hyperactive Member
thx dude
Thanks for taking the time to reply
I have frmMain as the main form and frmAccounts as the second form.
I have declared the array as public on the frmmain and loaded it with data from a text file.
fsout = new system.io.filestream(filename,system.io.filemode.openorcreate,system.io.fileaccess.read)
oReader = new system.io.streamreader(fsout)
strContents = oReader.readtoend
arrnew = split(sContents,vbcrlf)
I now want to pass the array to my second form ( frmAccount) and load my list box on frmAccounts
lstboxAccounts
I want to use the array on the main form and the frmaccounts , you know pass the array around to use between forms. I think I can use a module to do this and declare it there as public as see it from everywhere but I really want to know what I am doing wrong in the previous example so I can learn and get the BIG picture.
Thanks for any help
-
Sep 12th, 2002, 10:56 AM
#14
OK, here we go.
When you start your program, .Net will create an instance of frmMain (lets call it FormA).
You then create a new instance of frmAccounts (lets call it FormB).
You need to call something like FormA.GimmeArray.
FormB does not know where FormA is, it needs a reference to it to be able to do anything with it.
----
This is where you are getting confused, with VB6 it does this.
When you start your program, VB6 will create an instance of frmMain, and actually calls it frmMain.
You call frmAccounts, and VB6 autocreates it, and again calls it frmAccounts.
You then can from frmAccounts call, frmMain.GimmeArray because you have a reference to it which is auto-handled by VB6.
----
Try this, on your frmMain, put a button and call this (im assuming you already do)
VB Code:
Private frmForm as frmMain 'dim this at the top of form
'----- Button event
frmForm = New frmMain()
frmForm.Text = "FORM1"
frmForm.Show()
frmForm = New frmMain()
frmForm.Text = "FORM2"
frmForm.Show()
frmForm = New frmMain()
frmForm.Text = "FORM3"
frmForm.Show()
After this code is ran, your private variable will have a reference to the last form titled "FORM3". You reference to FORM1 and FORM2 is gone, you can no longer access them.
If you want your two forms to talk to each other you need to do one of two things,
1. Put a public variable in both forms, and when you create the 2nd form, set each one to reference the other form.
e.g
sub procShowAccounts()
frmLinkToAccount = New frmAccounts
frmLinkToAccount.frmLinkToMain = Me
End sub
or 2. Have a module with 2 varaibles and fill them in when you load the forms.
e.g
frmMainLoadEvent
frmLinkToMain = Me
frmAccountsLoadEvent
frmLinkToAccount = Me
Then you can just call frmLinkToMain from anywhere and it will always give you the instance of the form you want. But remember this point, an object will not unload until all references to it are deleted. so when you close a form, remove all references e.g
frmLinkToAccount = Nothing
Good luck
-
Sep 12th, 2002, 01:44 PM
#15
Frenzied Member
i just had an interesting idea in the new method for your second form you could ask for an array or you could ask for a form ref to the main form the array would be better. you could go as far as to list out all the fields you need to populate in the new method.
VB Code:
Public Sub New(ByVal text1 As String, ByVal text2 As String, ByVal text3 As String)
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
TextBox1.Text = Text1
TextBox2.Text = text2
TextBox3.Text = text3
End Sub
this is probably a good way to go if you don't have allot of forms or data to manage
Magiaus
If I helped give me some points.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|