473,403 Members | 2,284 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

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 9869
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

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

Similar topics

4
by: Jason Us | last post by:
Does anyone have experience with passing variables from an ASP page to a JSP page. The way it currently works in passing the SSN in the URL. This cannot be good. I thought that storing a...
2
by: Richard | last post by:
**** Post for FREE via your newsreader at post.usenet.com **** HI, I am working on a project where I need to input data to a (local) HTML page using multiple form elements, such as text,...
5
by: Jack | last post by:
Hi, I need to pass multple variables in a link in order to go to a asp page with the two varables. The following are the values of the variables using response.write: <%'Response.Write Mypage...
9
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...
5
by: Ross A. Finlayson | last post by:
Hi, I'm scratching together an Access database. The development box is Office 95, the deployment box Office 2003. So anyways I am griping about forms and global variables. Say for example...
39
by: Mike MacSween | last post by:
Just spent a happy 10 mins trying to understand a function I wrote sometime ago. Then remembered that arguments are passed by reference, by default. Does the fact that this slowed me down...
2
by: Michael C | last post by:
What's the best wat to pass data between two separate forms. For instance, Form1 invokes Form2 like this: Form z = new Form2(); z.Show(); How can I pass back strings and boolean values from...
8
by: Johnny | last post by:
I'm a rookie at C# and OO so please don't laugh! I have a form (fclsTaxCalculator) that contains a text box (tboxZipCode) containing a zip code. The user can enter a zip code in the text box and...
6
by: Scott Zabolotzky | last post by:
I'm trying to pass a custom object back and forth between forms. This custom object is pulled into the app using an external reference to an assembly DLL that was given to me by a co-worker. A...
7
by: The Doctor | last post by:
A rather elementary question, In VB5, how can I pass a variable from one form to another?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...
0
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...

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.