Page 1 of 1

[Unity] Numeric TextBox

Posted: 16 Sep 2014, 13:42
by golgepapaz
Hi all,

I'm trying to create numeric textbox, so I register PreviewKeyDown event of textbox and set e.handled true when entered key is nonnumeric but text is changing.

Re: [Unity] Numeric TextBox

Posted: 18 Sep 2014, 16:27
by sfernandez
TextBox text is managed by TextInput event, so you have to filter characters in the PreviewTextInput event:
var tb = root.FindName<TextBox>("tb");
tb.PreviewTextInput += tb_PreviewTextInput;
void tb_PreviewTextInput(BaseComponent sender, TextCompositionEventArgs e)
{
    if (e.ch >= '0' && e.ch <= '9')
    {
        // numeric: OK!
    }
    else
    {
        e.handled = true;
    }
}