The right to enter text in a text box?

I have a Currency Textbox with a mask. The mask is shown in Textbox as --------.--

Thus, the user type enters the numbers in the mask.

Now the client says that he does not want to type letters from left to right. He wants to print from right to left.

Similar to what we have in the calculator.

Now I tried to change the Textbox property of the righttoleft , but that does not help my reason.

In the end, I was stuck in handling a key event to manually change the position. I can change position, but stuck in completing logic.

This is what my code looks like:

  void Textbx_KeyDown(object sender, KeyEventArgs e) { String temp = T.Text; string temp2 = T.Text; int CursorIndex = T.SelectionStart - 1; for (int i = 0; i <= CursorIndex; i++) { if (i == 7) { temp2 = temp2.Insert(i, temp[i + 2].ToString()); temp2 = temp2.Remove(i, 2); //i = i + 2; } else if (CursorIndex == i) { temp2 = temp2.Remove(i, 1); temp2 = temp2.Insert(i, temp[i + 1].ToString()); } else { // T.Text = T.Text.Insert(i + 1, "_"); temp2 = temp2.Insert(i, temp[i + 1].ToString()); temp2 = temp2.Remove(i + 1, 1); } } T.Text = temp2; // T.Text = T.Text.Insert(CursorIndex-1, temp[CursorIndex].ToString()); if (CursorIndex != -1) T.SelectionStart = CursorIndex - 1; } 

Is there a better way to do this? If not, how do I need to finish the logic?

+6
source share
3 answers

In the text box there is a property:

 T.RightToLeft = RightToLeft.Yes 
+3
source

I wrote this code for you; try:

 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { string mychar = "000000"; string mtxt; int mypos = 6; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { maskedTextBox1.Text = mychar; } private void maskedTextBox1_KeyPress(object sender, KeyPressEventArgs e) { mtxt = mtxt + e.KeyChar; mypos--; mychar = mychar.Remove(mypos, mtxt.Length); mychar = mychar.Insert(mypos, mtxt); maskedTextBox1.Text = mychar; } } } 
+2
source

try to do this with maskedTextBox.

set the property TextMaskFormat = IncludePrompt

  private void maskedTextBox1_Click(object sender, EventArgs e) { maskedTextBox1.SelectionStart = maskedTextBox1.Mask.Length + 1; } private void maskedTextBox1_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar != (char) Keys.Back) { String a = maskedTextBox1.Text + e.KeyChar; maskedTextBox1.Text = a.Substring(1, a.Length - 1); maskedTextBox1.SelectionStart = maskedTextBox1.Mask.Length + 1; } } private void maskedTextBox1_KeyUp(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Back) { maskedTextBox1.Text = "_" + maskedTextBox1.Text; maskedTextBox1.SelectionStart = maskedTextBox1.Mask.Length + 1; } } 
0
source

Source: https://habr.com/ru/post/948671/


All Articles