문제

I want to ask how I can make the new form works as an alternative to message box dialog to prevent the form closing in VB.NET?

Well, I create a new form that when the [X] button is click, it will popup and confirm if the user is really want to exit the program.

The form that will popup if the [X] button is click I design is similar to Message Dialog Yes/No, I used two picture box as the Yes and No of my form.

I used this code in the Yes and No picture box in the form that will popup:

Private Sub picxyes_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles picxyes.Click

        Main.Close()
        about.Close()
        Me.Close()

    End Sub

Private Sub picxno_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles picxno.Click
        Me.Close()

And this in form closing event:

e.Cancel = true
exitgame.Show()

'exitgame is the name of the form that will popup when [x] is click.

I also try to add a e.Cancel = False under the Private Sub picxyes_Click but it says error.

I need to know how can I used this picture box (picxyes and picxno) that when picxyes is click, the e.Cancel must be turn into false and whole form must close, otherwise, when picxno is click, only exitgame form will close? Please share any ideas that might help this. Thank you.

도움이 되었습니까?

해결책

MessageBox.Show() displays a dialog. You want to create a dialog as well:

Private Sub picxyes_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles picxyes.Click
    Me.DialogResult = DialogResult.OK
End Sub

Private Sub picxno_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles picxno.Click
    Me.DialogResult = DialogResult.Cancel
End Sub

Now you can simply use the DialogResult, like you do for MessageBox, in your FormClosing event handler:

    If e.CloseReason = CloseReason.UserClosing Then
        Using dlg = New ExitGame
            If dlg.ShowDialog() = DialogResult.OK Then e.Cancel = True
        End Using
    End If
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top