Passing string parameter from Form1 to Form2

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.

**BEGIN**
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
   Get
       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 :-)
**END**


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!
TSFLLCAsked:
Who is Participating?
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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)
        f2.Show()
    End Sub

End Class



Public Class Form2

    Private data As String

    Public Sub New(ByVal dataIn As String)
        InitializeComponent()
        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
0
 
TSFLLCAuthor Commented:
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?
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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?
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

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

So
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
    MessageBox.Show(Me._PassedValue)
  End Sub
End Class


HTH
~BC
0
 
bchoorCommented:
oops - I didn't realize y'all figured it out already - I guess I had it open too long without refreshing...LOL!!!
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
It's no biggie bchoor...I do it often as well.  =)
0
 
TSFLLCAuthor Commented:
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
records.


0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.