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

Kamil
  • 13,363
  • 24
  • 88
  • 183
  • http://stackoverflow.com/questions/3725189/wpf-textbox-binding-with-stringformat-0f2-dont-show-zeros – kenny Feb 17 '14 at 03:30
  • @kenny Im creating control programatically, not in XAML. I don't know how to use information from link to do that programatically. – Kamil Feb 17 '14 at 03:47

3 Answers3

2

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

deloreyk
  • 1,248
  • 15
  • 24
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:

Community
  • 1
  • 1
Athari
  • 33,702
  • 16
  • 105
  • 146
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;
}
Dmitriy Khaykin
  • 5,238
  • 1
  • 20
  • 32
  • Thanks, but thats not what I want. – Kamil Feb 17 '14 at 03:46
  • 2
    @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. – Dmitriy Khaykin Feb 17 '14 at 03:47
  • I want something like format in MS Access forms, (user can put only numbers in that textbox). – Kamil Feb 17 '14 at 03:56
  • So you want to limit the user input to only allow numbers, that makes sense. Please edit your question and put that in there. – Dmitriy Khaykin Feb 17 '14 at 03:58
  • 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.) – Kamil Feb 17 '14 at 04:19
  • @Kamil: See my updated answer, I think this will do what you want and you can change it around for different decimal spaces. – Dmitriy Khaykin Feb 17 '14 at 04:49