vb.net 如何从 ShowDialog 窗口返回一个值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17982144/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I return a value from a ShowDialog window?
提问by Landmine
frmMain
主界面
DoSomething()
My.Forms.frmMessage.ShowDialog(Me)
If AcceptButtonClicked Then
' Do Code
DoCode()
Else
' Cancel Button Pressed
DoOtherCode()
End If
DoMore()
frmMessage
消息
My.Forms.frmMain.AcceptButtonClicked = True
Is there a way to pass a value from a Dialog window back to a paused thread on the main window? I want to know if they pressed the Ok or Cancel Button after filling out a form that pops up.
有没有办法将值从对话框窗口传递回主窗口上的暂停线程?我想知道他们在填写弹出的表格后是否按下了确定或取消按钮。
回答by Steve
You should set the AcceptButton
and the CancelButton
property on the Form, but also, the AcceptButton
should have its property DialogResult
set to OKand the CancelButton
to Cancel.
你应该设置AcceptButton
和CancelButton
财产的形式,而且,在AcceptButton
应该有其属性DialogResult
设置为OK和CancelButton
以取消。
In this way, when your user presses one of these buttons the ShowDialog call returns and you can check the return value using the predefined values in the enum DialogResult
这样,当您的用户按下这些按钮之一时,ShowDialog 调用将返回,您可以使用枚举DialogResult 中的预定义值检查返回值
DoSomething()
Dim result = My.Forms.frmMessage.ShowDialog(Me)
If result = DialogResult.OK Then
' Do Code
DoCode()
Else
' Cancel Button Pressed
DoOtherCode()
End If
DoMore()
回答by Styxxy
You can use the DialogResult
property on your form. This value will be returned by the ShowDialog function you call. You can also set this property on your buttonsso WinForms will handle the setting of the form property.
您可以DialogResult
在表单上使用该属性。该值将由您调用的 ShowDialog 函数返回。您还可以在按钮上设置此属性,以便 WinForms 处理表单属性的设置。
In your frmMessage
you'll have to set the property accordingly (pick the one you need, OK
and Cancel
). Then you can check the return value easily:
在您中,frmMessage
您必须相应地设置属性(选择您需要OK
Cancel
的属性,和)。然后您可以轻松检查返回值:
If My.Forms.frmMessage.ShowDialog(Me) = System.Windows.Forms.DialogResult.OK Then
' OK button pressed
DoCode()
Else
' Cancel button pressed
DoOtherCode()
End If
Don't forget that the user might be able to close the form in another way than closing it with your buttons (e.g. by closing it with the close button).
不要忘记,用户可以用另一种方式关闭表单而不是用您的按钮关闭它(例如通过使用关闭按钮关闭它)。
回答by Wayne
The answers by Styxxyand Steveboth work for handling the DialogResult
in the main window. However the DialogResult
property for the Accept
button should not be set in the properties window, it should be set in your code after validationoccurs. This way if the user inputs bad data in the form they can get an error message and fix it without losing any work, instead of starting over.
Styxxy和Steve的答案都适用于处理DialogResult
主窗口中的 。然而,按钮的DialogResult
属性Accept
不应在属性窗口中设置,而应在验证发生后在您的代码中设置。这样,如果用户在表单中输入错误数据,他们可以获得错误消息并修复它而不会丢失任何工作,而不是重新开始。
'code in Dialog Form
Private Sub btnAccept_Click(sender As System.Object, e As System.EventArgs) Handles btnAccept.Click
If IsValid() = True Then
DialogResult = Windows.Forms.DialogResult.OK
End If
End Sub
回答by aex
Me.DialogResult = Windows.Forms.DialogResult.Abort Me.Close()
Me.DialogResult = Windows.Forms.DialogResult.Abort Me.Close()
and it will return result Abort
它将返回结果中止