Tags

,

I thought it would be nice to provide some configuration options, such as font family and size, in a desktop application I’ve been working on recently.  I also wanted to provide a default option, and have that configured in the app.config file instead of having to recompile the application if I decide to (or get told to) change the font size, bold/un-bold, font family, etc.  I couldn’t, however, just set a value in the app settings and add keys with the name and size.   I needed to actually define a font setting.  But I’d never bothered to do that before.  So here’s what I did.

Right-click the project and select Properties, then navigate to the Settings tab and add a new System.Drawing.Font value:

I chose a nice big, bold Arial so it could be seen on a monitor stuck to the wall from across the room.

The value will look something like this in your app.config:

<applicationSettings>
    <MyApplication.Properties.Settings>
        <setting name="DisplayFont" serializeAs="String">
            <value>Arial, 15.75pt, style=Bold</value>
        </setting>
    </MyApplication.Properties.Settings>
</applicationSettings>

Finally, I added a little something like this to the form’s load method to grab the default font…at first:

private void Form1_Load(object sender, EventArgs e)
    {
        // set the font
        dg1.DefaultCellStyle.Font = Properties.Settings.Default.DisplayFont;

And that uses the default font of my choice, as set in the app.config, every time the application launches.  That way I could change the font setting in app.config any time I wanted and not have to recompile the application.  That was step one.

In the end, what I ended up doing was using the application level default value in Settings, and adding a user scope font setting also.  If the user scoped font value is null or it equals the default application scope font, I use the default font.  If the user scope font is different — read by saying something like

System.Drawing.Font a = Settings.Properties.User.MyFontValue;

anyway, if font a is different from font b (from Settings.Properties.Default.DisplayFont), dg1.DefaultCellStyle.Font = a;

And then I provided a font dialog that’s opened up from an option dropdown button and when you pick a font, Settings.Properties.User.MyFontValue is updated…

FontDialog Fdiag = new FontDialog();
if(Fdiag.ShowDialog() != DialogResult.Cancel )
    {
       Settings.Properties.User.MyFontValue=FDiag.Font
    }

Or something like that.  Pretty basic stuff, really, but I haven’t done a whole lot of desktop applications where I actually needed to provide…or save…options for the user…