Passing Data between VB.NET forms

人走茶凉 提交于 2019-12-13 16:25:37

问题


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.


回答1:


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



回答2:


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.




回答3:


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


来源:https://stackoverflow.com/questions/4787867/passing-data-between-vb-net-forms

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!