What's a good approach for creating a dialog for entering an 8 digit pin number. It needs to work similar to a TextBox, but each digit should be shown in its own box. Is there an existing control that will do this? Is it possible to visually style a TextBox so that it looks like a series of boxes, one for each digit? Should a custom control be created with a TextBox for each digit with lots of event handling so that typing will move focus between cells and so that clipboard paste will distribute characters between each cell?
CodePudding user response:
You're correct, using a TextBox
is probably the best way to complete this. What you want is
<TextBox MaxLength="1"/>
This would allow only one character input per box. Simply line up x-amount of boxes for the length of the pin.
You could also do some work in the code-behind to automatically advance to the next when one had been filled so it is a smooth experience. I also suggest checking to make sure each TextBox
only receives numeric values (try { int.Parse(textbox.Text.ToString()) } catch (Exception) { // Not a number! }
) because there isn't a way to do that in XAML.
There are numeric input controls but those generally include buttons for the mouse and don't have the same feel.
Additionally as rbdeenk suggested you can use tab-index to allow the user to tab-through (though it might be smoother to have it auto-advance from the code-behind).
CodePudding user response:
I think you can bind the textbox changes on the same event via the designer. Then you implement a function that finds the current tab index and sets the tab index 1 when a number is pressed. When backspace is pressed it does the opposite. As far as basic controls go i'm not aware of any existing for what you are looking for.