Passing Data between VB.NET forms

2019-07-18 05:52发布

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.

3条回答
爷、活的狠高调
2楼-- · 2019-07-18 05:56

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
查看更多
乱世女痞
3楼-- · 2019-07-18 06:03

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.

查看更多
老娘就宠你
4楼-- · 2019-07-18 06:16

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
查看更多
登录 后发表回答