Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / VB.NET / March 2008

Tip: Looking for answers? Try searching our database.

passing value of variable from one form to another

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Stephen - 05 Mar 2008 17:47 GMT
How do I pass the value of variables created in Form1 to similar named
variables in a second form?

Thanks
Steve
Trammel - 05 Mar 2008 18:30 GMT
> How do I pass the value of variables created in Form1 to similar named
> variables in a second form?
>
> Thanks
> Steve

"Guessing" as I've not typed it in to check:

Declare variables as "public" in say frm1.  Then to pass something from frm2
to frm1:  frm1.myVar = 42
SurturZ - 05 Mar 2008 23:13 GMT
Well done for asking this, it is a tricky one for new programmers to .NET,
and I don't think Microsoft have ever really articulated how they thought
this "should" be done.

By default a .NET program will shut down if the first form closes, so what I
do is create an instance of the second form from the first form. You can then
either hand the variables through the constructor for the second form, or
simply pass them as Public variables.

The following code shows both methods. Make a new windows form project with
two forms, each with a button, and add the following code:

Public Class Form1

   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
       Using f As New Form2("Hello")
           f.PublicVar = "World"
           f.ShowDialog()
       End Using
   End Sub
End Class

Public Class Form2

   Public PublicVar As String

   Private mstrVar1 As String

   Sub New(ByVal var1 As String)

       ' This call is required by the Windows Form Designer.
       InitializeComponent()

       ' Add any initialization after the InitializeComponent() call.
       mstrVar1 = var1
   End Sub

   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
       MsgBox("mstrvar1=" & mstrVar1 & "; PublicVar=" & Me.PublicVar)
   End Sub
End Class

Signature

David Streeter
Synchrotech Software
Sydney Australia

> How do I pass the value of variables created in Form1 to similar named
> variables in a second form?
>
> Thanks
> Steve

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.