Z. Jiménez
Z. Jiménez

Reputation: 35

result of a modal form in vb.net

I create a form 'frmX' and i call it as a modal form :

res = frmX.ShowDialog()

This form has 3 buttons, Abort(3), Retry(4) and Ignore(5), but when the form opens, all the buttons on the first click return 2.

I don't know why this occurs--all of the buttons has their property DialogResult right.

*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*

Can someone help me?

Upvotes: 1

Views: 751

Answers (2)

Martin Verjans
Martin Verjans

Reputation: 4796

The error comes from the fact that you set the DialogResult of the buttons. You must set the DialogResult of the form !

You actually have more than one option.

Option 1 : Set the 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

Option 2 : Set the 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

Upvotes: 1

Kevin O'Donovan
Kevin O'Donovan

Reputation: 1669

Could do with seeing a bit more context, but the following should do what I think you want:

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

This will close the dialog and return the associated result code to the caller. As to the original code, it seems a bit strange setting the values in the buttons click handlers?

Upvotes: 2

Related Questions