• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 614
  • Last Modified:

VB.Net - Passing Parameter to Main Form



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
chokka
Asked:
chokka
  • 4
  • 2
  • 2
  • +2
1 Solution
 
himanshutCommented:
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
 
ZhaolaiCommented:
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
 
John (Yiannis) ToutountzoglouInstructor Multiengine PilotCommented:
hi....use Public Variables...
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
John (Yiannis) ToutountzoglouInstructor Multiengine PilotCommented:
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
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
"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
 
chokkaAuthor Commented:
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
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
 
chokkaAuthor Commented:
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
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

  • 4
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now