jinanwow
jinanwow

Reputation: 509

Passing Data between VB.NET forms

I have a form that has a button, when clicked it pops up a Dialog Form. Within this dialog form the user needs to select some data and when the user is finished they click the OK button. Once they click the OK button it needs to return an integer back to the previous form.

I created a Dialog Form and tried calling it via the code below:

Dim intResult as Integer = frmData.ShowDialog()

Debug.Writeline(intResult)

However, it seems I can only return DialogResults (Abort, Cancel, Ignore...)

I was wondering how I can try this without having to create a public variable and storing the result there.

Upvotes: 2

Views: 3906

Answers (3)

Tim Schmelter
Tim Schmelter

Reputation: 460028

Create a cutom Dialog to your project(add/new element/Windows Forms/Dialog). Then create an instance from it, call showDialog and check if its DialogResult is Windows.Forms.DialogResult.Ok. You can access all of its controls, for example:

Dim d As New Dialog1
Dim result As DialogResult = d.ShowDialog(Me)
If result = Windows.Forms.DialogResult.OK Then
    Dim selectedText As String = d.ComboBox1.SelectedText
End If

Upvotes: 1

Henk Holterman
Henk Holterman

Reputation: 273179

Create a property on the Dialog that will return the value.

If frmData.ShowDialog() Is Not DialogResult.Cancel
   Dim value as integer = frmData.MyProperty 
   ...
Endif

Upvotes: 4

George Johnston
George Johnston

Reputation: 32258

Create an event on your dialog form, subscribe to it on your main form, and raise it on your dialog with the appropriate data contained within the event arguments.

Upvotes: 2

Related Questions