Angelos Petropoulos' WebLog

Thoughts occasionally worth reading

A more straightforward Microsoft Unity quickstart

clock April 22, 2008 14:54 by author angelosp

I recently downloaded and started playing with Unity, trying to see if it fits our project's needs. You can grab the installer from CodePlex, which also includes a couple of quickstarts.

Personally I found the bundled quickstarts too advanced and complicated for someone that hasn't used IoC frameworks before. In my opinion they try to demonstrate too much functionality at once. In any case, I decided to create a small project that would put Unity to use and I thought I'd share it here, as others may find it useful.

Enjoy

Simple Unity Quickstart.zip (23.13 kb)

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5


CodeFormatter - Format code, XML, etc. with syntax highlighting in HTML

clock September 26, 2007 15:29 by author angelosp

For ages I've been struggling with pasting code into my blog and having it look readable, let alone nice. I searched around the web for a solution and found a couple of Visual Studio add-ins (CopySourceAsHTML, Output Source Files in HTML Format), but they were not what I was looking for. Having a VS add-in to do this is cool, but it is not always the best solution.

  1. I am not always on my personal development machine and I may not have the add-in or even Visual Studio all together installed.
  2. I don't only post C# code. I also post SQL, XML, HTML, etc. It would be nice if the solution could cater for all of them.

After searching a bit more online I came across this cool solution, which is an online source code formatter. It can do C#, VB, SQL, XML, HTML and even MSH! It also has some nice little extras, such as alternate row highlighting and line numbers.

Unfortunately it didn't play nice with my blog. The output makes use of the "<pre>" HTML tag, which didn't agree with the engine my blog uses, so quite simply I couldn't use it. More searching online didn't yield any better alternatives, so I decided to take the plunge, crank open the code and mess with it until it works. While I was there I took the opportunity to fix a couple of CSS bugs related to line numbers and alternate row highlighting.

Here is the result.

I've made it available to everyone, so enjoy. You will notice that the biggest change is that by default the "<pre>" tag is not used in the HTML output, but the extra option allows you to turn that functionality back on.

Many thanks to manoli for making the source code available.

Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5


Using the .NET 2.0 Configuration framework for your application's settings

clock September 7, 2007 11:20 by author angelosp

This is a quick guide to get you up and running with the .NET 2.0 Configuration framework that also answers some of the frequently asked questions. If you are after an in-depth explanation of the .NET 2.0 Configuration framework you will probably find what you need in this article on The Code Project. It does a great job at explaining each part of the framework extensively, as well as give you some examples to work with. I recommend it.

Typical scenario

  1. Windows Forms application
  2. The application has some settings the users can change
  3. You want an easy and quick way of saving the application's settings to a file, loading them when he application starts and allowing the users to change them while the application is running.

One way to go about it would be to specify an XML file (e.g. ApplicationSettings.xml) and use the System.XML namespace and its classes to write to and read from it. It is not difficult and it will work, but you will be saving yourself some time and effort if you just leverage the .NET 2.0 Configuration Framework as it already does a lot of what you want to do.

Step 1 

First of all add an app.config to your application. Since the app.config file can be used to specify various .NET related settings and options for your application, I like to keep the real "application settings" seperate from it, so I use a seperate file for that. Create a new configuration section and point it to a seperate file by adding the following XML element in the app.config

<applicationSettings configSource="ApplicationSettings.xml" />

My new configuration section is called "applicationSettings" and its contents are in the file "ApplicationSettings.xml". This is a relative path, but a fully qualified path can also be used. Create the "ApplicationSettings.xml" file and add one of the settings in it.

<applicationSettings restorePositionOnOpen="true">
</applicationSettings>

I have chosen to add a setting that has a "true" or "false" value and is called "restorePositionOnOpen".

Step 2 

Now its time for some simple code. This is the main part were you will be saving yourself time if you use the .NET 2.0 Configuration framework. You will need a class that gives you programatic access to the application settings. All that is necessary is a new class that inherits from "ConfigurationSection" and some properties that will allow to get or set each application setting.

public class ApplicationSettings : ConfigurationSection
{
    [ConfigurationProperty("restorePositionOnOpen")]
    public bool RestorePositionOnOpen
    {
       get { return (bool)this["restorePositionOnOpen"]; }
       set { this["restorePositionOnOpen"] = value; }
    }
}

