How do I make negative signs appear on the left of numbers in a right to left box in C#?

StackOverflow https://stackoverflow.com/questions/23599391

  •  20-07-2023
  •  | 
  •  

Pregunta

I'm developing a calculator in C# and I ran into a stumbling block. As usual calculators do, I have my text box set to RightToLeft. However, when I convert the number to a negative number using my + - button, the negative sign always goes to the RIGHT of the number, not the left. How do I make it appear on the left side using a right to left box?

My negative converter:

decimal neg;
neg = number * 2;
number = number - neg;
richTextBox1.Text = Convert.ToString(number);
¿Fue útil?

Solución

As usual calculators do, I have my text box set to RightToLeft.

No, that’s not what usual calculators do. Set TextAlign instead. (And use a regular TextBox, not a RichTextBox. RichTextBoxes are hard to format, but you can select the entire thing and use SelectionAlignment if you really need to.)

Otros consejos

Right-to-left mode is used for certain languages such as Arabic, where people read and write starting from the right side of the page. So having the minus sign appear to the right is natural.

For your case, you want to set the alignment of the text instead.

richTextBox1.HorizontalContentAlignment = HorizontalAlignment.Right;

http://msdn.microsoft.com/en-us/library/system.windows.controls.control.horizontalcontentalignment.aspx

I think you might be modifying the incorrect property on your richTextBox. The code you want to use is this.

richTextBox1.SelectionAlignment = HorizontalAlignment.Right;
richTextBox1.SelectedText = Convert.ToString(number);
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top