Results 1 to 15 of 15

Thread: very new to vb.net , how do you pass variables from form 1 to form 2

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 1999
    Location
    UK
    Posts
    300

    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
    Resistance is futile

  2. #2
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267

    Cool i have no idea

    Magiaus

    If I helped give me some points.

  3. #3
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780
    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:
    1. Dim ctrl As Control
    2.  
    3.         For Each ctrl In Me.Controls
    4.             ctrl.Enabled = False
    5.         Next

  4. #4
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267
    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.

  5. #5
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267
    btw resistance is futile
    Magiaus

    If I helped give me some points.

  6. #6
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780
    When this guy gets up to that level im sure he will come back and ask you for more info .

  7. #7
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267

    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.

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 1999
    Location
    UK
    Posts
    300

    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
    Resistance is futile

  9. #9
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267
    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.

  10. #10
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267
    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.

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 1999
    Location
    UK
    Posts
    300

    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 ?
    Resistance is futile

  12. #12
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780

    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.

  13. #13

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 1999
    Location
    UK
    Posts
    300

    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
    Resistance is futile

  14. #14
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780
    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:
    1. Private frmForm as frmMain  'dim this at the top of form
    2.  
    3.  
    4. '----- Button event
    5.  
    6.         frmForm = New frmMain()
    7.         frmForm.Text = "FORM1"
    8.         frmForm.Show()
    9.  
    10.         frmForm = New frmMain()
    11.         frmForm.Text = "FORM2"
    12.         frmForm.Show()
    13.  
    14.         frmForm = New frmMain()
    15.         frmForm.Text = "FORM3"
    16.         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

  15. #15
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267
    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:
    1. Public Sub New(ByVal text1 As String, ByVal text2 As String, ByVal text3 As String)
    2.         MyBase.New()
    3.  
    4.         'This call is required by the Windows Form Designer.
    5.         InitializeComponent()
    6.  
    7.         'Add any initialization after the InitializeComponent() call
    8.         TextBox1.Text = Text1
    9.         TextBox2.Text = text2
    10.         TextBox3.Text = text3
    11.     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
  •  



Click Here to Expand Forum to Full Width