Right to left typing in text box?

user1331032 picture user1331032 · Jul 4, 2013 · Viewed 11.8k times · Source

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

So user types in digits over the mask.

Now customer says he does not want to type in letter from left to right. He wants to type from right to left.

Similar to what we have in calculator.

Now I tried changing the textbox's righttoleft property but that does not help my cause.

In the end I am stuck with handling the key event to manually change the position. I am able to change the position but getting stuck completing the logic.

Here's how 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 should I go about completing the logic?

Answer

SysDragon picture SysDragon · Jul 4, 2013

There is a property for that in the textbox:

T.RightToLeft = RightToLeft.Yes