How to create Numeric Textbox in Silverlight

No.of Views4545
Bookmarked0 times
Downloads 
Votes0
By  RRaveen   On  18 Aug 2010 11:08:57
Tag : Silver Light and XAML , Edit Controls
In this code snippet, explain about create Numeric Textbox in Silverlight
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

In this code snippet, explain about create Numeric Textbox in Silverlight. This is pretty simple work, but we have to do some extra work than, when we are create numeric Textbox in C# or Vb.NET.
Because in Silverlight, there are does not exist keypress event to the Textbox. So we have to use the keydown for this purpose.

Implementation

Step 1

As usual create Silverlight project with visual studio 2010 or 2008

Step 2

And then add a class calla as NumericTextBox. Then make you are have inherited from existing Textbox object to access exiting members in textbox.

Step 3

Just compose code like following way,

C# Code

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace SilverlightApplication2
{public class NumericTextBox : TextBox
    {protected override void OnKeyDown(KeyEventArgs e)
        {if (e.Key < Key.D0 || e.Key > Key.D9)
            {if (e.Key < Key.NumPad0 || e.Key > Key.NumPad9)
                {if (e.Key != Key.Back && e.Key != Key.Shift)
                    {
                        e.Handled = true;
                    }
                }
            }
        }

    }
}

VB.NET code

Imports System.Net
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Documents
Imports System.Windows.Ink
Imports System.Windows.Input
Imports System.Windows.Media
Imports System.Windows.Media.Animation
Imports System.Windows.Shapes

Namespace SilverlightApplication2Public Class NumericTextBoxInherits TextBoxProtected Overrides Sub OnKeyDown(e As KeyEventArgs)If e.Key < Key.D0 OrElse e.Key > Key.D9 ThenIf e.Key < Key.NumPad0 OrElse e.Key > Key.NumPad9 ThenIf e.Key <> Key.Back AndAlso e.Key <> Key.Shift Thene.Handled = TrueEnd IfEnd IfEnd IfEnd SubEnd ClassEnd Namespace

In above code, I have overridden the keydown event. Within override event, I have checked three conditions to ignore the non number keys. And also allow     Back and Shift key as well.

Then you can build this project, once you are build you can able to text in toolbox this control as like follows,

Image Loading

That's all, then you can drag and drop this in your page, you can use as it usual .NET controls.

Sample Project Source

Download source files -61 kb

 
Sign Up to vote for this article
 
About Author
 
RRaveen
Occupation-Software Engineer
Company-TGS
Member Type-Gold
Location-Singapore
Joined date-03 Jun 2009
Home Page-codegain.com
Blog Page-www.codegain.com
- B.Sc. degree in Computer Science. - 4+ years experience in Visual C#.net and VB.net - Obsessed in OOP style design and programming. - Designing and developing Network security tools. - Designing and developing a client/server application for sharing files among users in a way other than FTP protocol. - Designing and implementing GSM gateway applications and bulk messaging. - Windows Mobile and Symbian Programming - Having knowledge with ERP solutions
 
 
Other popularSectionarticles
Comments
By:RaveenDate Of Posted:4/20/2011 11:32:46 PM
To Him
Hi Kim, ooh nice, then you can solve easily, you add ASCII code blocking for @ sign. anyhow i will update.
By:KimDate Of Posted:2/25/2011 12:41:09 AM
This doesn't work
This doesn't work. Try Shift 2 and it allows "@" to be entered.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top