0

I have a problem where my code clears regardless if I press yes or no inside my message box. I believe i could probably get rid of my if statments as well i dont know why i included them like that

Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
    If MsgBox("Are you sure?", MessageBoxButtons.YesNo, MessageBoxIcon.Question = DialogResult.Yes) Then

        If (chkLevel3Support.Checked) Then
            chkLevel3Support.Checked = False
        End If

        If (chkOnSiteTraining.Checked) Then
            chkOnSiteTraining.Checked = False
        End If

        If (chkCloudBackup.Checked) Then
            chkCloudBackup.Checked = False
        End If

        If (rdbYearlyLicense.Checked) Then
            rdbYearlyLicense.Checked = False
        End If

        If (rdbOneTIme.Checked) Then
            rdbOneTIme.Checked = False
        End If

        lbCostofLicenseOutput.Text = " "

        lbCostofOptionalOutput.Text = " "

        lbTotalCostSale.Text = " "
    End If
End Sub
djv
  • 15,168
  • 7
  • 48
  • 72
  • The code you presented has no MessageBox. Did you mean to include the commented code? Can you please fix it so it represents your actual code? – djv Feb 18 '20 at 20:38
  • 1
    Please use [`Option Strict On`](https://stackoverflow.com/a/29985039/1115360) to let Visual Studio tell you where the problems are. – Andrew Morton Feb 18 '20 at 20:41
  • 1
    There is no point in checking for `chkLevel3Support.Checked`, and so on, if it has to end up as `False` anyway. – Andrew Morton Feb 18 '20 at 20:43
  • You probably meant `If MessageBox.Show("Are you sure?", "Confirm clear", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then`. – Andrew Morton Feb 18 '20 at 20:50
  • 1
    @AndrewMorton don't forget about CheckState.Intermediate :) `chkLevel3Support.CheckState = CheckState.Indeterminate` `If chkLevel3Support.Checked Then chkLevel3Support.Checked = False` – djv Feb 18 '20 at 20:56
  • 1
    @djv Lol, I guess no-one was sure if that would be in the spec :) – Andrew Morton Feb 18 '20 at 22:22

1 Answers1

1

I think your right parenthesis in in the wrong spot. Try this:

If MsgBox("Are you sure?", MsgBoxStyle.YesNo Or MsgBoxStyle.Question) = MsgBoxResult.Yes Then

or doing it the .NET way as proposed by Andrew:

If MessageBox.Show("Are you sure?", "Confirm clear", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
Brian M Stafford
  • 8,483
  • 2
  • 16
  • 25