如何允许在填充的蒙版文本框上编辑编号?

时间:2017-08-23 17:48:32

标签: c# .net wpf xaml

我有一个蒙面文本框,我想在文本框的中间编辑一个数字,将右边的数字改为我输入的数字。

例如:

111.511-1 / 11

我想将这个数字改为5到9.所以我要键入de left arrow,直到我靠近圆点。当我靠近点并输入9时,数字5将消失,数字9将出现。

111.911-1 / 11

我认为我需要捕捉左箭头键入以进行更改。但我不知道如何抓住它。

任何代码建议?

XAML

     <TextBox TextChanged="FormataProcesso" KeyDown="NumberOnly"/>

C#

     private void FormataProcesso(object sender, EventArgs e)
        {
            Regex regex1 = new Regex("[0-9]{4}$");
            Regex regex2 = new Regex("[0-9]{3}.[0-9]{4}$");
                Regex regex3 = new Regex("[0-9]{3}.[0-9]{3}-[0-9]{2}$");
    }



    if (regex3.IsMatch(process.Text))
                {
                    isValorTratado = true;
                    processo.Text = string.Format("{0}/{1}", process.Text.Substring(0, 9), process.Text.Substring(9, 1));

                    process.SelectionStart = process.Text.Length;
                    process.SelectionLength = 0;
                }
                else if (regex2.IsMatch(process.Text))
                {

                    processo.Text = string.Format("{0}-{1}", process.Text.Substring(0, 7), process.Text.Substring(7, 1));

                    process.SelectionStart = process.Text.Length;
                    process.SelectionLength = 0;
                }


....

1 个答案:

答案 0 :(得分:0)

感谢您的更正,伙计。

我用过它:

            int position = numProcess.SelectionStart;

            if (numProcess.Text.Length <= position)
            {
               ...
            }
            else
            {
                string firstPiece = numProcess.Text.Substring(0, position);
                string secondPiece = numProcess.Text.Substring(position + 1);

                numProcess.Text = firstPiece + secondPiece ;

                numProcess.SelectionStart = position;
                numProcess.SelectionLength = 0;
            }