【问题标题】:Right to left typing in text box?从右到左在文本框中输入?
【发布时间】:2013-07-04 04:50:00
【问题描述】:

我有一个带面具的货币Textbox。 掩码在textbox 中显示为--------.--

所以用户在掩码上输入数字。

现在客户说他不想从左到右输入字母。 他想从右到左打字。

类似于我们的计算器。

现在我尝试更改 textboxrighttoleft 属性,但这对我的事业没有帮助。

最后,我坚持处理关键事件以手动更改位置。 我可以更改位置,但在完成逻辑时遇到了困难。

我的代码如下所示:

 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;


    }

有没有更好的方法来做到这一点?如果不是,我应该如何完成逻辑?

【问题讨论】:

    标签: c# .net winforms textbox maskedtextbox


    【解决方案1】:

    在文本框中有一个属性:

    T.RightToLeft = RightToLeft.Yes
    

    【讨论】:

    • 您好试过了,但是当我做那个掩码时,我从“--------.--”更改为“--.--------”。所以这里它和以前做的一样,就是从小数位到小数位输入。我想让用户从十进制输入到单位,十进制等。用于输入 6.95。光标将在右侧,用户只需键入 695,它将显示为 6.95。
    【解决方案2】:

    我已经为你编写了这段代码;请尝试一下:

    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;
            }
        }
    }
    

    【讨论】:

      【解决方案3】:

      使用 maskedTextBox 试试这个。

      设置 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;
              } 
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-01-05
        • 1970-01-01
        • 1970-01-01
        • 2014-12-07
        • 2014-05-31
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多