Question

I have a Visual Studio, Visual Basic form that includes an OK button and a Cancel button.

What I want to do is have the OK button save the options that the user chooses and of course the Cancel button discarding them and returning them to their previous values.

But what I'm noticing is that as I'm debugging the form, the values are being saved regardless of whichever button I'm choosing. On the form's properties, I have declared that indeed the CancelBtn is the CancelBtn and that the OK button is the OK button, but the values are still being saved regardless.

Is there a better way to do what I would like this form to do?

EDIT:

Here's the code so far for the two buttons, both are being set to close the window. AcceptOption should save the values and CancelOption should just close the form. I'm sorry if this isn't done well but the FAQ's that I found only mention changing the properties of each button and nothing about the code.:

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles AcceptOptionBtn.Click
    ' Save the Options
    Me.Close()
    ' Close the form
End Sub

Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles CancelOptionBtn.Click
    ' Close the form
    Me.Close()
End Sub
Was it helpful?

Solution

Don't change "the values" until the user clicks the Save button.

The form should be preloaded with a copy of the values you would like to update.

The Cancel button should just close the form.

The Save button should cause "the values", not the forms copy, to be updated.

EDIT:-

In regard to this question, there is nothing wrong with the code you have posted. Are the right handlers being called for the right button clicks? Are the form's AcceptButton and CancelButton properties set to the right buttons?

What data are your editing controls bound to, if at all?

OTHER TIPS

There's nothing magical about OK and Cancel buttons. They're just... buttons. If you save your data every time a change is made, the Cancel button won't magically "unsave" them. Though if you save changes in the OK button's Click event handler, then clicking the Cancel button obviously won't save your changes. To help you further we'd need to know how you save your data.

Edit:

From looking at your code, I think you're passing data directly to your form, without performing a copy of your objects. Therefore if you modify this data, it will also be changed in the parent form. By working with a copy of your data in this form, any changes which aren't saved will be correctly discarded.

Your event handler for the cancel button should look like this:

Private Sub btnCancel_Click(sender As System.Object, e As System.EventArgs) Handles btnCancel.Click
    Me.Close()
End Sub

Your event handler for the OK button should look like this:

Private Sub btnOK_Click(sender As System.Object, e As System.EventArgs) Handles btnOK.Click
    SaveSettings 'call a routine to save the settings the user has entered
    Me.Close()
End Sub

It is as simple as that!

If you open your form like

myForm.showdialog()

you don't have to define the handler for the close button click event, it is automatically handled; just set the 'DialogResult' property for the button

btnCancel.DialogResult = DialogResult.Cancel

Also if you want to close the form when ESC is pressed then set the 'CancelButton' property for the form:

myForm.CancelButton = btnCancel

On the other hand if you open the form like

myForm.Show()

you do need to specify the action(s) to take on the close button click event as indicated here, ie:

Private Sub BtnCancelClick(ByVal sender As System.Object, ByVal e As EventArgs) Handles btnCancel.Click
     Close()
End Sub

I was having the same issues. As soon as I use My.Settings.Blabla = Blabla.value, it gets saved even if I haven't used My.Settings.Save() which makes My.Settings.Save() completely pointless as far as I can tell.

I ended up taking up Jordell's advice: Don't change "the values" until the user clicks the Save button but it wasn't too clear for me how to go about it.

I ended up using temporary variables in all my settings subs instead of the user My.Settings.UserConfigs. Only when I was in the OK sub did I call

My.Settings.UserConfigSetting = temporary_UserCofigValue

Here is an example from the code I was working on:

Private Sub btnOptionsThemeLB_Back_Update_Click(sender As System.Object, e As System.EventArgs) Handles btnOptionsThemeLB_Back_Update.Click
    If (tempOptionsThemeLB_Back = Nothing) Then
        tempOptionsThemeLB_Back = Me.btnOptionsThemeLB_Back.BackColor
    End If
    tempOptionsThemeLB_Back = RGBToColor(txtbOptionsThemeLB_Back_Red.Text, txtbOptionsThemeLB_Back_Green.Text, txtbOptionsThemeLB_Back_Blue.Text, tempOptionsThemeLB_Back)
    Me.btnOptionsThemeLB_Back.BackColor = tempOptionsThemeLB_Back
End Sub

And only withing the Ok sub did I call My.Settings.

'Theme Section
My.Settings.colorBtnBack = tempOptionsThemeLB_Back
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top