473,398 Members | 2,403 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,398 software developers and data experts.

How to Pass Variable Values Between Forms?

I have 2 forms that I need to go back and forth on a regular basis.

I am using Hide/Show to only display one form at a time.

If Form1 puts a value in a variable, then I Hide Form1 and Show Form2, how can I move the value to Form2 so Form2 can display and change it?

Basically, how do I define a sub that runs each time its Form is Shown?
Jul 27 '07 #1
8 6080
Expand|Select|Wrap|Line Numbers
  1. Public Class Form1   
  2.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  3.         Dim form As New Form2
  4.         form.Show()
  5.         Me.Label1.Text = form.Label1.Text
  6.     End Sub
  7. End Class
  8.  
  9. Public Class Form2
  10.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  11.         Dim form As New Form1
  12.         form.Show()
  13.         Me.Label1.Text = form.Label1.Text
  14.     End Sub
  15. End Class
The forms keep the values of the variables, accessing them is therefore easy. Put 2 forms in a project and paste the code behind each form. You should be able to see what happens to the variables. Note that the forms are not destroyed so that you can create many instances of the forms.
Good luck.
Jul 27 '07 #2
Thank you! I will give this a try.

Is there a way to pass the information without the need of a button click or other user action?

For example, on Form1 the variable: username="Dan". When I Show Form2, how can I have a label's text show the value of the username variable ("Dan")?
Jul 27 '07 #3
kadghar
1,295 Expert 1GB
Thank you! I will give this a try.

Is there a way to pass the information without the need of a button click or other user action?

For example, on Form1 the variable: username="Dan". When I Show Form2, how can I have a label's text show the value of the username variable ("Dan")?
use a variable in a module and make a function to call and a sub to write.
e.g.:

Expand|Select|Wrap|Line Numbers
  1. '(in a module)
  2. option explicit
  3. dim Str1 as string
  4.  
  5. sub WriteFn(wStr1 as string) 
  6.   str1 = wstr1
  7. end sub
  8.  
  9. public function ReadFn() as string
  10.   readfn = str1
  11. end function
  12.  
  13. '(in Form1)
  14. sub command1_click()
  15. call writefn(textbox1.text)
  16. end sub
  17.  
  18. '(in Form2)
  19. sub form2_load() 'or initialize, or watever
  20. label1.caption = readfn
  21. end sub
HTH
Jul 27 '07 #4
I like the idea of writing it to a file.

From what I've seen, the Load contents are only executed the first time a Form is Shown. If I bounce back and forth between Forms, the Load contents do not get executed each time the Form is Shown.

Is there a simple way to have a section of code run each time a Form is shown that does not involve a user action?
Jul 27 '07 #5
kadghar
1,295 Expert 1GB
I like the idea of writing it to a file.

From what I've seen, the Load contents are only executed the first time a Form is Shown. If I bounce back and forth between Forms, the Load contents do not get executed each time the Form is Shown.

Is there a simple way to have a section of code run each time a Form is shown that does not involve a user action?
use the Load, or the Initialize sub in the form, that actions are done when the sub is initialized or loaded. (it vary with the version of vb, but you should be able to find which sub fits you)
Jul 27 '07 #6
The problem I've had with "Load" is it only gets performed the first time the Form is Shown. Each time I re-Show the Form, it isn't performed.

Does Initialize get performed every time a Form is Shown?
Jul 28 '07 #7
I'm a little confused. I would have thought that running code everytime a Form is Shown would be a casual everyday thing to do.

Yet, everything involved with this seems to be a major hassle.

Any ideas?
Jul 29 '07 #8
Got It!

I called a friend who is a programmer.

To get code to run each time a Form is Shown, put it inside its "Activate" event.

Many thanks to everyone who replied!
Jul 29 '07 #9

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

Similar topics

12
by: Casey | last post by:
Yeah, I know this question was asked by someone elselike 2 weeks ago. But I need some additional help. I have a program I'm developing, and multiple different forms will be opened. For now though,...
7
by: Zlatko Matić | last post by:
Let's assume that we have a database on some SQL server (let it be MS SQL Server) and that we want to execute some parameterized query as a pass.through query. How can we pass parameters to the...
8
by: Brian F | last post by:
Exactly what the subject says. I have an ASP page that I want my C# windows application to open in Internet Explorer, and if possible have it send along a list of values from a list box. Thank you.
3
by: Hermit Dave | last post by:
Hi, Trying to pass a value to user control within a page. 1. Couldnt find a way to pass it from code behind file. 2. Trying to pass it from aspx page... using EditURL='<% "mypage.aspx?myvar=" +...
3
by: prabodhtiwari | last post by:
function submitPartsForm(str) { var count=document.getElementsByName("partId"); for(var i=0;i<count.length;i++) { document.mylist.myNum.value= document.getElementsByName("partNum")...
19
Atli
by: Atli | last post by:
Introduction At some point, all web developers will need to collect data from their users. In a dynamic web page, everything revolves around the users input, so knowing how to ask for and collect...
10
by: Robert Dailey | last post by:
Hi, I noticed in Python all function parameters seem to be passed by reference. This means that when I modify the value of a variable of a function, the value of the variable externally from the...
14
by: =?Utf-8?B?Umljaw==?= | last post by:
I have seen examples of passing data from FormB to FormA (Parent To Child) but I need an example of passind data from FormA to FormB. My main form is FormA; I will enter some data in textboxes and...
13
by: magickarle | last post by:
Hi, I got a pass-through query (that takes about 15 mins to process) I would like to integrate variables to it. IE: something simple: Select EmplID from empl_Lst where empl_lst.timestamp between...
12
by: raylopez99 | last post by:
Keywords: scope resolution, passing classes between parent and child forms, parameter constructor method, normal constructor, default constructor, forward reference, sharing classes between forms....
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.