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

x
?
Solved

Passing string parameter from Form1 to Form2

Posted on 2006-04-18
7
Medium Priority
?
431 Views
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.

**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!
0
Comment
Question by:TSFLLC
  • 3
  • 2
  • 2
7 Comments
 

Author Comment

by:TSFLLC
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?
0
 
LVL 86

Accepted Solution

by:
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)
        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
 
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?
0
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

by:bchoor
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

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
 
LVL 10

Expert Comment

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

Expert Comment

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

Author Comment

by:TSFLLC
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
records.


0

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