Solved

User Settings Help - Vb.net

Posted on 2011-09-16
5
326 Views
Last Modified: 2012-05-12
I have a windows form application being developed in VS 2008 on Windows 7 64 bit machine.

My user settings are not being persisted between application loads.

So for example. I have this in the form close event:
MsgBox(My.Settings.copyValues.ToString)

The setting is a Boolean value. When the application closes the value it TRUE.
I can see the user.config file has the value stored in it.

I then have this in the me.Startup Event
MsgBox(My.Settings.copyValues.ToString)

When this code runs the value is TRUE - as one would think.
BUT then the next thing that fires is the MyBase.Load event.
In this event the first thing is:

MsgBox(My.Settings.copyValues.ToString)

Here the value equals FALSE.

This tells me somehow between the startup application event and the form load event the value is changing... this is so very frustrating because I cannot for the life of me figure this out and I have invested hours trying to understand when/how this is getting changed.. but cannot get it.

The settings all have GenerateDefaultValueInCode set to True - which I understand to mean if it does exist use the value you specify in the project settings.
Scope: User
Roaming: False

Please advise

0
Comment
Question by:NTGuru705
  • 2
  • 2
5 Comments
 
LVL 17

Expert Comment

by:nepaluz
ID: 36552589
make sure you call My.Settings.Save() after you update / change your settings
0
 
LVL 1

Author Comment

by:NTGuru705
ID: 36552660
Yes this is presently already being done
notice that in the start event the value is correct

additionally the user dot configfile has the correct values

the problem manifests between the start event and the form load event
0
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 500 total points
ID: 36553095
The value cannot change automatically. There is a piece of code that changes it.

The "next thing that fires" is often a view of our head. We think that this is what is happening, but in a world full of event, what we think happens is usually wrong. The only way to go is to examine what happens.

Put My.Settings.copyValues in the watch window.
Set a breakpoint in the Startup event.
When the application breaks, execute the lines one by one (F11 by default).
Check the watch window to see at what point the value gets resetted to False.
You got your culprit.
0
 
LVL 1

Author Comment

by:NTGuru705
ID: 36553989
Ok I found it... I am sorry for the trouble.
It seems what I didnt know is that the event optCheckedOnly.CheckedChanged (a checkbox) fires between the application start and the form load event. I didnt know this I thought there was nothing that fired before the load event but the startup event.

I had code in this handler that was changing it.
Something to know that a checkbox checkchanged event will fire before form load.

Thanks
0
 
LVL 40
ID: 36554381
Many events can trigger before a form load.

When you call New on a form, it calls a New procedure (called a constructor) in the code hidden in the designer.vb file code that is compiled into your form. New calls a method called InitializeComponents. This method builds the form and then apply all the properties that you have defined for the controls that are not the default property. If one of those changes is the kind to trigger an event, such as you CheckedChange event, it will trigger.

Load is called only after all that has happened.

This sometimes cause trouble, as you have seen. One way to go around this is the following:
Public Sub EventProcedure (sender As Object, e As SomeEventArgs) Handles SomeControl.SomeEvent
   Static flag As Boolean
   If Not flag Then
      flag = True  'Do nothing on the first pass in the event, the one called before Load
   Else
      'Do you stuff here
   End If
End Sub

Open in new window

A static variable keeps its value between calls. As any Boolean, it gets initialized to False the first time. Change, and it will still be True everytime you go back in the event.

Another way around is to remove the Handles clause at the end of the declaration. That way, the procedure is not an event and won't be called by the first call.

In the Load event, you can "activate" the event by registering your procedure with code like the following:
AddHandler SomeControl.SomeEvent, AddressOf EventProcedure

Open in new window


Wonderful little world, that .NET, once you've got all the basic tricks.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now