passing variables between forms in vb.net 2005

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • captainphoenix
    New Member
    • Mar 2007
    • 22

    passing variables between forms in vb.net 2005

    I have VB2005 and I need to pass intnumplayers from form1 to form2. Simply declaring the variable in a global module WILL NOT SOLVE MY PROBLEM, as I need to pass the values themselves from form1 to form2. Specifically, I have a textbox that acts to input the value for the number of players on a soccer team in form1. I did the necessary code to make sure it's a positive integer, so that code is not necessary. However, after that info (among other bits of info) is gathered, I want to use it in an array in form2, but the value is stored in the private class for the button that gathers the values from form1 (btnNext). Also, I have VB2005, and I noticed that using the code form1.intnumpla yers DOES NOT WORK. How do I get the fact that there are, say, 22 players on the soccer team from btnNext in form1 to form2?
  • Lazareth
    New Member
    • Jan 2007
    • 29

    #2
    Hi,


    I'm not sure if this is the easiest way to do this, but you could just access the textbox data directly.

    I'm assuming btnnext opens a new form, if so you could do this...

    Code:
     [size=2][color=#0000ff]
    Private[/color][/size][size=2] [/size][size=2][color=#0000ff]Sub[/color][/size][size=2] Form2_Load([/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] sender [/size][size=2][color=#0000ff]As[/color][/size][size=2] System.Object, [/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] e [/size][size=2][color=#0000ff]As[/color][/size][size=2] System.EventArgs) [/size][size=2][color=#0000ff]Handles[/color][/size][size=2] [/size][size=2][color=#0000ff]MyBase[/color][/size][size=2].Load
     
    TextBox1.Text = Form1.txtTop.Text
     
    [/size][size=2][color=#0000ff]End[/color][/size][size=2] [/size][size=2][color=#0000ff]Sub
     
    [/color][/size]

    Comment

    • Lazareth
      New Member
      • Jan 2007
      • 29

      #3
      Okay.


      Sorry first time I have used the code input, not sure what happened there but basically add

      TextBox1.Text = Form1.txtTop.Te xt

      on form load event or whatever you're text boxes are called

      Comment

      • captainphoenix
        New Member
        • Mar 2007
        • 22

        #4
        No, I used that info to determine the the size of an array. Now I need to get the values of the arrays from one button to another. I'm bailing on the two form thing. That was too much work and an awful mess. I'm keeping everything to one form using visible/hide operations, but I need to pass the info gathered from btnA to btnB. For example, stringvariableA (x) was assigned a value of "blah" in btnA via inputboxes. Now I need to transfer the values of stringvariableA (x) (which is "blah") from btnA to btnB without repopping up the inputboxes, thereby preventing the user from having to repeatedly type in the data.

        Comment

        • Killer42
          Recognized Expert Expert
          • Oct 2006
          • 8429

          #5
          It's not clear what you mean about transferring values to or from a button. The button is merely an interface element used to get input from the user, in the form of a "click". Are you trying to display this info as the caption on the button?

          Can you explain further?

          Comment

          • captainphoenix
            New Member
            • Mar 2007
            • 22

            #6
            No. I mean I have one button that gathers all the data and stores it in the necessary variables. I.E. when you click on the button, inputboxes pop up and ask for the values of different parts of the array. Once all the information is gathered, the information is outputted onto a listbox. Under the listbox is a new set of buttons designed to search for values and edit them, as well as print the form and close the application. However, when I tell it to search for, say, "John Doe" in the array, VB can't find a "john doe" because that was assigned to str1 in btn1, and the value of str1 is now "". I need it to stay as "John Doe". Specifically:

            The form opens up with three labeled textboxes and two buttons designed to gather information regarding a hypothetical soccer team. One textbox asks for the team name, the second the number of players on the team, and the last the number of games they play. When the user types in proper values for each and clicks the button btnNext (text "Next ->"), the form hides and inputboxes appear asking for the number of goals each player scored in each game, as well as each players' name and the date of each game (that's 2 1d arrays and 1 2d array). Once all this is done, the form reappears, resized, and a listbox appears displaying the scoring information for the team with a spreadsheet look: the team's name becomes the heading, and the dates and player names assume the labels of the columns and rows, respectively, with the game scoring info filling in the "spreadshee t", as well as total scoring for each game and player and the team total for the season. Underneath the listbox are several new buttons, including one to search for a player's stats, one to search for the team's stats on a given date, one to edit the scoring/player info, one to print the form, and one to close the application.

            The problem is that the arrays are stored in the private subroutine for btnNext; the private subroutine btnsearchname has none of the array's values. I realize I could cut and paste the code to each of these buttons and just simply ask for all the scoring information all over again, but that doesn't seem very user-friendly. I assume there HAS to be code to call the numerical values themselves from one subroutine to another, but I obviously don't know what it is. This is what I am asking for.

            Comment

            • Killer42
              Recognized Expert Expert
              • Oct 2006
              • 8429

              #7
              Sorry, in a real rush right now.

              But it sounds as though you need to define the arrays at the form level, not within the button's event procedure.

              Comment

              • captainphoenix
                New Member
                • Mar 2007
                • 22

                #8
                ok totally different project, but same problem. for some reason the project refuses to pass variables from one form to another, and this project is huge so i have to use multiple forms. once again, simply declaring the variables at the global level WILL NOT WORK. I know that you can cheat the system and do like form.control.te xt, but in this case that's not going to work (way too many variables, way too many forms, way too much code, and some of them are in array format).

                Comment

                • Killer42
                  Recognized Expert Expert
                  • Oct 2006
                  • 8429

                  #9
                  Ok, for starters, why don't globals fit the bill?

                  Comment

                  • Killer42
                    Recognized Expert Expert
                    • Oct 2006
                    • 8429

                    #10
                    Hello? Earth to captainphoenix. ..

                    Comment

                    • QVeen72
                      Recognized Expert Top Contributor
                      • Oct 2006
                      • 1445

                      #11
                      Hi All,

                      This is for people who are stuck with passing control values between forms in VB.Net. Unlike VB6, we cannot access form control variables using Form1.Text1.Tex t.
                      Easy way out is keeping global variables. But if you have too many variables to pass, overhead simply increases.

                      In VB.net, forms are classes. You need to do a bit of work around.
                      Say you have Form1 as parent form and you are calling child for Form2,
                      and you are wanting to show some textbox values of Form1 in Form2.

                      In Form2 (child) add a "New" constructor. Something like this :
                      [code=vbnet]
                      ' Declare a Form Level Variable and New Constructor
                      Dim ParForm As Form1
                      Public Sub New(ByVal myParForm As Form1)
                      MyBase.New()
                      ParForm = myParForm
                      InitializeCompo nent()
                      End Sub
                      ' In Form Load, Show the Contents of Form1
                      Private Sub Form2_Load(ByVa l sender As System.Object, ByVal e As System.EventArg s) Handles MyBase.Load
                      Me.TextBox1.Tex t = ParForm.TextBox 1.Text
                      Me.TextBox2.Tex t = ParForm.TextBox 2.Text
                      Me.TextBox3.Tex t = ParForm.TextBox 3.Text
                      Me.TextBox4.Tex t = ParForm.TextBox 4.Text
                      Me.PictureBox1. Image = ParForm.Picture Box1.Image
                      End Sub
                      [/code]

                      And in parent form (Form1), to show child Form2 :

                      [code=vbnet]
                      Dim TForm As New Form2(Me)
                      TForm.Show()
                      [/code]

                      Hope it helps.

                      Regards
                      Veena
                      Last edited by Killer42; Dec 13 '07, 01:40 AM.

                      Comment

                      Working...