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

Ron Allen chizotz at mchsi.com
Wed Jul 18 11:59:00 CDT 2007


Oops. Copy and paste error in the code I sent. The variable regPath should be 
the same in both the form open and form close events, and in the code I sent 
it wasn't. I copied the code out of a form where I do other things with the 
registry too, and copied the wrong line where the value of regPath is set for 
the form close event. Sorry.


----------------------  Original Message:  ---------------------
From:    chizotz at mchsi.com (Ron Allen)
To:      dba-vb at databaseadvisors.com
Subject: Re: [dba-VB] VB.Net XML - store form defaults
Date:    Wed, 18 Jul 2007 16:53:46 +0000

> 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");
> }





More information about the dba-VB mailing list