Custom dialogue box

Steve36445

Well-known member
Joined
Dec 23, 2007
Messages
58
Programming Experience
10+
Can you help me please?

I am trying to make a custom save dialogue box.

I am trying to test to see if a selected file exists when the save button is pressed and if it does it should show an overwrite message, if it is decided to overwrite the existing file it should return vbOK the file name and path is then available as the standard save dialogue box.

The problem is if I try to exit the save button routine before hiding the form i.e. not accept file. The form still closes.

It seems that once the "form accept" button is pressed no matter what happens vbOK will be returned and the form will be closed.

Is there something like "e.handled" in the keypress event.


Simplified code below-


VB.NET:
'Set out the forms and buttons
        Me.AcceptButton = okay_button
        Me.cancel_button = cancel_button
        Me.ControlBox = False


        AcceptButton.DialogResult = vbOK
        cancel_button.DialogResult = vbCancel



  Private Sub okay_button_Click(sender As System.Object, e As System.EventArgs) Handles okay_button.Click
        If CheckBox1.Checked = False Then

            Exit Sub
        End If

        Me.Close()

    End Sub


thanks in advance,

Steve
 
Firstly, you should never use vbOK or vbCancel. We're not in VB6 anymore Toto. Where VB.NET expects a DialogResult value, use a DialogResult value. Your code would not even compile with Option Strict On, which it should pretty much always be. I suggest that you set it so in the project properties now and also in the IDE options, so that it will be On for all future projects by default. You would then be forced to use DialogResult.OK and DialogResult.Cancel.

As for the issue, it stems from the fact that you're setting the DialogResult properties of those Buttons. If that property is set on a Button then clicking that Button will ALWAYS cause the form to close with its DialogResult property set to the same value. You can handle the Click event and do some extra work but the form WILL close.

If you only want the form to close if a specific condition is met then don't set the DialogResult property of the Button. Handle the Click event of the Button, test your condition and then set the DialogResult property of the form explicitly. Setting the form's DialogResult property will close it, so there's no need to call Close. That might only apply if it was displayed by calling ShowDialog, but I'm guessing that it was in this case.

By the way, what you do is up to you but I would generally tend to do what you're doing in code outside the Click event handler there in the designer.
 
Hi,
I have modified the code as suggested and it is functioning as I wish, however, Whichever button 'OK' or 'Cancel' is pressed it still returns the same value- "cancel"

VB.NET:
Pivate Sub test_dialog_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        ''Set out the forms and buttons
        Me.AcceptButton = okay_button
        Me.cancel_button = cancel_button
        Me.ControlBox = False
        AcceptButton.DialogResult = DialogResult.None
        cancel_button.DialogResult = DialogResult.None
End Sub


  Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Label1.Text = test_dialog.ShowDialog().ToString
    End Sub

 Private Sub okay_button_Click(sender As System.Object, e As System.EventArgs) Handles okay_button.Click
        If CheckBox1.Checked = False Then

            Exit Sub
        End If

        AcceptButton.DialogResult = DialogResult.OK

        Me.Close()

    End Sub

 Private Sub cancel_button_Click(sender As System.Object, e As System.EventArgs) Handles cancel_button.Click
        cancel_button.DialogResult = DialogResult.Cancel

        Me.Close()

    End Sub

Any help would be greatly appreciated.

Thanks,

Steve
 
When you try to implement advice and it doesn't work, the very first thing you should do is to go back and re-read the advice to make sure that you've actually followed it. In this case, you haven't.
Handle the Click event of the Button, test your condition and then set the DialogResult property of the form explicitly. Setting the form's DialogResult property will close it, so there's no need to call Close.
If you actually do modify the code as suggested, it will work.

By the way, there' no point setting any DialogResult properties to None because they already have that value by default.
 
Back
Top