1

How do i make MessageBoxButtons.RetryCancel to restart the program(for Retry) or exit the program(for Cancel)? Here is my code:

Private Sub btnDisplay_Click(sender As Object, e As EventArgs) Handles btnDisplay.Click
    ' displays a student's grade

    Double.TryParse(txtEarned.Text, dblEarned)

    For Each minimum As Double In dblMinimumPoints
        If dblEarned >= minimum Then
            lblGrade.Text = strGrade(gradeIndex)
            gradeIndex += 1
        End If
    Next

    txtEarned.ReadOnly = False
    btnDisplay.Enabled = False

    MessageBox.Show("Do you want to try again?", "Grade Calculator",
                    MessageBoxButtons.RetryCancel, MessageBoxIcon.Question)
End Sub
0000
  • 677
  • 2
  • 8
  • 20

3 Answers3

4

I suppose that this is a WinForms application.
So, if this is the case, the Application class contains the methods required

Dim result As DialogResult = MessageBox.Show("Do you want to try again?", _
                             "Grade Calculator", _
                             MessageBoxButtons.RetryCancel, MessageBoxIcon.Question)
if result = DialogResult.Retry Then
    Application.Restart()
else
    Application.Exit()
End If

Just a little warning, both methods are a bit dangerous and many people recommends against using them. With a little research on those methods you could find a lot of warnings on the side effects of calling these twos.

Why is Application.Restart() not reliable?
Application.Exit() vs Application.ExitThread() vs Environment.Exit()

and so on....

Community
  • 1
  • 1
Steve
  • 213,761
  • 22
  • 232
  • 286
1

MessageBox returns a DialogResult object. You just need to put in an if statement to branch. The end statement drops out of the program. There is no need to restart, just clear your data entry fields.

 Dim adlg As DialogResult = MessageBox.Show("blah", "blao", MessageBoxButtons.RetryCancel)
        If adlg = Windows.Forms.DialogResult.Retry Then
            resetProgram()
        Else
            'cancel
            End

        End If
Andrew Neely
  • 908
  • 11
  • 19
1
Do

'your code here

Dialogresult res = MessageBox.Show("Do you want to try again?", "Grade Calculator",
                    MessageBoxButtons.RetryCancel, MessageBoxIcon.Question)

Loop While ( (res == DialogResult.Retry) Or (res == DialogResult.Cancel) )
blubbiedevis
  • 415
  • 3
  • 11