简体   繁体   English

正则表达式前3位数字,左移

[英]RegEx to enfore 3 digits, padleft

I need to create an edit mask for a textbox that only allows digits 0-9 (on it's own, easy enough!), but must always be 3 digits long, and left padded with 0's if a number such as 69 is entered. 我需要为文本框创建一个编辑蒙版,该文本框仅允许使用0-9数字(靠它自己,足够简单!),但必须始终为3位数字,如果输入数字(例如69),则必须填充0。 It is used for a text box holding azimuth values, so I want a bearing such as 78 to show as 078 as the control loses focus. 它用于保存方位角值的文本框,因此当控件失去焦点时,我希望诸如78的方位显示为078。

The following has been suggested in another thread but that is not my requirement: 在另一个线程中提出了以下建议,但这不是我的要求:

someText = Regex.Replace(someText, @"\d+", n => n.Value.PadLeft(8, '0'));

At the moment I do the following in the Validated event, but I have feeling a RegEx edit mask may be more elegant: 目前,我在Validated事件中执行以下操作,但我感到RegEx编辑蒙版可能更优雅:

textBox.Text = textBox.Text.PadLeft(3, '0');

You can use a MaskedTextBox as well with 000 mask. 您也可以将MaskedTextBox000 mask一起使用。

Mask
Gets or sets the input mask to use at run time. 获取或设置在运行时使用的输入掩码。

And MaskedTextBox.Mask Property reference : MaskedTextBox.Mask Property参考

Masking element Description 遮罩元素 说明
0 Digit, required. 0位数字,必填。 This element will accept any single digit between 0 and 9. 该元素将接受0到9之间的任何一位数字。
9 Digit or space, optional. 9位数字或空格,可选。
# Digit or space, optional. #数字或空格,可选。 If this position is blank in the mask, it will be rendered as a space in the Text property. 如果该位置在蒙版中为空白,则它将在Text属性中呈现为空格。 Plus ( + ) and minus ( - ) signs are allowed. 允许使用加号( + )和减号( - )。

There are more options there. 那里还有更多选择。

Also, you can set the PromptChar to 0 to make it look nice (as if padded). 另外,您可以将PromptChar设置为0 ,以使其看起来不错(好像已填充)。

EDIT: 编辑:

I have just tried to create a function for TextChanged event, to achieve what you want without masked text box. 我刚刚尝试为TextChanged事件创建一个函数,以实现您想要的没有蒙版文本框的功能。 Just for an exercise: 只是为了锻炼:

private void textBox2_TextChanged(object sender, EventArgs e)
{
    var sel = textBox2.SelectionStart;
    if (string.IsNullOrWhiteSpace(textBox2.Text))
    {
        textBox2.Text = "000";
        return;
    }
    if (sel != 0)
    {
        var symbol_entered = textBox2.Text.Substring(sel - 1, 1);
        if (!Char.IsDigit(symbol_entered.ToCharArray()[0]))
        {
            sel--;
            textBox2.Text = textBox2.Text.Remove(sel, 1);
        }
        else
        { // entered a digit
            if (textBox2.Text.Length == 4 && textBox2.SelectionStart == 4) // last digit entered 
                textBox2.Text = textBox2.Text.Substring(0, 3).PadLeft(3, '0'); // trim it
            else if (textBox2.Text.Length == 4)
                textBox2.Text = textBox2.Text.Remove(sel, 1);
         }
    }
    if (!string.IsNullOrWhiteSpace(textBox2.Text)) // removed the first digit
    {
        textBox2.Text = textBox2.Text.PadLeft(3, '0'); // trim it
        textBox2.SelectionStart = sel;
    }
}

Have fun! 玩得开心! :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM