Reputation: 19
I'm trying to make a simple calculator, and it has to focus on the last number so that when the text is too long, the user can still see what he/she is typing. Also, when the caret is not at the end of the string (arrow keys used), pressing a number button will put it back to the end of the string. How do you do this?
Upvotes: 2
Views: 1975
Reputation: 4464
Set the CaretIndex
to the end of input like this when text input is received.
TextBoxName.CaretIndex = TextBoxName.Text.Length - 1;
If you’re trying to create a calculator like that in Windows10
, then the textbox should be read only. You can set the TextAlignment
to Right
to display the text from right as in Windows calculator.
Since the textbox is readonly, when user clicks on some number that string needs to be concatenated to the textbox. The logic is very simple.
You can google for wpf calculator and find some good examples.
If you’re having a 2 textbox and 1 result textbox type calculator, then you can follow the method I mentioned above to achieve the expected behavior.
Upvotes: 3