Thursday, November 15, 2007

To LiorZ with love

As promised, famous T-shirts have been printed. Here it comes

Picture 2

I can not judge the creativity of Lior, Ori and Gil, but I begin to produce such t-shirts. They are available in black and white in any size (M,L,XL and XXL) for 50 NIS (~12.5$) + postal costs (if you'll take it yourself nothing should be paid). If you want your own blog (or any other address) add another 5 NIS.

If you have (or have a connection to) print house and can make it cheaper - you're more, then welcome to help us with those t-shirts.

Number only TextBox

There are a lot of cases, you have to validate, that user inputs numbers only into TextBox. Someone things, that it's bad behavior to prevent user from typing well-known bad characters. I think, it's good. Let's take an example of form, you have to enter Social ID. Do you really think, that user expects the form tell him: "Hey, you, it's 9 digits only. You typed 'Z', which is not digit. Don't do it anymore...". I think, that the good behavior is when user types Z in text area for social id, nothing should happens. But, anyhow, it's up to you to decide.

Today, I want to explain how to build TextBox, that accepts only numbers in WPF.

In WinForms world, we used to handle KeyDown or KeyPressed event to get character and check whether it number. In WPF world, there are a lot of ways to input anything, that text (for example Voice recognition or handwriting. More then this, in WPF (device independent) world we can not be sure, that when you press key with 2 and @ in your keyboard without Shift pressed, you meant "2". Thus KeyDown or, even PreviewKeyDown event will not bring you salvation. We should handle input text without relation to input device. For this purpose, we have TextInput and PreviewTextInput event. Let's handle it

public class NumberTextBox:TextBox
    {

        protected override void OnPreviewTextInput(System.Windows.Input.TextCompositionEventArgs e)
        {
            e.Handled = !AreAllValidNumericChars(e.Text);
            base.OnPreviewTextInput(e);
        }

     }

Pretty cool, ah? What's about validation itself? "5" is definitely number, but what about "٥" ? Well, it's five in Arabic and it's number too. Is '½' is number? Sure, it's a half, but is it digit? No! This why we should use Char.IsDigit method, rather then Char.IsNumber which is not limited to radix 10 digits. The same behavior,   you'll get using Char.GetUnicodeCategory method to compare to UnicodeCategory.DecimalDigitNumber, but in this case, it will be current culture oriented.

Well. now we know if the input it decimal, what's about 1,000.00 numbers? The simplest way is to check whether the char is '.'{0x2e} or ',' {0x2c} and mark it as valid, however, what's about 1'00''00 or 100° which is absolutely valid numbers? NumberFormatInfo.CurrentInfo is your best friend in this case. You can always check my Globalization statements by using this handy program.

Now, when we sure, that everything is ok, we can write the test method

bool AreAllValidNumericChars(string str)
        {
            bool ret = true;
            if (str == System.Globalization.NumberFormatInfo.CurrentInfo.CurrencyDecimalSeparator |
                str == System.Globalization.NumberFormatInfo.CurrentInfo.CurrencyGroupSeparator |
                str == System.Globalization.NumberFormatInfo.CurrentInfo.CurrencySymbol |
                str == System.Globalization.NumberFormatInfo.CurrentInfo.NegativeSign |
                str == System.Globalization.NumberFormatInfo.CurrentInfo.NegativeInfinitySymbol |
                str == System.Globalization.NumberFormatInfo.CurrentInfo.NumberDecimalSeparator |
                str == System.Globalization.NumberFormatInfo.CurrentInfo.NumberGroupSeparator |
                str == System.Globalization.NumberFormatInfo.CurrentInfo.PercentDecimalSeparator |
                str == System.Globalization.NumberFormatInfo.CurrentInfo.PercentGroupSeparator |
                str == System.Globalization.NumberFormatInfo.CurrentInfo.PercentSymbol |
                str == System.Globalization.NumberFormatInfo.CurrentInfo.PerMilleSymbol |
                str == System.Globalization.NumberFormatInfo.CurrentInfo.PositiveInfinitySymbol |
                str == System.Globalization.NumberFormatInfo.CurrentInfo.PositiveSign)
                return ret;

            int l = str.Length;
            for (int i = 0; i < l; i++)
            {
                char ch = str[i];
                ret &= Char.IsDigit(ch);
            }

            return ret;
        }

The only thing we should do is handle drag and drop and paste whole text into the textbox. That's all folks. If you want to know how to handle and validate D&D and paste, let me know.

Friday, November 09, 2007

Call for action: Register Microsoft Developer Academy 2 - this is not about CRM

Shahar and Guy announced about open registration to 1032359731 (Microsoft Developer Academy 2). Please notice, it is not "תתחיל להכיר את הלקוחות שלך מקרוב - כנס CRM לארגונים תעשיה ביצוא", it's Developer Academy. So, sign up and attend at hall E at 2:45 PM - 4:00 PM for "WPF For Developers -  Lessons learn from last year (Or Optimizing Your WPF Application)" . Know, it's only 2.5 weeks from now - prepare your questions and bring your problems with you.

Developer Academy

It will take place in Airport city (near Ben-Gurion airport) at Avenue convention center, northern Kfar Truman on road 453. Here the map. Be sure to be there.

image

Thursday, November 08, 2007

Your potential, their T-shirts

Back to the Live event yesterday, I recalled old joke, prepared by me for Lior (Microsoft MARCOM) after Blogference, but never sent him. Today, using Microsoft Live Services, I can collaborate this image with him, while wearing Microsoft's T-shirt with slogan "The Jewish people are Live".

image

I'll probably, print this slogan (about T-shirts, not about Jewish people) one day on white T-shirt for my own...


I even prepaired images suitable for printing on material (TIFF, 300 dpi, black and white background). Download it, if you want to print such T-shirt for your own (or for me :) )

Visual Studio Windows Live Tools - November CTP

For all those, who want to incorporate Windows Live into their applications, new Windows Live Tools from Visual Studio will be great place to begin. Download November CTP on Microsoft Connect.

via LiveSide

Windows Live Tools

Wednesday, November 07, 2007

Windows Live Messenger Web API

A couple of hours after Windows Live has been announced, you can chat with me directly from my (or your blog. Enter and see the left side for Windows Live Messenger Web Control.

Want too? That's simple. First, allow others to see your presence. Then choose the control to use. Now (for those, who are using CS) add it into News section or other place in your blog (Spaces for sure).

image

New branded YourName@live.com is available

After a long wait, Microsoft Live is officially available. So, if you are in USA, you already can register new @LIVE.COM email address and have a new fresh start