473,379 Members | 1,222 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

Something wrong with the code

The object of the code snippet below is to save and restore a form's size
and location. The saveFormSettings() method is called when a form is closing
and restoreFormSettings() is called after InitializeComponent().

The problem is even though the settings appear to be being saved, the
strWidth variable is always null, so the settings are never restore.

Any clues as to what I'm doing wrong?

TIA

David McCallum

private static NameValueCollection appSettings =
ConfigurationManager.AppSettings;

/// <summary>
/// Restore a form's settings
/// </summary>
/// <param name="form">The form to restore</param>
public static void restoreFormSettings(Form form)
{
string strWidth = appSettings.Get(form.Name + "FormWidth");
if (strWidth != null)
{
string strHeight = appSettings.Get(form.Name + "FormHeight");
string strX = appSettings.Get(form.Name + "FormX");
string strY = appSettings.Get(form.Name + "FormY");
form.Size = new Size(Int32.Parse(strWidth), Int32.Parse(strHeight));
form.Location = new Point(Int32.Parse(strX), Int32.Parse(strY));
}
}

/// <summary>
/// Save a form's settings
/// </summary>
/// <param name="form">The form to save</param>
public static void saveFormSettings(Form form)
{
appSettings.Set(form.Name + "FormWidth", form.Size.Width.ToString());
appSettings.Set(form.Name + "FormHeight", form.Size.Height.ToString());
appSettings.Set(form.Name + "FormY", form.Location.Y.ToString());
appSettings.Set(form.Name + "FormX", form.Location.X.ToString());
}
May 19 '07 #1
5 1538
There is a much easier way to do this using Visual Studio. You can create
Application Settings and bind your Form properties to them. See the
following articles:

http://msdn2.microsoft.com/en-us/library/k4s6c3a0.aspx
http://msdn2.microsoft.com/en-us/lib...9h(VS.80).aspx

--
HTH,

Kevin Spencer
Microsoft MVP

Printing Components, Email Components,
FTP Client Classes, Enhanced Data Controls, much more.
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net

"David McCallum" <dm**********@THISblueyonder.co.ukwrote in message
news:v0*****************@fe2.news.blueyonder.co.uk ...
The object of the code snippet below is to save and restore a form's size
and location. The saveFormSettings() method is called when a form is
closing and restoreFormSettings() is called after InitializeComponent().

The problem is even though the settings appear to be being saved, the
strWidth variable is always null, so the settings are never restore.

Any clues as to what I'm doing wrong?

TIA

David McCallum

private static NameValueCollection appSettings =
ConfigurationManager.AppSettings;

/// <summary>
/// Restore a form's settings
/// </summary>
/// <param name="form">The form to restore</param>
public static void restoreFormSettings(Form form)
{
string strWidth = appSettings.Get(form.Name + "FormWidth");
if (strWidth != null)
{
string strHeight = appSettings.Get(form.Name + "FormHeight");
string strX = appSettings.Get(form.Name + "FormX");
string strY = appSettings.Get(form.Name + "FormY");
form.Size = new Size(Int32.Parse(strWidth), Int32.Parse(strHeight));
form.Location = new Point(Int32.Parse(strX), Int32.Parse(strY));
}
}

/// <summary>
/// Save a form's settings
/// </summary>
/// <param name="form">The form to save</param>
public static void saveFormSettings(Form form)
{
appSettings.Set(form.Name + "FormWidth", form.Size.Width.ToString());
appSettings.Set(form.Name + "FormHeight", form.Size.Height.ToString());
appSettings.Set(form.Name + "FormY", form.Location.Y.ToString());
appSettings.Set(form.Name + "FormX", form.Location.X.ToString());
}

May 21 '07 #2

"Kevin Spencer" <un**********@nothinks.comwrote in message
news:uX**************@TK2MSFTNGP04.phx.gbl...
There is a much easier way to do this using Visual Studio. You can create
Application Settings and bind your Form properties to them. See the
following articles:

http://msdn2.microsoft.com/en-us/library/k4s6c3a0.aspx
http://msdn2.microsoft.com/en-us/lib...9h(VS.80).aspx
I had a look at this, but as I understand application wide settings are
readonly, or am I mistaking.

Also forms will be created on the fly. How do I create settings, e.g. name1,
name2, name3......

TIA

David.
May 21 '07 #3
On Mon, 21 May 2007 11:08:20 -0700, David McCallum
<dm**********@THISblueyonder.co.ukwrote:
I had a look at this, but as I understand application wide settings are
readonly, or am I mistaking.
Define "application wide". It's true that settings in the "Application"
domain are read-only. However, for the example you've given here, it
appears that the settings would more appropriately belong in the "User"
domain. Why should window position saved for one user affect the window
position for a different user? "User" domain settings are read/write and
very convenient to use.
Also forms will be created on the fly. How do I create settings, e.g.
name1, name2, name3......
Well, I can assure you that you could get it to work with the .NET
Settings architecture. So that's more than I can say for the method
you're using now. :)

