Practical DiskDiff in C#: Cleaning Up for the Parents

Although you may not mind if your friends see a big mess, you’d probably prefer that things are nicer for your parents. In the following sections, you’ll add a few things to make your appli­cation nicer.

1. Keyboard Accelerators

Accelerators are quite easy to add to an application; you just put an ampersand in front of whatever character is the accelerator key in the text.

2. Most Recently Used List

The most recently used (MRU) list is a nice way to access recently used documents. On the File menu, you can add entries (typically four) to hold the last four documents used and then use these to open those documents.

You can do the user interface side of this as you’d do any other menu item, but to store the items themselves, you’ll need to use the Windows Registry. For this example, you’ll encapsulate this access in the following class:

public class MRU {

ArrayList entries = new ArrayList();

string keyRoot = @”Software\Sample\DiskDiff”;

public MRU()

{

RegistryKey ourKey;

ourKey = Registry.CurrentUser.CreateSubKey(keyRoot);

for (int index = 0; index < 4; index++)

{

string keyName = “MRU_” + index;

string value = (string) ourKey.GetValue(keyName);

if (value != null)

{

entries.Insert(index, value);

Console.WriteLine(“{0} {1}”, index, value);

}

}

ourKey.Close();

}

public string this[int index]

{

get

{

if (index >= entries.Count)

return(“”);

else

return((string) entries[index]);

}

}

public void AddEntry(string entry)

{

entries.Insert(0, entry);

if (entries.Count > 4)

{

entries.RemoveAt(4);

}

Save();

}

void Save()

{

RegistryKey ourKey;

ourKey = Registry.CurrentUser.CreateSubKey(keyRoot);

for (int index = 0; index < entries.Count; index++)

{

string keyName = “MRU_” + index;

ourKey.SetValue(keyName, entries[index]);

}

ourKey.Close();

}

You can think of the Windows Registry as a hierarchical database in which a program can store values. We’re storing our information in the Software\Sample\DiskDiff key off the HKEY_CURRENT_USER root (which stores per-user customization).

To access information in the Windows Registry, you must first open a key at the specific level. In the constructor, the key is open and then each of the keys is looked up. To save the data to the Windows Registry, the process is reduced. The indexer retrieves the current values of the list, and the AddEntry() function adds an entry to the first entry of the list.

You then hook up this class into the rest of the application. The code to save an item now adds an entry to the MRU list, and you add a menu item handler to open the file when one of the items is chosen.

Using the Windows Registry is the traditional way of storing such information, but the . NET Framework provides a new method, using a config file, which is covered in the next section. The reason we present both alternatives in this chapter is that the Windows Registry is still a valid implementation option despite being mostly superceded by configuration files. The Windows Registry is more mature as a user setting store and generally works better in scenarios with roaming users. You can configure a Windows security domain so the HKEY_CURRENT_USER section of the Windows Registry is automatically downloaded to any machine that a user logs into; this way, their configuration settings are always available. By contrast, configuration files don’t have this level of support yet.

3. Most Recently Used List: A Configuration File Alternative

New to .NET 2.0 is the ability to use the configuration infrastructure to store per-user settings. Achieving this is simple, and it offers an alternative to the Windows Registry if you don’t require support for roaming users. To begin setting up a configuration file for DiskDiff, open the Properties page for the project, select the Settings tab, and add an MRU setting of type string with a User scope, as shown in Figure 37-2.

You can now write the configuration file-based MRU class using the same interface as the registry-based approach. In this case, the filenames are stored in a single setting and delineated with pipes:

using System;

using System.Collections.Generic;

using System.Configuration;

using DiskDiff.Properties;

namespace DiskDiff {

class MRU_Config {

List<String> entries = new List<string>();

Settings settings = new Settings();

public MRU_Config()

{

entries.AddRange(settings.MRU.Split(‘|’));

}

public string this[int index]

{

get

{

if (index >= entries.Count)

return (“”);

else

return entries[index];

}

}

public void AddEntry(string entry)

{

entries.Insert(0, entry);

if (entries.Count > 4)

{

entries.RemoveAt(4);

}

Save();

}

void Save()

{

foreach(string s in entries)

{
settings.MRU += (s + “|”);

}

settings.Save();

}

}

}

To modify DiskDiff to use the configuration file approach, you need to change a single line of code in Form1:

//was MRU mru = new MRU();

MRU_Config mru = new MRU_Config();

4. ToolTips

ToolTips make your user interface much easier to figure out and reduces the need for help. Adding them is easy in a Windows Forms application. Dragging a ToolTip control from the Toolbox to the form adds a ToolTip property to the properties collection of each control on the form. Merely set the ToolTip control’s text for each property, and you’re done.

Source: Gunnerson Eric, Wienholt Nick (2005), A Programmer’s Introduction to C# 2.0, Apress; 3rd edition.

Leave a Reply

Your email address will not be published. Required fields are marked *