[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


Passing string parameter from Form1 to Form2

Posted on 2006-04-18
Medium Priority
Last Modified: 2012-08-14
I found this piece of code on the forum that apparently worked for someone.  But it's confusing to me.
I want to pass a string from say Form1 to Form2.

Another option is to create a Class and store the variable there.  I have done that for an application I created and it works well.  For example:

Imports System

Public Class Global (whatever you want to call it)

Private Shared EmpID as String

Public Shared Property EmpIDVal as String
       Return EmpID
   End Get
   Set(ByVal Value As String)
       EmpID = Value
   End Set
End Property

To use in a form, send the value to this class by saying global.EmpIDVal = txtEmpID.Text.

In order to get the value in a form, use the following code:
Public Function ID() As String
        Return Global.EmpIDVal
    End Function

Hope this helps :-)

The generated code from my Form2 looks like this:

    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    End Sub

Are you able to use the 'Dim frm AS New Form2(text_value)' and modify the Form2_Load() above to accept this
text_value.  Or do you have to balloon across the Atlantic to do this....using code similar to the code I pasted
at the top.  This is a function that will be necessary for me in about (20) forms.  If it means setting up a Class
or something else that can be used globally..I'm all for it.  Just the same I've never created a separate Class
nor do I know where it would go.

My Module1.vb contains public variables as follows:

Imports System.Data
Imports System.Data.SqlClient

Module Module1
     Public this and that
End Module

If it can be added here...where do I add it.
Otherwise where and how would I add a Class to be used globally through the application and then how
would I suscessfully use it?

Thanks in advance!
Question by:TSFLLC
  • 3
  • 2
  • 2

Author Comment

ID: 16483405
I did get the create new Class code above to work.

However, what is more resourceful...or should I say efficient?

Create the class I included or just create a Global variable in Module1 and access it throughout the program?
LVL 86

Accepted Solution

Mike Tomlinson earned 2000 total points
ID: 16483424
It seems like you want to pass the string from Form1 to Form2 when you create Form2.

If so, then...

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim data As String = "data to pass to Form2"
        Dim f2 As New Form2(data)
    End Sub

End Class

Public Class Form2

    Private data As String

    Public Sub New(ByVal dataIn As String)
        Me.data = dataIn
    End Sub

    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        MessageBox.Show(Me.data, "Data Passed In", MessageBoxButtons.OK, MessageBoxIcon.Information)
    End Sub

End Class
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 16483427
What is more efficient?

That really depends on what you're doing...

Will you have more than one instance of Form2 that will require a different value to be passed to each?
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

LVL 10

Expert Comment

ID: 16483444
I can think of 2 quick ways you could do it
1. Using your Module1 to store the value as a global variable

Module Module1
  Public EmpID As String
End Module

In Form 1
Module1.EmpID = "1234"
' then Open Form 2

In Form 2
Dim EmpID As String = Module1.EmpID

2. Create an overrload in your form2 to accept the value as an argument

In Form2
Private _EmpID As String
Public Sub New(EmpID As String)
  _EmpId = EmpID
End Sub

_EmpId now stores the value

In Form1
Dim frm2 as New Form2("1234")

note: If you plan on using the 2nd approach - it'll add value to create a base form that includes this overload. Then Form2 will inherit from this form as follows

Class myBaseForm
  inherits Form
  Private _PassedValue As String
  Public Sub New (Value As String)
    _PassedValue = Value
  End Sub
End Class

Then in the form
Class Form2
  inherits myBaseForm

  Public Sub Click(..., ...) handles cmd.Click
  End Sub
End Class

LVL 10

Expert Comment

ID: 16483448
oops - I didn't realize y'all figured it out already - I guess I had it open too long without refreshing...LOL!!!
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 16483461
It's no biggie bchoor...I do it often as well.  =)

Author Comment

ID: 16483739
Mike(Idle Mind),

I used your code and it works well.  Enjoy the points...

I'm assuming that I would need to add the 'Public Sub New()' code in each subsequent form
that requires the value to be passed?

What's more efficient...more instances of Form2 that require different values?

Yes, the value will be different for each form.  It's a unique record id for each table I store.
That's how I determine the record to display in all of my data entry forms.
I include this record id as the 1st column with width = 0 from a datagrid that contains table


Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses
Course of the Month19 days, 1 hour left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question