none
Can't prevent a form shown with ShowDialog from closing when user clicks OK button

    Question

  • I have a VB.NET 2010 Forms Application which includes a form with a textbox, and two buttons which I call from the main form using .ShowDialog

    I've messed with something such that I can no longer prevent the dialog from closing when the user clicks the Ok button but has entered invalid information.

    Here is the handler for the OK button's click event:

    Private Sub cmdOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdOK.Click

    If SomethingIsWrongWithWhatTheUserEntered Then
        MsgBox("Invalid blah blah bla", MsgBoxStyle.Exclamation)
        Exit Sub ' Dialog gets closed anyway
    End If

    Me.DialogResult = DialogResult.OK
    Me.Close()

    End Sub

    I did have the OK and Cancel buttons set as the form's accept and cancel buttons at one point but changing those settings makes no difference.

    I did just recently make my main form an MDI parent form if that makes any difference.

    What am I doing wrong?

    Thanks.

    VB.NET 2010 .NET 4 Windows Forms

    Monday, July 18, 2011 4:28 AM

Answers

  • This works as expected.  Pressing the "OK" button  doesn't close the dialog.  Is your If statement correct?

    Public Class Form1
      
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dialog1.ShowDialog()
      
    End Sub
    End
     Class

    Imports System.Windows.Forms
    Public Class Dialog1
      
    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
        MsgBox(
    "Invalid blah blah bla", MsgBoxStyle.Exclamation)
        
    Exit Sub ' Dialog gets closed anyway
        Me.DialogResult = System.Windows.Forms.DialogResult.OK
        
    Me.Close()
      
    End Sub
      Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
        
    Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        
    Me.Close()
      
    End Sub
    End
     Class
    Monday, July 18, 2011 6:40 AM

All replies

  • What exactly would you like to do now?
    Mitja
    Monday, July 18, 2011 4:46 AM
  • This works as expected.  Pressing the "OK" button  doesn't close the dialog.  Is your If statement correct?

    Public Class Form1
      
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dialog1.ShowDialog()
      
    End Sub
    End
     Class

    Imports System.Windows.Forms
    Public Class Dialog1
      
    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
        MsgBox(
    "Invalid blah blah bla", MsgBoxStyle.Exclamation)
        
    Exit Sub ' Dialog gets closed anyway
        Me.DialogResult = System.Windows.Forms.DialogResult.OK
        
    Me.Close()
      
    End Sub
      Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
        
    Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        
    Me.Close()
      
    End Sub
    End
     Class
    Monday, July 18, 2011 6:40 AM
  • You've probably been busy with the "feature" to assign a exit and cancel to a button.

    For me it has in history given so much pain, that I simply always avoid that "feature" currently as one of the most important things to avoid.

     


    Success
    Cor
    Monday, July 18, 2011 7:05 AM
  • Thanks guys. It may well have something to do with my playing with the form's AcceptButton and CancelButton properties in the IDE. It's as if there's some hidden IDE stuff still intercepting the clicks on the button that used to be designated as the form's AcceptButton.

    Is there a way to correct this?

    Monday, July 18, 2011 2:46 PM
  • I don't think the AcceptButton property can cause the problem you are reporting. AcceptButton simply indicates which button's Click handler is to be called when the user hits enter. It does not change what happens within the Click event handler, which is entirely controlled by your code. If that code is not doing what you expect, I suggest you set a break point at the beginning of the method and step through it with the debugger to see where it goes wrong.
    Monday, July 18, 2011 11:50 PM
  • Thanks. I have debugged it. Something is messed up. I'm just going to start from scratch and re-do the form.
    Tuesday, July 19, 2011 12:33 AM