0
votes

I am trying to make it so that when my C# application closes, it will save the window's size and location to the registry, then on startup it will change the window to those sizes. I am getting an error when I try it this way:

        Microsoft.Win32.RegistryKey key;
        key = Microsoft.Win32.Registry.CurrentUser.CreateSubKey("Software\\Pandora");
        //key.SetValue("Size", this.Size);
        //key.SetValue("Location", this.Location);
        //key.SetValue("Browser", Browser.Location);
        this.Size = new System.Drawing.Size(key.GetValue("Size").ToString());
        key.Close();

What do I need to do to set this.size to the "Size" Value?

==EDIT==

With

this.Size = new System.Drawing.Size(key.GetValue("Size").ToString(), Microsoft.Win32.RegistryValueKind.String);

I get the errors:

Error 1 The best overloaded method match for 'System.Drawing.Size.Size(int, int)' has some invalid arguments C:\Users\Sean\Documents\Visual Studio 2010\Projects\Pandora\Pandora\Form1.cs 23 25 Pandora

Error 2 Argument 1: cannot convert from 'string' to 'int' C:\Users\Sean\Documents\Visual Studio 2010\Projects\Pandora\Pandora\Form1.cs 23 49 Pandora

Error 3 Argument 2: cannot convert from 'Microsoft.Win32.RegistryValueKind' to 'int' C:\Users\Sean\Documents\Visual Studio 2010\Projects\Pandora\Pandora\Form1.cs 23 82 Pandora

If I change it to:

this.Size = new System.Drawing.Size(key.GetValue("Size").ToString());

The result is

Error 1 The best overloaded method match for 'System.Drawing.Size.Size(System.Drawing.Point)' has some invalid arguments C:\Users\Sean\Documents\Visual Studio 2010\Projects\Pandora\Pandora\Form1.cs 23 25 Pandora

Error 2 Argument 1: cannot convert from 'string' to 'System.Drawing.Point' C:\Users\Sean\Documents\Visual Studio 2010\Projects\Pandora\Pandora\Form1.cs 23 49 Pandora

And

this.Size = new System.Drawing.Size(key.GetValue("Size"));

Gives me

Error 1 The best overloaded method match for 'System.Drawing.Size.Size(System.Drawing.Point)' has some invalid arguments C:\Users\Sean\Documents\Visual Studio 2010\Projects\Pandora\Pandora\Form1.cs 23 25 Pandora

Error 2 Argument 1: cannot convert from 'object' to 'System.Drawing.Point' C:\Users\Sean\Documents\Visual Studio 2010\Projects\Pandora\Pandora\Form1.cs 23 49 Pandora

3
Why are you using the registry? .NET has a mechanism for application settings (XML file): msdn.microsoft.com/en-us/library/ms973902.aspx (MSDN -- also mentions registry settings). Also: csharpcity.com/…ashes999

3 Answers

2
votes

I agree that using the Registry is not the best idea. Nonetheless, you may want to consider separating the width and height rather than trying to store a type "Size"

Size size = this.Size;
int width = size.Width;
int height = size.Height;
// Now store "width" and "height" in their own key...
// ...
// ...
key.SetValue("SizeWidth", width.ToString(), Microsoft.Win32.RegistryValueKind.String);
key.SetValue("SizeHeight", height.ToString(), Microsoft.Win32.RegistryValueKind.String);

Also, what error do you get specifically?

1
votes

Your errors are because you cannot create a new "Size" with a string. The size is a complex datatype that requires two numbers (width and height) to be initialized. You are trying to initialize it with a single string.

For example: If the value in the registry were "30500," would it make sense to say this:

this.Size = 3500;

There is no way to tell what the dimensions of the window will be. You need to seperate the width and height and store them in two seperate keys (see my previous post).

When you are retrieving the values, you will need to parse them, as "Size" requires two ints and not two strings. Try something like:

string sHeight = key.GetValue("SizeHeight");
string sWidth  = key.GetValue("SizeWidth");
int iHeight    = Int32.Parse(sHeight);
int iWidth     = Int32.Parse(sWidth);
Size size      = new Size(iWidth, iHeight);
this.Size      = size;

You'll probably have to make a few adjustments to the code, but that should be a good place to start.

0
votes

Using:

static Size.Parse(string );