Solved

Passing parameters to forms in VB.Net

Posted on 2003-10-30
10
639 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
 

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video discusses moving either the default database or any database to a new volume.

747 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now