Пропустить Me.FormClosing

Я установил предупреждение перед закрытием формы, но есть ли способ пропустить его иногда?

Мой код:

Sub Me_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
    If MessageBox.Show("Are you sure you want to cancel the installation?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
        e.Cancel = False
    Else
        e.Cancel = True
    End If
End Sub

но у меня есть окончательный код, который должен закрыть приложение без этого сообщения:

Private Sub Done_Click(sender As Object, e As EventArgs) Handles Done.Click
    'need to close without warning
    Close()
End Sub

Можете ли вы помочь мне изменить это или добавить что-то, что позволяет этой кнопке закрыть форму без запуска Me.FormClosing?

1 ответ

Решение

Использовать Boolean флаг, чтобы определить статус установки (успех или сбой / отмена)

Private installSuccess As Boolean ' False by default.

Private Sub Install()

    Try 
        ' Installer logic here
        ' ...

        Me.installSuccess = True

    Catch ' ex As Exception

    End Try

End Sub

Затем:

Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) _
Handles MyBase.FormClosing

    If Me.installSuccess Then
        Exit Sub
    End If

    If MessageBox.Show("Are you sure you want to cancel the installation?", "Warning", 
                       MessageBoxButtons.YesNo, 
                       MessageBoxIcon.Question) = DialogResult.Yes Then
        e.Cancel = False

    Else
        e.Cancel = True

    End If

End Sub
Другие вопросы по тегам