Database clearing even when I don't want it to

ghz 7months ago ⋅ 78 views

As part of a program I'm making in VB.NET forms, there's the option to delete the contents of a database. This functionality isn't intended to be used very often in the final version and I wouldn't want users to accidentally click on the button and delete the contents when they didn't want to.

I didn't think a simple message box with Yes and No buttons would cut it, as I've heard of several people doing things in programs they didn't want to even with measures like these in place. So, I wanted the message box that pops up to have a "no" button and a checkbox which needs to be ticked to reveal the "yes" button. However, there exists no default message box that has this. After further research, I decided to create a custom one. It works for the most part, however, it clears the database anyway, regardless of whether I tick the checkbox and press "yes" or whether I press "no".

Below is the code that is used to clear the database:

Private Sub btnClearDatabase_Click(sender As Object, e As EventArgs) Handles btnClearDatabase.Click
    'Opens the Clear Database dialog to confirm if the user wants to clear the database.
    ClearDatabase.ShowDialog()
    If ClearDatabase.DialogResult.OK Then
        'Clears Database. Code not shown
    End If
End Sub

I set the DialogResult property of the "yes" button to OK, so I assumed that the code in the If statement would only run if that button was pressed. But it runs anyway regardless if I press it. It runs when I open the dialog. Why does it do this?

It might have something to do with a warning in the If statement code that says:

BC42025 Access of shared member, constant member, enum member or nested type through an instance; qualifying expression will not be evaluated.

Rewriting If ClearDatabase.DialogResult.OK Then as If DialogResult.Ok Then gets rid of the warning but running it still has the same result. I'm really confused as to why it's doing this.

EDIT: To clarify, the code doesn't run the moment the dialog opens. It runs the moment the dialog closes.

Answers

It seems like there might be a misunderstanding about how the DialogResult property works. The DialogResult property is set when the dialog is closed, not when a button inside the dialog is clicked. Therefore, the If ClearDatabase.DialogResult.OK Then condition is evaluating the DialogResult after the dialog is closed, not based on which button was clicked inside the dialog.

To achieve the desired behavior of only clearing the database when the "yes" button is clicked and the checkbox is ticked, you need to modify your custom dialog form. Here's how you can do it:

  1. Add a checkbox and "yes" and "no" buttons to your custom dialog form.
  2. In the "yes" button click event handler, check if the checkbox is checked. If it is, set the DialogResult of the form to DialogResult.OK. If it's not checked, display a message to the user indicating that they need to check the checkbox.
  3. In the "no" button click event handler, set the DialogResult of the form to DialogResult.Cancel.

Here's an example of what your custom dialog form code might look like:

Public Class ClearDatabaseDialog
    Private Sub btnYes_Click(sender As Object, e As EventArgs) Handles btnYes.Click
        If chkConfirm.Checked Then
            DialogResult = DialogResult.OK
        Else
            MessageBox.Show("Please confirm by checking the checkbox.", "Confirmation Required", MessageBoxButtons.OK, MessageBoxIcon.Warning)
        End If
    End Sub

    Private Sub btnNo_Click(sender As Object, e As EventArgs) Handles btnNo.Click
        DialogResult = DialogResult.Cancel
    End Sub
End Class

With this setup, the DialogResult property of the custom dialog form will correctly reflect whether the user clicked "yes" or "no". Then, in your main form, you can check the DialogResult after the dialog is closed to determine whether to clear the database:

Private Sub btnClearDatabase_Click(sender As Object, e As EventArgs) Handles btnClearDatabase.Click
    Dim clearDialog As New ClearDatabaseDialog()
    If clearDialog.ShowDialog() = DialogResult.OK Then
        ' Clear the database
    End If
End Sub

This way, the database will only be cleared if the user clicks the "yes" button and checks the checkbox in the custom dialog form. Otherwise, the database will not be cleared.