We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,415 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Numeric Box in WPF

Hello,

I am trying to have a numeric box in WPF toolbox, I have the following code which is giving me the control in the tool box, but I am unable make it accept only the numeric input.

protected override void OnKeyDown(KeyEventArgs e)
{
      short val;
      if (!Int16.TryParse(e.key.ToString(), out val))
      {
        e.Handled = true;
      }    
}

The problem I am facing is:

1 - When I press the numeric key 1 the value taken is D1 where it is treated as a character and displayed in Textbox
2- The Xaml designer is not coming up

 Window x:Class="TEST.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525" xmlns:my="clr-namespace:TEST">
    <Grid>
  <my:NumericTextBox Height="23" HorizontalAlignment="Left" Margin="183,128,0,0" x:Name="TextBox1" VerticalAlignment="Top" Width="75" />
    </Grid>
    </Window>

The error is

Error 1 Undefined CLR namespace. The 'clr-namespace' URI refers to a namespace 'TEST' that is not included in the assembly.
Error 2 The type 'my:NumericTextBox' was not found. Verify that you are not missing an assembly reference and that all referenced assemblies have been built.

However I am still able to run the app

I want to have a custom control for textbox accepting only numerics.Please suggest....

Regards

2
Contributors
5
Replies
5 Months
Discussion Span
7 Months Ago
Last Updated
8
Views
getnit
Junior Poster in Training
53 posts since Aug 2008
Reputation Points: 10
Solved Threads: 3
Skill Endorsements: 0

I want to have a custom control for textbox accepting only numerics.Please suggest....

Then why dont you try it on change event? Use a variable and Just check for every new character and remove it if its not numeric. if its numeric copy the value to your variable and if its not numeric then
textbox.text = yourVariable;

And if you are using blend, then I will suggest you to use Visual Studio to debug and even writing the code because its really hard and time consuming to figure out the error in Blend.

Sahil89
Posting Whiz in Training
202 posts since Sep 2011
Reputation Points: 10
Solved Threads: 22
Skill Endorsements: 1

Thank you for the response...But the point is I do not want to have the validation everytime I use the textbox, I would need a numeric box across the application and hence trying to have a custom component...

getnit
Junior Poster in Training
53 posts since Aug 2008
Reputation Points: 10
Solved Threads: 3
Skill Endorsements: 0

Are u using Blend?

Sahil89
Posting Whiz in Training
202 posts since Sep 2011
Reputation Points: 10
Solved Threads: 22
Skill Endorsements: 1

No I am not using Blend for this...I am doing it in VS

getnit
Junior Poster in Training
53 posts since Aug 2008
Reputation Points: 10
Solved Threads: 3
Skill Endorsements: 0

Have you referenced your user control? If not then go to projects > add reference
Then browse your control library and everything will work fine. When I removed the reference from my project I got same errors, So I think you forgot to add reference.

Sahil89
Posting Whiz in Training
202 posts since Sep 2011
Reputation Points: 10
Solved Threads: 22
Skill Endorsements: 1

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0683 seconds using 2.67MB