Monthly Archives: January 2012

A Different Approach to Flow Control and the Action Delegate

This originally started as a post about the Action delegate. But Recently on StackOverflow, I stumbled across an interesting technique for flow control which differs from the standard if/switch statements approach. The example I will show below uses the Action Delegate.

The Action delegate has a void return type and like many of the System delegates, it has a large number of overrides which cater for differing numbers of parameters:

In this example, I am using 2 different overrides of the Action delegate in conjunction with a couple of dictionaries. First up, I create the dictionaries:

    public partial class MainWindow : Window
    {
        private IDictionary<string, Action> clearControlsDelegateBucket;
        private IDictionary<string, Action<CanExecuteRoutedEventArgs>> buttonCanExecuteDelegateBucket;
        public MainWindow()
        {
            clearControlsDelegateBucket = new Dictionary<string, Action>(2, StringComparer.Ordinal)
                                              {
                                                  {"ClearTextBoxButton", ClearTextBox},
                                                  {"ClearAllControlsButton", ClearAllControls}
                                              };
            buttonCanExecuteDelegateBucket = new Dictionary<string, Action<CanExecuteRoutedEventArgs>>(2, StringComparer.Ordinal)
                                         {
                                             {"ClearTextBoxButton", TextBoxCanClear},
                                             {"ClearAllControlsButton", ControlsCanClear}
                                         };
            InitializeComponent();

The UI looks like this:

<Window x:Class="ActionDelegate.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:ActionDelegate.CustomCommands" Title="MainWindow" Height="145" Width="225">
        
    <Window.CommandBindings>
        <CommandBinding Command="local:ClearUiControlCommand.ClearControls" CanExecute="CanClear" Executed="ClearControls_Executed"></CommandBinding>
    </Window.CommandBindings>        
        
    <Grid>
        <StackPanel>
            <CheckBox x:Name="CheckedCheckBox" Content="This is initially checked" IsChecked="True" Margin="10,2,10,2" />
            <TextBox x:Name="TextBoxWithText" Text="This text is here" Margin="10,0,10,2" />
            <Label x:Name="LabelWithText" Content="This label content is here" Margin="10,0,10,2" />
            <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
                <Button x:Name="ClearTextBoxButton" Width="100" Content="Clear TextBox" Command="local:ClearUiControlCommand.ClearControls" />
                <Button x:Name="ClearAllControlsButton" Width="100"  Content="Clear All Controls" Command="local:ClearUiControlCommand.ClearControls"  />
            </StackPanel>
        </StackPanel>        
    </Grid>
</Window>

As you can see, I have bound the 2 clear buttons, ClearTextBoxButton and ClearAllControlsButton, to the same command ClearUiControlCommand.ClearControls. The Executed property of that custom command is set to the handler ClearControls_Executed. So, if the button can execute, it runs the following code:

        private void ClearControls_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            Button clickedButton = e.OriginalSource as Button;
            String buttonName = clickedButton.Name;

            clearControlsDelegateBucket[buttonName].Invoke();
        }

You can see there how it has accessed the relevant dictionary item by way of the key which we gave to it in the constructor. That key being the same as the name of the button which was clicked. I then invoke the Action delegate stored at that item in the collection.

So, there is an interesting alternative to conventional flow control, using the Action delegate and an IDictionary collection.

Get the code:

Visual Studio Shortcuts – Ones I Use

As with most people in programing, I am a shortcuts guy. They are great enablers. They enable me to work faster. And as I don’t touch type, this is an important productivity gain.

In this post, I want to list out a bunch of my favourite shortcuts. And I plan to update this post from time to time as I discover more great shortcuts. I am not going to bother with the ones everyone should know, like Ctrl+S to save.

I will also distinguish between normal shortcuts and multi-key (chord) shortcuts. Key chords will be depicted with a comma e.g. Ctrl+K, C

Visual Studio 2010

Out of the Box

Shift+Alt+Enter full screen.
F6 Build the whole solution.
Shift+F6 Build only the project which has context.
F7 when clicked on a mark-up window, will display the code-behind/code-file for that mark-up.
Ctrl+D+I brings up the immediates debugging window.
Ctrl+K, C comment out code.
Ctrl+K, U uncomment commented code.
Shift+Delete not only deletes the contents of a line of text, but also
completely removes the line.
Ctrl+W, B view the Bookmarks window.
Ctrl+F4 close the current tab.
Ctrl+K+K toggle creation/removal of a bookmark.
Ctrl+] go to end/start (toggle) of matching brace or region boundary
Ctrl+F6 Puts your cursor in the text editor windows. Useful if you are in a docked windows such as Bookmarks or Immediate.

Resharper

Ctrl+Shift+L In Solution Explorer, navigates to the file currently being
edited. This great shortcut obviates the need to do this.
Alt+PgDn go to next issue in file.
Alt+PgUp go to previous issue in file.
Shift+Alt+PgDn go to next error in file.
Shift+Alt+PgUp go to previous error in file.
Ctrl+T go to file.
Ctrl+Shift+T go to type.

Outlook

Ctrl+Q
mark as read.
Ctrl+U
mark as unread.

As I mentioned at the top of the post, more coming as I discover them.

Working on a Page With a Query String Parameter

Quite often in web development, we use query strings to pass values to a page. It is a convenient way to pass non-sensitive data to a page, and also makes the URL hackable (allowing users to plug in their own values in the URL and reloading the page for the information relevant to those values).

When developing these pages in ASP.NET webforms, there is a neat little trick that can be employed to save you stacks of time. You can configure   Visual Studio to load a particular URL, with a particular value locked and loaded in the Query String. This saves you having to load the page from which the user must navigate, select/enter values and click a submit button to kick off the navigation to the page which you are working on.

Here’s how:

  1. Click on the Web Project in Solution Explorer and holding down the ALT key, depress the Enter key (or just right-click and click Properties).
  2. Click on the Web tab.
  3. Then, select the Start URL radio button and enter in your URL plus query string as depicted below:

VisualStudioWebProperties

Once you have it all working nicely, you can go back and test the whole thing from the navigation of the previous page to this one.

If you are working on such a page for many hours (even days), this tip will save you heaps of time, and the tediousness of repeatedly clicking through to your destination page.