golgepapaz
Topic Author
Posts: 43
Joined: 01 Aug 2013, 01:59

[Unity] Numeric TextBox

16 Sep 2014, 13:42

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.
 
User avatar
sfernandez
Site Admin
Posts: 3222
Joined: 22 Dec 2011, 19:20

Re: [Unity] Numeric TextBox

18 Sep 2014, 16:27

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;
    }
}

Who is online

Users browsing this forum: Ahrefs [Bot], liangcheng and 3 guests