vb.net中的模态形式的结果

时间:2016-05-23 12:37:53

标签: vb.net modal-dialog

我创建了一个'frmX'形式,我将其称为模态形式:

res = frmX.ShowDialog()

此表单有3个按钮,Abort( 3 ),Retry( 4 )和Ignore( 5 ),但表单打开时,第一次点击的所有按钮都会返回 2

我不知道为什么会这样 - 所有按钮的属性都是DialogResult。

*Private Sub btnIgnorar_Click(sender As Object, e As EventArgs) Handles btnIgnorar.Click
    btnIgnorar.DialogResult = DialogResult.Ignore
End Sub
Private Sub btnAbortar_Click(sender As Object, e As EventArgs) Handles btnAbortar.Click
    btnAbortar.DialogResult = DialogResult.Abort
End Sub
Private Sub btnReintentar_Click(sender As Object, e As EventArgs) Handles btnReintentar.Click
    btnReintentar.DialogResult = DialogResult.Retry
End Sub*

有人可以帮助我吗?

2 个答案:

答案 0 :(得分:2)

可以看到更多的上下文,但以下应该做我认为你想要的:

Private Sub btnIgnorar_Click(sender As Object, e As EventArgs) Handles btnIgnorar.Click
    DialogResult = DialogResult.Ignore
    Close
End Sub

这将关闭对话框并将关联的结果代码返回给调用者。至于原始代码,设置按钮单击处理程序中的值似乎有点奇怪吗?

答案 1 :(得分:1)

错误来自于您设置按钮的DialogResult这一事实。您必须设置表单的DialogResult!

您实际上有多个选项。

选项1:设置Form.DialogResult

Private Sub btnIgnorar_Click(sender As Object, e As EventArgs) Handles btnIgnorar.Click
    Me.DialogResult = DialogResult.Ignore
End Sub
Private Sub btnAbortar_Click(sender As Object, e As EventArgs) Handles btnAbortar.Click
    Me.DialogResult = DialogResult.Abort
End Sub
Private Sub btnReintentar_Click(sender As Object, e As EventArgs) Handles btnReintentar.Click
    Me.DialogResult = DialogResult.Retry
End Sub

选项2:设置Button.DialogResult

Public Sub New()
    InitializeComponents()
    'Your init code here
    '...
    'By setting the buttons DialogResults, you don't even have to handle the click events
    btnIgnorar.DialogResult = DialogResult.Ignore
    btnAbortar.DialogResult = DialogResult.Abort
    btnReintentar.DialogResult = DialogResult.Retry
End Sub

'However, if you need to do some stuff before closing the form, you can
Private Sub btnAbortar_Click(sender As Object, e As EventArgs) Handles btnAbortar.Click
    'Do some stuff
    'You don't need the following line, as it will be done implicitly
    'Me.DialogResult = DialogResult.Abort
End Sub