Numeric TextBox in WPF

.xaml Page

<TextBox HorizontalAlignment="Left"  Name="txtPassword" VerticalAlignment="Top" 
MaxLength="8"  PreviewKeyDown="check_space"  PreviewTextInput="textbox_input" />

.cs Page

using System.Text.RegularExpressions;

private void check_space(object sender, KeyEventArgs e)
{
try
{
if (e.Key == Key.Space)
{
e.Handled = true;
}
}
catch (Exception ex)
{
new MsgCommon(ex).ShowMsg(MsgCommon.ErrorMsgType.Error);
}
}

private void textbox_input(object sender, TextCompositionEventArgs e)
{
try
{
e.Handled = !IsValidTextNumber(e.Text);
}
catch (Exception ex)
{
new MsgCommon(ex).ShowMsg(MsgCommon.ErrorMsgType.Error);
}
}
private bool IsValidTextNumber(string p)
{
try
{
return Regex.Match(p, "^[0-9]*$").Success;
}
catch (Exception ex)
{
new MsgCommon(ex).ShowMsg(MsgCommon.ErrorMsgType.Error);
return false;
}
}

 

Advertisements
Categories: General | Tags: , , , , , | 1 Comment

Post navigation

One thought on “Numeric TextBox in WPF

  1. o5k4r.m4dr1d

    Thanks..!!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: