Whats wrong with my code? everytime i input 3 digit number in textbox, the focus jump to the front of 3 digit that. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Whats wrong with my code? everytime i input 3 digit number in textbox, the focus jump to the front of 3 digit that.

private void textBoxMoney_TextChanged(object sender, EventArgs e) { textBoxMoney.Text = string.Format("{0:n0}",double.Parse(textBoxMoney.Text); } output: the number should be like this 15,001 but in the output become like this 11,500 the number 1 in the back go to the front because the cursor lose focus

1st Sep 2019, 1:52 PM
hideyori
hideyori - avatar
4 Answers
+ 1
I want to get the thousand separator by doing string.Format
1st Sep 2019, 2:37 PM
hideyori
hideyori - avatar
+ 1
so what the best way to get thousand separator while typing in textbox in c#?
1st Sep 2019, 2:39 PM
hideyori
hideyori - avatar
0
I guess the cursor moves to the beginning of text because you changed the TextBox `Text` property every time the content changes. The content of the TextBox is redrawn entirely when it happens, and the cursor is moved to the left of text. I'm not sure what that '{0:n0}' means, can you tell me? also, what exactly are you trying to do with the code.
1st Sep 2019, 2:33 PM
Ipang
0
Honestly that was quite a tricky thing, probably the easiest way would be to display the formatted text when the TextBox loses focus, so it happens only once rather than on content change. A rather complex way (not even sure it'll work or is feasible), was to remember the cursor position before changing/formatting the text, and restore cursor position after the formatted text is displayed. That way the next input will go to the desired position (hopefully).
1st Sep 2019, 3:22 PM
Ipang