locked
Odd DialogResult action RRS feed

  • Question

  • Hi

    I'm obviously missing something obvious but.

    I have a form with 2 buttons, OK and Cancel, with DialogResult.OK and DialogResult.Cancel respectively.

    If I add a Click event to the OK button and do

        Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
            Me.DialogResult = Windows.Forms.DialogResult.None
        End Sub

    and debug it, when I click OK, the code above runs, but after the Me.DialogResult = Windows.Forms.DialogResult.None, Me.DialogResult is set as DialogResult.Cancel. 

    If I change both buttons' DialogResult to None, it still comes back as cancel once the above code runs.

    I've used this method widely in WinForms so a little confused.

    Any ideas?

     

    Many thanks

    Mike

     

     

     

    Friday, September 9, 2011 2:40 PM

Answers

  • I see...

    Why don't you remove DialogResult of OK from the 'OK' button properties and set DialogResult to OK if your validation passes and do nothing if not?


    This posting is provided "AS IS" with no warranties, and confers no rights.
    • Marked as answer by Mike Hoffman Tuesday, September 13, 2011 4:03 PM
    Tuesday, September 13, 2011 12:46 PM

All replies

  • This is rather unusual code, IMO you never suppose to set result to DialogResult.None.

    What is the intended effect of that code?


    This posting is provided "AS IS" with no warranties, and confers no rights.
    Sunday, September 11, 2011 6:33 PM
  • Hi Ilya

    I'm trying stop the dialog from closing.

    I have an OK button, which has a DialogResult of OK.  When clicked I validate whatever I need to in the OK event handler.  If the validation fails, I don't want the dialog to close so set the DialogResult to none.  This 'should' leave the dialog in place (it does in WinForms).

    I could do the check in the FormClosing event but to me it's much tidier to do in the OK handler (as I don't have to distinguish why the form is closing and only do the check if it's the result of the OK button being clicked).

    Regards

    Mike

     

    Tuesday, September 13, 2011 12:38 PM
  • I see...

    Why don't you remove DialogResult of OK from the 'OK' button properties and set DialogResult to OK if your validation passes and do nothing if not?


    This posting is provided "AS IS" with no warranties, and confers no rights.
    • Marked as answer by Mike Hoffman Tuesday, September 13, 2011 4:03 PM
    Tuesday, September 13, 2011 12:46 PM
  • Hi Ilya

    Still not sure why the original method didn't work, but will put that onto my list of things never to worry about again :-)

    Thanks for your help

    Mike

     

    Tuesday, September 13, 2011 4:05 PM