Solved

VB.Net - Passing Parameter to Main Form

Posted on 2010-09-12
10
586 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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
 

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

821 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