Right to left typing in text box?

12,395

Solution 1

There is a property for that in the textbox:

T.RightToLeft = RightToLeft.Yes

Solution 2

I have written this code for you; please try it:

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;
        }
    }
}
Share:
12,395

Related videos on Youtube

user1331032
Author by

user1331032

Updated on June 04, 2022

Comments

  • user1331032
    user1331032 almost 2 years

    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?

  • user1331032
    user1331032 almost 11 years
    Hi tried that but when I do that mask that I have changes to "--.--------" from "--------.--". So here it does the same thing as before that is typing from digit places towards decimal places. I want to let user type from decimal then towards unit,tens and so on. For typing 6.95. cursor will be at the right and user will just type 695 and it will shown as 6.95.