How do I pass a value from a VB.NET Win Form dialog box back to the calling procedeure,

griller
griller used Ask the Experts™
on
I have a Win Forms application application in VB.NETVisual Studio 2008.

There is a form with a Datagrid view of data on it and a button.

The idea is that:

1) The dialog pops up when a selection is made on the main form.
2) the dialog has a data grid view on it which displays rows of relevant records
3) The user selects the row they are interested in
4) The user selects the OK button on the dialog
5) the dialog closes and retuens focus to the main form .

What I want to know is how do I get the dialog to pass back the key value of the selected row when the dialog closes.

I have included the simple code to date for open and close.

Thanks for your help







the user selects the row in the data grid that they are interested in. Then they click the dialog fom's OK button. At this point the dialog closes - but I want the dialog to pass back the key value from the selected row in the data grid view to the calling procedure.





When the dialog is closed I want the value selected in the dialog
'The Dialog form is opened from the main form

Private Sub mnubtnList_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnubtnList.Click
        'Open the search list - showing all of the available contact records

       Dim frm As New frmSearchContacts()
        frm.ShowDialog()

        'Some code goes here to collect the value o the closed dialog box !!!

End Sub


'The dioalog is closed once a user has selected a value


Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.OK
        Me.Close()
    End Sub

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2005
Commented:
Create a public property in the frmSearchContacts form which contains the value you need to return.

Once the form is closed, read the property:

Dim frm As New frmSearchContacts()
frm.ShowDialog()

IF dialogresult = OK
    x = frm.SomeProperyValue

Carl TawnSenior Systems and Integration Developer

Commented:
The simplest method would be to have a property on your form that exposes the selected value that you set from the gridview:


    Public Property SelectedValue As Integer
        Get
            Return <Value from your grid>
        End Get
    End Property

Open in new window

Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009

Commented:
Totally agree with Erick and carl!

Also, the Close() call in your OK_Button is NOT needed...just set DialogResult:

    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.OK
    End Sub

Then check the DialogResult as Erick did in pseudo code:

    If frm.ShowDialog = DialogResult.OK Then
    ' ... grab something from "frm" ...
    End If

Author

Commented:
Thanks

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial