?
Solved

VB.Net - Passing Parameter to Main Form

Posted on 2010-09-12
10
Medium Priority
?
608 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 86

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 86

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 86

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 86

Accepted Solution

by:
Mike Tomlinson earned 2000 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

719 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