Solved

VB.Net - Passing Parameter to Main Form

Posted on 2010-09-12
10
562 Views
Last Modified: 2012-05-10


VS 2005 / VB.Net

I have Form1.Vb and Form2.Vb

Form1.Vb is the Application Start - Form.

To this form, i should can pass or cannot pass an Integer parameter ( Method Overloading )

i.e Startup Form, when it launch for the first time it should run without paramters .. at the same time on a click event from Form2 to Form1 .. it should pass an Integer Parameter ..


0
Comment
Question by:chokka
  • 4
  • 2
  • 2
  • +2
10 Comments
 
LVL 10

Expert Comment

by:himanshut
ID: 33659118
You need to create an object for form1 in form2 and then you will be able to access all the values/controls that are there in Form1.vb

see the example here:
http://www.vbdotnetheaven.com/UploadFile/thiagu304/passdata12262006073406AM/passdata.aspx

Cheers!~
0
 
LVL 17

Expert Comment

by:Zhaolai
ID: 33659382
Do something like this:

In Form1,

Public Class Form1    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click        Form2.ShowDialog()        Dim iNum As Integer = Form2.iNumber        Form2.Close()    End SubEnd Class

In Form2,

Public Class Form2
    Public iNumber As Integer
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        iNumber = 12345
        Me.Hide()
    End Sub
End Class

0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33659410
hi....use Public Variables...
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33659440
1.Create a module and name it as ShredVariables
2.Declare as many Variables you want in a classic Way
Public MyInteger as Integer
3.You can use then this Variable any time you want in any Form ..

Hope It Helps
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 33659500
"at the same time on a click event from Form2 to Form1"

Do you want to pass a value to the EXISTING instance of Form1 from Form2?
...or create a NEW instance of Form1 and pass the value to that?
0
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!

 

Author Comment

by:chokka
ID: 33663180
Little confused on all the solutions ..

Let me explain my scenario ..

Form1 - > Has approximately 40 TextBox Fields ..! In Form1 We are filling the Application Form .. Like entering MemberName, Address etc ..!

Form2 -> We have a GridView which has list of available members, suppose if i need to edit a certain Member - I need to pass MemberId on Click event from Form2 to Form1.


Also in the Primary Scenario ..

Form1 has to run or load without any values for allowing new entries ..!

So technically Form1 should have Method Overloading Feature ?

As Form1 is the Startup form, i am confused on implementing the Method Overloading
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 33663239
So Form1 is the startup form and shows Form2.  You can simply use a CUSTOM event that broadcasts the MemberID to Form1 (which subscribes to the event).

Simplified example with just a TextBox and a Button on Form2.  The value is placed into a Label on Form1:
Public Class Form1



    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim f2 As New Form2

        AddHandler f2.MemberIDSelected, AddressOf f2_MemberIDSelected

        f2.Show()

    End Sub



    Private Sub f2_MemberIDSelected(ByVal MemberID As String)

        Label1.Text = MemberID

    End Sub



End Class



Public Class Form2



    Public Event MemberIDSelected(ByVal MemberID As String)



    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        RaiseEvent MemberIDSelected(TextBox1.Text)

    End Sub



End Class

Open in new window

0
 

Author Comment

by:chokka
ID: 33663314
Idle Mind ..

What i am expecting is


For example :-

In Form1

Public sub Test1()

End sub


Public sub Test1(ByVal iMemberId as Integer)

End sub

--- From Form 2

On GridView click event

// Here number 39 is a MemberId which is passed from Form2 through Click Event
Test1(39)
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 33663319
If Form2 stays open until a value is selected and then closed...AND Form1 should not be able to be used until the value has been selected then you have a "modal dialog" situation and it could be handled differently.

In that scenario you would simply display Form2 using ShowDialog() which would cause Form1 to STOP execution and be unreachable until Form2 is closed.  In Form2 you set the DialogResult property to "OK" or "Cancel" based on user action.  Once this is done, execution returns to Form1 and you simply directly extract the selected value from Form2.

Here's the same simplified example from above using this approach:
Public Class Form1



    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim f2 As New Form2

        If f2.ShowDialog = Windows.Forms.DialogResult.OK Then

            Label1.Text = f2.TextBox1.Text

        End If

    End Sub



End Class



Public Class Form2



    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click

        Me.DialogResult = Windows.Forms.DialogResult.OK

    End Sub



    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click

        Me.DialogResult = Windows.Forms.DialogResult.Cancel

    End Sub



End Class

Open in new window

0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 33663355
Ok...so using the first approach you would simply make the custom event match the "signature" of your Test1() method and then wire up that method in the AddHandler() line:

        AddHandler f2.MemberIDSelected, AddressOf Test1

Now Test1() will execute and be passed the selected iMemberID when the event fires.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

762 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

17 Experts available now in Live!

Get 1:1 Help Now