469,271 Members | 1,484 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,271 developers. It's quick & easy.

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.intnumplayers 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?
May 12 '07 #1
10 9580
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...

Expand|Select|Wrap|Line Numbers
  1.  [size=2][color=#0000ff]
  2. 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
  3.  
  4. TextBox1.Text = Form1.txtTop.Text
  5.  
  6. [/size][size=2][color=#0000ff]End[/color][/size][size=2] [/size][size=2][color=#0000ff]Sub
  7.  
  8. [/color][/size]
May 13 '07 #2
Okay.


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

TextBox1.Text = Form1.txtTop.Text

on form load event or whatever you're text boxes are called
May 13 '07 #3
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.
May 13 '07 #4
Killer42
8,435 Expert 8TB
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?
May 14 '07 #5
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 "spreadsheet", 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.
May 14 '07 #6
Killer42
8,435 Expert 8TB
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.
May 14 '07 #7
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.text, 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).
Dec 3 '07 #8
Killer42
8,435 Expert 8TB
Ok, for starters, why don't globals fit the bill?
Dec 9 '07 #9
Killer42
8,435 Expert 8TB
Hello? Earth to captainphoenix...
Dec 12 '07 #10
QVeen72
1,445 Expert 1GB
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.Text.
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 :
Expand|Select|Wrap|Line Numbers
  1. ' Declare a Form Level Variable and New Constructor
  2.     Dim ParForm As Form1
  3.     Public Sub New(ByVal myParForm As Form1)
  4.         MyBase.New()
  5.         ParForm = myParForm
  6.         InitializeComponent()
  7.     End Sub
  8.     ' In Form Load, Show the Contents of Form1
  9.     Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  10.         Me.TextBox1.Text = ParForm.TextBox1.Text
  11.         Me.TextBox2.Text = ParForm.TextBox2.Text
  12.         Me.TextBox3.Text = ParForm.TextBox3.Text
  13.         Me.TextBox4.Text = ParForm.TextBox4.Text
  14.         Me.PictureBox1.Image = ParForm.PictureBox1.Image
  15.     End Sub
  16.  
And in parent form (Form1), to show child Form2 :

Expand|Select|Wrap|Line Numbers
  1.     Dim TForm As New Form2(Me)
  2.     TForm.Show()
  3.  
Hope it helps.

Regards
Veena
Dec 12 '07 #11

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Richard | last post: by
9 posts views Thread by Max | last post: by
2 posts views Thread by Michael C | last post: by
6 posts views Thread by Scott Zabolotzky | last post: by
7 posts views Thread by The Doctor | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.