Thursday, October 25, 2007

Getting access to Settings in another project

One of the new cool features of Visual Studio 2005 is the new property editor. With this property editor you can easily add setting to your application. But ther is a problem the way its impelemented. Let me explain you why.

Usually the settings are specific to a project. When you add a setting in a project a special custom tool associate with the setting file generates a new class which you can use to access it. What is good about this class is it's strong typed. But behind the scene it's just getting a key from an xml file. This generated class is set as "internal sealed". This prevent from beeing accessed from any other assembly. What if you want to centralize where you edit these settings.

After many attempt to expose it I found a quick an easy way to do it. Let's say we have 2 project in our solution: an Engine and a WinApp. Each have settings but we want them to be editable from WinApp. Here is what it look like.




If you want to get access to Engine settings here th trick: Add a link file.

The link file will be compiled as part as you WinApp project. The setting class will still be internal and sealed but to WinApp project instead of Engine.

Here is the final result:


Notice that I addes a foler with the same name as my Engine project. This will be helpfull if you want to add settings from many projects.

With this in place you can access you engine setting the way from you engine class as from your WinApp class. You may omit the “Engine” part from your engine class because you should be in the same namespace. Here is what it should look like:

namespace WinApp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public void AccessConfig()
        {
            Engine.Properties.Settings.Default.EngineSetting = "test";
        }
    }
}

Wednesday, October 24, 2007

Editing a password in a PropertyGrid control

Sometimes, when something is too simple, you can spend hours to find out the solution. That happened to me yesterday as I was looking for a way to edit a password in a PropertyGrid control. As you should know, the PropertyGrid control can be customized by adding attributes on object's property you want to edit. One of this attribute is "Editor". With this attribute you can define a designer class that inherits from UITypeEditor to implement your own way to edit the value of a field in the property grid. I made some research on internet to find if somebody had build a special editor for password. No luck. Every time I was looking for page with "UITypeEditor and Password" I found many pages talking about UITypeEditor only. These pages were found because they all contain a "Forgot your password" link in it. Then I tied to search on Code Search. Here I found some interesting code that lead me to Enterprise Library, but it turns out to be a dead-end. Finally I used Reflector to search for "Password". This is where I found that simple attribute called "PasswordPropertyTextAttribute". So the solution was only to add this attribute to my password property and that's it!