Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

User Settings Help - Vb.net

Posted on 2011-09-16
5
Medium Priority
?
337 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Stack Overflow Podcast - Developer Story

Welcome to the Stack Overflow podcast recorded Thursday July 20 at Stack Overflow Headquearters in NYC. Your hosts today are podcast regulars Jay Hanlon, David Fullerton, and Ilana Yitzhaki, plus the quite irregular Matt Sherman (Stack Overflow Engineering Manager extraordinaire)

Question has a verified solution.

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

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…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

718 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