1

Im trying to create a TextBox control and force user to enter only numbers in specyfic format there.

How can I do it in WPF?

I have not found any properties like "TextFormat" or "Format" in TextBox class.

I made TextBox like this (not in visual editor):

TextBox textBox = new TextBox();

I want TextBox behavior like in MS Access forms, (user can put only numbers in that textbox in "000.0" format for example).

2
  • stackoverflow.com/questions/3725189/… Commented Feb 17, 2014 at 3:30
  • @kenny Im creating control programatically, not in XAML. I don't know how to use information from link to do that programatically. Commented Feb 17, 2014 at 3:47

3 Answers 3

2

Consider using WPF's built in validation techniques. See this MSDN documentation on the ValidationRule class, and this how-to.

Sign up to request clarification or add additional context in comments.

Comments

2

What you probably need is a masked input. WPF doesn't have one, so you can either implement it yourself (by using validation, for example), or use one of available third-party controls:

Comments

0

Based on your clarification, you want to limit user input to be a number with decimal points. You also mentioned you are creating the TextBox programmatically.

Use the TextBox.PreviewTextInput event to determine the type of characters and validate the string inside the TextBox, and then use e.Handled to cancel the user input where appropriate.

This will do the trick:

public MainWindow()
{
    InitializeComponent();

    TextBox textBox = new TextBox();
    textBox.PreviewTextInput += TextBox_PreviewTextInput;
    this.SomeCanvas.Children.Add(textBox);
}

Meat and potatoes that does the validation:

void TextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
    // change this for more decimal places after the period
    const int maxDecimalLength = 2;

    // Let's first make sure the new letter is not illegal
    char newChar = char.Parse(e.Text);

    if (newChar != '.' && !Char.IsNumber(newChar))
    {
        e.Handled = true;
        return;
    }

    // combine TextBox current Text with the new character being added
    // and split by the period
    string text = (sender as TextBox).Text + e.Text;
    string[] textParts = text.Split(new char[] { '.' });

    // If more than one period, the number is invalid
    if (textParts.Length > 2) e.Handled = true;

    // validate if period has more than two digits after it
    if (textParts.Length == 2 && textParts[1].Length > maxDecimalLength) e.Handled = true;
}

6 Comments

Thanks, but thats not what I want.
@Kamil: You want to do the format in XAML? Explain better what you want... and realize it will be limited by what's possible :) All you've shown is one line of code that just instantiates a TextBox control. It really isn't much to go by.
I want something like format in MS Access forms, (user can put only numbers in that textbox).
So you want to limit the user input to only allow numbers, that makes sense. Please edit your question and put that in there.
It's not just about "allow only numbers". I want them in specyfic format (i want to determine how many decimal places should be there etc.)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.