Solved

Passing parameters to forms in VB.Net

Posted on 2003-10-30
10
641 Views
Last Modified: 2008-01-09
I have a form (Form2) that I want to pass ByRef (from Form1) a parameter to and have the form (Form2) modify the parameter.  Is this possible???  Below is my own small script of the action (it does not represent the actual code).

Thanks in advance for any help,

Hammer



' Declare Form1
Form1

Dim strName As String

strName = "John"

Dim NewForm2 As New Form2(strName)

' strName should have changed from "John" to "Charley"

End Form1


' Declare Form2
Form2(ByRef strPrmNewName)

strPrmNewName = "Charley"

End Form2
0
Comment
Question by:HAMMER33333
  • 4
  • 3
  • 3
10 Comments
 
LVL 48

Expert Comment

by:Mikal613
ID: 9653285
yEAH JUST IN fORM 2

hAVE A Procedure called New()

And design as many parameters as needed

like

new(Blas as blah,Blas as blah)

And when you do AS new Form2(  you will get the parameters
0
 

Author Comment

by:HAMMER33333
ID: 9653363
How about this if Form2:

Public Sub New(ByRef strPrmNewName As String)
MyBase.New()

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

  'Add any initialization after the InitializeComponent() call

End Sub


on Form2 I have a button that calls a function that will update "strPrmNewName" so that when the form closes the ByRef setting will modify the parameter passed.  Is this possible???   I cannot seem to access "strPrmNewName" outside the "Sub New" and update it to something.

Thanks again,

Hammer
0
 
LVL 48

Expert Comment

by:Mikal613
ID: 9653409
so make a global Variable and then


Dim g_strPrmNewName  as String

New(strPrmNewName  as string)

g_strPrmNewName  =  strPrmNewName

end sub


Public sub whatever

  MSGBOX     G_strPrmNewName
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:HAMMER33333
ID: 9653458
But the parameter was passed ByRef so how do you get the value passed from Form1 to change?   In the above example you are setting it to a global variable which is local to Form2 (Form1 does not know about it).  So g_strPrmNewName is getting set to "strPrmNewName" but "Form1.strName" which is passed is not.  Do you see what I mean?

I want to:

1. Pass a variable from Form1 to Form2

2. Modify the passed variable from Form1 to something new using a function in Form2.

3. The result will be that the Form1 variable will change after a call to Form2.

I don't even know if this is possible.

Sorry for not being clear,
Thanks for the help,

Hammer
0
 
LVL 48

Expert Comment

by:Mikal613
ID: 9653484
USe ByVal not ByREf
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 9658341
Define a property and a module-level string as a holder:

rivate module_strName As String
   Public Property strName() As String
      Get
         Return module_strName
      End Get
      Set(ByVal Value As String)
         module_strName = Value
      End Set
   End Property
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 9658353
Define a property and a module-level string as a holder:

Private module_strName As String

Public Property strName() As String
   Get
      Return module_strName
   End Get
   Set(ByVal Value As String)
      module_strName = Value
   End Set
End Property

In your code you can change the value of module_strName, and when accessed from outside the form through the property, the modified value will be returned to the caller.
0
 

Author Comment

by:HAMMER33333
ID: 9658477
TheLearnedOne,

I am still a Grasshopper.  How can I apply this to my Form2 call?  As in:

' Declare Form1
Form1

Dim strName As String

strName = "John"

Dim NewForm2 As New Form2(strName)
NewForm2.ShowDialog()

' strName should change from "John" to "Charley" if the user clicks the button in Form2

End Form1


' Declare Form2
Form2

Public Sub New(ByRef strPrmNewName As String)
MyBase.New()

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

  'Add any initialization after the InitializeComponent() call

End Sub

Sub btnSetValue_Click

strPrmNewName = "Charley"
' "strPrmNewName" is not know here in this subroutine

Me.Close

End Sub

End Form2


Thanks a bunch,
I believe we are getting close,

Hammer
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 20 total points
ID: 9658555
Okay, grasshopper.  Lesson #1:  Using the New constructor to set a variable is basically a one-way operation.  The code to create the new form sets the value.  Then there is nothing on the other end to receive the change, because the change to the value is after the form is initialized, and the New is no longer in the picture.

Lesson #2:  Unlike VB6, where the form was almost like a class, the form in VB.NET is exactly like a class, so you can add properties like you can in a Class definition.  In this way you can transfer values between two objects (Form1 and Form2).  Form2 can modify the internal value, and when Form1 asks for the value from Form2, then the modified value will be returned.

' Declare Form1
Form1

Dim strName As String

strName = "John"

Dim NewForm2 As New Form2()
NewForm2.strName = strName
NewForm2.ShowDialog()

' strName should change from "John" to "Charley" if the user clicks the button in Form2

End Form1


' Declare Form2
Form2

Private module_strName As String

Public Property strName() As String
   Get
      Return module_strName
   End Get
   Set(ByVal Value As String)
      module_strName = Value
   End Set
End Property

Sub btnSetValue_Click

   module_strName = "Charley"

Me.Close

End Sub

End Form2
0
 

Author Comment

by:HAMMER33333
ID: 9658686
TheLearnedOne,

       It works!   Except I had to add the "*" code to the Form1 call of Form2 below.  It works just like I wanted.

       You da man!!!

Thanks very much,

Hammer



Form1

        MsgBox("Before: " & strName)

        Dim NewForm2 As New Form2()
        NewForm2.strName = strName
        NewForm2.ShowDialog()
 *     If NewForm2.strName <> strName Then
 *         ' Set it only if it was modified
 *         strName = NewForm2.strName
 *     End If

        MsgBox("After: " & strName)

End Form1
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

773 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