Can't make a numeric textbox with one comma or dot in c# wpf -
i'm making program finances management in wpf , need textboxes ones title describes.
before call duplicate, have searched in multiple sites , solutions proposed work fine numbers, don't seem recognize dot key or comma key (i use latin american qwerty keyboard), dont know if solutions tried region specific (because of keyboards) or on code.
so far have tried:
this, multiple regex have found around internet should have same result
the same before manually comparing e.keychar convert.tochar(".")
using keydown event , multiple if (e.key == key.dx) (this worked dot, not numbers , tried d0-d9 keys , oem ones)
none of these seem work me, , because need math numbers , because of purpose need them have decimal dot (or comma)
any or ideas appreciated.
you use standard textbox allows character, , use event handler on keydown or textchanged checks text illegal characters (anything other number, comma, or period). this:
private void textbox1_textchanged(object sender, eventargs e) { string result = ""; char[] validchars = new char[] {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ',', '.' }; // these ok foreach (char c in textbox1.text) // check each character in user's input { if (array.indexof(validchars, c) != -1) result += c; // if ok, add result } textbox1.text = result; // change text "clean" version illegal chars have been removed. }
Comments
Post a Comment