Solved

User Settings Help - Vb.net

Posted on 2011-09-16
5
331 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

803 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