The class is called "ApplicationSettings" and inherits from "ConfigurationSection". It contains a single property called "RestorePositionOpen" that just gets or sets the value of the XML attribute "restorePositionOnOpen" in the "ApplicationSettings.xml" file. The property is annotated with the attribute "[ConfigurationProperty("storePositionOnOpen")]" so that the .NET 2.0 Configuration Framework knows how to bind the configuration section and the classes's properties.

Step 3

All that remains now is tell the  .NET 2.0 Configuration Framework to use the class "ApplicationSettings" for the configuration section "applicationSettings". This is done by adding the following XML element in the app.config file

<configSections>
     <section name="applicationSettings" type="Namespace.ApplicationSettings, AssemblyName" />
</configSections>

That is it, you now have an XML file separate to app.config that holds your application's settings and you can programmatically access it through the class "ApplicationSettings".

Saving changes

By default the configuration settings are read only, so when you try to set the value of a property an exception will be thrown. The solution is quite simple, you need to load the configuration using "OpenExeConfiguration(...)"

/// <summary>
/// Gets the application's settings.
/// </summary>
public
static ApplicationSettings Settings
{
    get
    {
        // If the configuration file has not been loaded yet
        if (MainForm.configuration == null)
        {
            // Load the configuration file from disk
            MainForm.configuration =
                ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
        }

        return (ApplicationSettings) configuration.Sections["applicationSettings"];
    }
}

By keeping a class scope reference of the configuration class, it is possible to save the changed made to the settings at any point necessary. For example

protected override void OnClosed(EventArgs e)
{
    // Perform the standard logic during close 
    base.OnClosed(e);
      
    // Save the changes made to the configuration file 
    MainForm.configuration.Save();
}


Beyond the basics

You can build on this to create a rather sophisticated solution for your application settings. Configuration settings that are accessed through properties can be marked as mandatory and you can even specify default values for when they are missing. This way you can introduce new settings to your application as part of a new version keeping compatibility with settings files from older versions. You can even have validation take place at the time of loading the values from the configuration file to ensure that it has not been changed outside the application to something invalid. Check out the ConfigurationPropertyAttribute spec for all this and more ...

You may decide to have different settings for each user, in which case you'd need to load the appropriate configuration for the user corrently logged in. Check out the details of the OpenExeConfiguration method for more information.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5


Placing images in a RichTextBox control

clock September 5, 2007 13:29 by author angelosp

For the past few weeks I have been playing around with a Windows Form application that I put together and just adding features to it incrementally. I started off with a simple TextBox control, I then changed to using a RichTextBox and finally I wanted to add images to the RichTextBox control. Having a look around for a way to do that I quickly realised that it is not as easy as one would expect it to be.

The easiest way to do it is to programmatically copy/paste the image inside the RichTextBox, but I won't even start on how crappy that solution is. It also lacks considerably in performance so it was never really an option for me.

After some searching I came across this article on The Code Project. It provides the code to insert an image in a RichTextBox control using OLE. This is a much cleaner approach that performs much better than the copy/paste option. I have created my own control that is heavily based on the code of this article so I can use it in all of my Windows Form applications. I thought it would be useful if I posted the source code for it.

Grab it here.

Currently rated 4.0 by 1 people

  • Currently 4/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5


DirectoryWatcher - A better FileSystemWatcher

clock September 4, 2007 12:47 by author angelosp

For a while now I been getting annoyed with how FileSystemWatcher works. To put it nicely, it does not do what you expect it to. I have created a wrapper that takes care of some of the annoyances, more specifically it does the following:

  • It ensures the appropriate event is fired. I don't want to get the "Changed" event when a file is created, I want to get the "Created" one instead! 
  • It ensures that no duplicate events are fired.

Grab it here, you are free to use it whichever way you want. 

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5


Subscribe

Syndicate
AddThis Feed Button
AddThis Social Bookmark Button

Search

Calendar

<<  August 2008  >>
SuMoTuWeThFrSa
272829303112
3456789
10111213141516
17181920212223
24252627282930
31123456

Archive

Tags

Categories


Blogroll

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2008

Sign in