2

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
2
  • We can't help you without some of your code
    – Akrem
    Feb 1, 2012 at 14:28
  • Have you tried stepping through your code to see what is happening?
    – Matt Wilko
    Feb 1, 2012 at 15:08

5 Answers 5

3

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?

4
  • Okay I think I can follow you on this one. So pretty much, store the options I want to have changed in one set of variables and have the form preload those variables each time. When I want to save the form, simple copy the forms values into the preloaded variables. When I want to discard the form, simple do nothing. I just need to have the program call the premade variables. Feb 1, 2012 at 15:07
  • @Paul Williams, exactly, thats it. So the form can either be passed the values or read them when it loads.
    – Jodrell
    Feb 1, 2012 at 15:16
  • Sounds easy in principle but how do you avoid setting any values across 20 or 25 subs in the options section? Isn't that My.Settings.Save() is for? To basically save your setting user settings only if you use it and destroy them if you don't? If not what's the point of having it? Jun 8, 2017 at 7:47
  • @thebunnyrules, 1. Don't write code that does that. 2. The My.Settings object provides access to the application's settings. Not sure how that's pertinent. learn.microsoft.com/en-us/dotnet/visual-basic/…
    – Jodrell
    Jun 8, 2017 at 10:15
1

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.

1
  • Okay now I'm understanding your question. The options should be stored to memory while the program is running. I'm not saving them to a configuration file. Feb 1, 2012 at 15:05
1

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!

4
  • 1
    I don't see how this differs from the question, apart from the simpler and more correct naming of the handlers.
    – Jodrell
    Feb 1, 2012 at 15:17
  • Yes, same code as in the question, it's curious that this answer got accepted. Feb 1, 2012 at 15:29
  • Sorry about that one. Still new to the site. Feb 1, 2012 at 15:37
  • @Jodrell - The question was edited one minute after I posted this to include the code extract.
    – Matt Wilko
    Feb 1, 2012 at 15:58
1

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
0
1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.