Solved

User Settings Help - Vb.net

Posted on 2011-09-16
5
318 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
Comment Utility
make sure you call My.Settings.Save() after you update / change your settings
0
 
LVL 1

Author Comment

by:NTGuru705
Comment Utility
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
Comment Utility
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
Comment Utility
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

Expert Comment

by:Jacques Bourgeois (James Burger)
Comment Utility
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

763 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

9 Experts available now in Live!

Get 1:1 Help Now