Numeric Textbox Control in WPF

by Bobby Cannon July 17, 2010 08:17 AM

I am in need of a small utility to do some testing so I decided I would write it in WPF. I'm not that familiar with WPF so I figured this would be a great little application to use to learn.

On my dialog I need an input for "Quantity". I looked for the old faithful NumbericUpDown control but it does not exist. I was sad but decided to push forward.

I placed a textbox down and started figuring out how to block non numeric values. This turned out to be completely different than I expected. Maybe my expectation where wrong. I thought I would start by using the PreviewTextInput event to ensure that the input was a number.

private void TextBoxQuantity_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
    e.Handled = !e.Text.All(Char.IsNumber);
}

This worked great... except it would not capture the space key (" "). Look as the possible input.

This would not work. I'm not entirely sure why the PreviewTextInput event is not catching the space key but we can fix this by using the PreviewKeyDown event. Let's take a look

private void TextBoxQuantity_PreviewKeyDown(object sender, KeyEventArgs e)
{
    if (e.Key == Key.Space)
    {
        e.Handled = true;
    }
}

Now the textbox will not accept the space input. At this point many would think we are done but we are not. Take a look at what happens if you "Paste" into the textbox.

Well that's no good. We cannot let the user paste text into a NumbericTextBox. We will disable the context menu (copy, cut, paste) and disable the paste command (CTRL + V).

This code will get rid of the context menu.

public MainWindow()
{
    InitializeComponent();
    TextBoxQuantity.ContextMenu = null;
}

This code will disable the paste command.

XAML

<TextBox CommandManager.PreviewExecuted="TextBoxQuantity_PreviewExecuted" />

Code

private void TextBoxQuantity_PreviewExecuted(object sender, ExecutedRoutedEventArgs e)
{
    var commandName = ((RoutedUICommand) (e.Command)).Text;
    if (commandName == "Paste")
    {
        e.Handled = true;
    }
}

This is acceptable for my small utility but I will need to figure out how to handle the accepting or rejecting of "pasted" data. It would be nice to still allow the user to paste number into the textbox.

Now we have a textbox that will only accept numeric numbers.

Tags: ,

Programming

Using the Windows Phone 7 Accent Color

by Bobby Cannon July 10, 2010 10:58 AM

Windows Phone 7 support themes that allow you to configure the color background of Dark (black) or Light (white) and a Accent color. Currently the phone will only support four colors of Orange, Blue, Red, and Green. The user will be able to select their favorite color of the four and the phone will change the mood of the phone. Below is an example of an application before and after implementing the feature.

To set the color of a TextBlock is very easy. Here is the code.

TextBlockListTitle.Foreground = new SolidColorBrush((Color) Resources["PhoneAccentColor"]);

Adding this feature to your application in specific areas will make your application feel more integrated with the Windows Phone 7 environment. The user experience goes up and the user will enjoy your application more therefore making both the user and you happy. Some may look at this at say this is just silly but user experience is very important and you should always be interesting in every little details of your application.

Tags: ,

Programming

Remove Regions with Resharper

by Bobby Cannon July 07, 2010 08:27 PM

I am an avid ReSharper user and I couldn't seem to figure out how to get the code cleanup to remove "regions". I found this little bit of info so I thought I would pass it on.

  • Go to ReSharper | Options
  • Under Language Options, expand C#, then Formatting Style
  • Select Type Members Layout
  • Uncheck "Use Default Patterns" and don't be alarmed at all the text that appears
  • Remove it all and add the text in the <code> block.
Code<?xml version="1.0" encoding="utf-8" ?>
<Patterns xmlns="urn:shemas-jetbrains-com:member-reordering-patterns">
  <Pattern RemoveAllRegions="true">
     <Entry />
  </Pattern>
</Patterns>

Tags: ,

Programming | Configuration

New Auto-Implementing Properties Feature in C# 3.0

by Bobby Cannon July 07, 2010 08:10 PM

This new Property feature is called "Auto-Implementing Properties". Below is a code snippet of the old way and the new way. The new way is so much cleaner and it takes away much of the grind in writing properties.

Code: New and Old Syntax for Properties

Tags: ,

Programming

Month List