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

Ron Allen chizotz at mchsi.com
Wed Jul 18 11:53:45 CDT 2007


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
> 



More information about the dba-VB mailing list