I admit, I don't know much about the ConfigurationManager.AppSettings
class, but as near as I can tell the AppSettings class offers only
read-only access to settings as well. From
http://msdn2.microsoft.com/en-us/lib...nmanager.aspx:

"For some sections such as appSettings and connectionStrings,
use the AppSettings and ConnectionStrings classes. These members
perform read-only operations..."

I don't know how you have determined that "the settings appear to be being
saved", but the above quote suggests to me that they aren't in fact
saved. Which would explain why they always come back null.

It seems to me that you can either use the ConfigurationManager on a
custom "Configuration" object, which the docs say that you can use for
read/write access to settings, or you can update your technique and use
the regular Properties.Settings.Default that is provided for in .NET.

Back to the specific question about saving settings based on the specific
form name...

I'm not really clear on why you want to do this, assuming your forms'
names are set at design time. Even if the user can change the name of the
form at run-time, in most situations you would still have a consistent
internal variable name associated with each form that you could use to tie
specific settings to a specific form.

But let's assume that you've got one of those rare applications where the
user can create their own forms at run-time. Let's also assume that the
form name is a unique identifier useful for tying specific settings to a
specific form. Then one possibility is to create a Dictionary<instance
that ties the form name to a struct containing the form position (for
example, a Rectangle) and store that in your settings.

Of course, you may find it easier at this point to continue using the
ConfigurationManager class. If so, I can't help much with that, since I
haven't used it myself. But you should definitely explore the possibility
that you're using it wrong and that it won't save the settings you assign
in the AppSettings class.

Pete
May 21 '07 #4
Define "application wide". It's true that settings in the "Application"
domain are read-only. However, for the example you've given here, it
appears that the settings would more appropriately belong in the "User"
domain. Why should window position saved for one user affect the window
position for a different user? "User" domain settings are read/write and
very convenient to use.
Granted in this case they would be user dependant, but I also have a setting
of data locations which will be set by the user. This will be the same for
each user.
I don't know how you have determined that "the settings appear to be being
saved", but the above quote suggests to me that they aren't in fact
saved. Which would explain why they always come back null.
What I meant was the method is executed.

David.
May 21 '07 #5
These would be user-scoped, which means that they are read-write, and
specific to each logged-in user.

--
HTH,

Kevin Spencer
Microsoft MVP

Printing Components, Email Components,
FTP Client Classes, Enhanced Data Controls, much more.
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net

"David McCallum" <dm**********@THISblueyonder.co.ukwrote in message
news:oe******************@fe1.news.blueyonder.co.u k...
>
"Kevin Spencer" <un**********@nothinks.comwrote in message
news:uX**************@TK2MSFTNGP04.phx.gbl...
>There is a much easier way to do this using Visual Studio. You can create
Application Settings and bind your Form properties to them. See the
following articles:

http://msdn2.microsoft.com/en-us/library/k4s6c3a0.aspx
http://msdn2.microsoft.com/en-us/lib...9h(VS.80).aspx

I had a look at this, but as I understand application wide settings are
readonly, or am I mistaking.

Also forms will be created on the fly. How do I create settings, e.g.
name1, name2, name3......

TIA

David.


May 22 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Luka Milkovic | last post by:
Hello, I have a little problem and although it's little it's extremely difficult for me to describe it, but I'll try. I have written a program which extracts certain portions of my received...
0
by: Steve | last post by:
Hi, Is there something fundamentally wrong with the following query? SELECT MAX(OrderID) AS Expr1 FROM Orders WHERE (MarketActionCode = 'S') AND (OrderLegCode = 'S') AND (Status = 'F') AND...
6
by: Vandana Rola | last post by:
Hello Everyone, I posted this question earlier under creating Multiple choice quiz. Is it possible to ignore something using javascript. What I am trying to do is creating a multiple answer...
6
by: Michael Sparks | last post by:
Hi, I suspect this is a bug with AMK's Crypto package from http://www.amk.ca/python/code/crypto , but want to check to see if I'm being dumb before posting a bug report. I'm looking at...
3
by: Beemer Biker | last post by:
I started seeing huge amounts of error messages such as "Validation (XHTML 1.0 Transitional):" Element schema is not supported". There was nothing wrong with that for 3 weeks and suddenly there...
11
by: matsi.inc | last post by:
I am looking to make something like a delegate that i can use in my projects but am having a hard time getting started. The behavior I am most interested in is how a delegate changes it's Invoke...
25
by: Jon Slaughter | last post by:
I have some code that loads up some php/html files and does a few things to them and ultimately returns an html file with some php code in it. I then pass that file onto the user by using echo. Of...
2
by: patrickdepinguin | last post by:
Hi, I use zlib to write data structures to a compressed file, using the gzwrite function. Afterwards I read the data back with gzread. I notice that this works well when the data written is not...
3
by: Peter Oliphant | last post by:
It's not necessary, but it would be nice if there was a way I could easily create something like a 'web browser form' or a 'display web page form' as forms in my application (in a Panel would be...
34
by: raphfrk | last post by:
This program should copy one file onto the other. It works if I compile it with gcc to a cygwin program. However, if I compile it with the -mno-cygwin option, it doesn't work (this targets native...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.