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

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.

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...

Are u using Blend?

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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.