[dba-VB] VB.Net XML - store form defaults

Charlotte Foust cfoust at infostatsystems.com
Wed Jul 18 12:05:08 CDT 2007


The problem with using the registry is that a lot of IT departments are
locking it down and require you to use the Documents and Settings folder
to store current user stuff.  We use XML for this purpose.  We'll have
an XML file that contains a variable number of elements, which all have
a ReadOnly attribute set to False as the default.  We use a class to
retrieve values from the file and to save values to it.  Similar to what
we used to do with registry settings.  You could have a
FormsDefaults.XML file that contains a template element with default
settings in it for any form you have saved custom settings for, and then
the class could create a new element for each form that had saved
settings.  Not hard and it doesn't require any api calls to read.

Charlotte Foust

-----Original Message-----
From: dba-vb-bounces at databaseadvisors.com
[mailto:dba-vb-bounces at databaseadvisors.com] On Behalf Of Ron Allen
Sent: Wednesday, July 18, 2007 9:54 AM
To: dba-vb at databaseadvisors.com
Subject: Re: [dba-VB] VB.Net XML - store form defaults

John,

I know that there is a school of thought that this approach is
inappropriate, but I use the registry for this sort of thing. I use the
HKEY_CURRENT_USER hive so each user has their own set of saved values,
which is most often convenient for me.

This example stores (in the form close event) the location, height, and
width of the form, and (in the form open event) restores the form to
that size and location. You should be able to easily adapt it for your
needs.

Ron



(all syntax in C#, but should translate easily and well to VB)

using Microsoft.Win32; //necessary include to get Registry classes

In form close event:

/*
If form is maximized, minimized, or larger than the screen in either
dimension, do not save size and position information */
if(this.WindowState != FormWindowState.Maximized && 
   this.WindowState != FormWindowState.Minimized &&
   this.Width <= Screen.PrimaryScreen.Bounds.Width && 
   this.Height <= Screen.PrimaryScreen.Bounds.Height)
{
   string regPath = @"Software\Tribune\Circ\CircTools\Settings";
   RegistryKey regKey = Registry.CurrentUser.OpenSubKey(regPath, true);
   if(regKey.GetValue("ResettingPositions").ToString() == "0")
   {
      regPath = @"Software\Tribune\Circ\CircTools\Forms\Main\Position";
      regKey = Registry.CurrentUser.OpenSubKey(regPath, true);
      if(regKey != null)
      {
          regKey.SetValue("X", Location.X);
	  regKey.SetValue("Y", Location.Y);
	  regKey.SetValue("H", this.Height);
	  regKey.SetValue("W", this.Width);
      }
   }
}

In form open event:

string regPath = @"Software\Tribune\Circ\CircTools\Forms\Main\Position";
RegistryKey regKey = Registry.CurrentUser.OpenSubKey(regPath, true);
if(regKey == null) {
    regKey = Registry.CurrentUser.CreateSubKey(regPath);
}
if(regKey.GetValue("X") != null && regKey.GetValue("Y") != null) {
    Location = new Point((int)regKey.GetValue("X"),
(int)regKey.GetValue("Y")); }
if(regKey.GetValue("H") != null && regKey.GetValue("W") != null) {
	this.Height = (int)regKey.GetValue("H");
	this.Width = (int)regKey.GetValue("W"); }


----------------------  Original Message:  ---------------------
From:    "jwcolby" <jwcolby at colbyconsulting.com>
To:      <dba-vb at databaseadvisors.com>
Subject: [dba-VB] VB.Net XML - store form defaults
Date:    Wed, 18 Jul 2007 15:31:33 +0000

> I need a way to store the values in controls as the form closes, such 
> that the form can the load those last values when it re-opens.  I do 
> things like enter data in controls for server, database, table, paths 
> to files etc.  I need the form to save those values so that the next 
> time the form opens it can retrieve those values and I don't have to
key them in again every time.
> 
> Does anyone has any code for doing that, or hotlinks to UNDERSTANDABLE

> (to a
> nubee) web sites?
> 
> TIA,
> 
> John W. Colby
> Colby Consulting
> www.ColbyConsulting.com
> 
> _______________________________________________
> dba-VB mailing list
> dba-VB at databaseadvisors.com
> http://databaseadvisors.com/mailman/listinfo/dba-vb
> http://www.databaseadvisors.com
> 
_______________________________________________
dba-VB mailing list
dba-VB at databaseadvisors.com
http://databaseadvisors.com/mailman/listinfo/dba-vb
http://www.databaseadvisors.com





More information about the dba-VB mailing list