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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 17:03:13  来源:igfitidea点击:

How can I return a value from a ShowDialog window?

vb.netwinformspopupshowdialog

提问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 AcceptButtonand the CancelButtonproperty on the Form, but also, the AcceptButtonshould have its property DialogResultset to OKand the CancelButtonto Cancel.

你应该设置AcceptButtonCancelButton财产的形式,而且,在AcceptButton应该有其属性DialogResult设置为OKCancelButton取消

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 DialogResultproperty 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 frmMessageyou'll have to set the property accordingly (pick the one you need, OKand Cancel). Then you can check the return value easily:

在您中,frmMessage您必须相应地设置属性(选择您需要OKCancel的属性)。然后您可以轻松检查返回值:

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 DialogResultin the main window. However the DialogResultproperty for the Acceptbutton 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.

StyxxySteve的答案都适用于处理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

它将返回结果中止