Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Regex.Replace函数的最大长度_C#_.net_Winforms - Fatal编程技术网

C# Regex.Replace函数的最大长度

C# Regex.Replace函数的最大长度,c#,.net,winforms,C#,.net,Winforms,我使用该函数只允许文本框上的整数 texbox.Text=Regex.Replace(texbox.Text,[^0-9],”) 然后我需要在上面添加一个max length=4,但它不起作用 texbox.Text=Regex.Replace(texbox.Text,“[^0-9]{1,4}$”,”) 我想我错过了逻辑,但我无法解决它。我不想直接在属性上设置文本框的最大长度,因为当用户使用复制粘贴时,我需要整个输入 有关更多详细信息,请参阅下面的代码 //Focus to Next Contr

我使用该函数只允许
文本框
上的
整数

texbox.Text=Regex.Replace(texbox.Text,[^0-9],”)

然后我需要在上面添加一个
max length=4
,但它不起作用

texbox.Text=Regex.Replace(texbox.Text,“[^0-9]{1,4}$”,”)

我想我错过了逻辑,但我无法解决它。我不想直接在属性上设置文本框的最大长度,因为当用户使用
复制粘贴时,我需要整个输入

有关更多详细信息,请参阅下面的代码

//Focus to Next Control when max-length > 4
private void FocusToNextTab(object sender){
    TextBox txtBox = (TextBox)this.ActiveControl;
    if (txtBox.TextLength > 4){
        this.SelectNextControl((Control)sender, true, true, true, true);    
    }
}


private void OnTextBoxChanged(object sender, EventArgs e){

    var textBox = (TextBox)sender;
    string original_text = textBox.Text; //I will need this on user copy-paste. 

    var cursorPosition = textBox.SelectionStart;
    textBox.Text = Regex.Replace(textBox.Text, "[^0-9]{1,4}$", "");
    textBox.SelectionStart = cursorPosition;

    FocusToNextTab(sender);      

    ...
}

在你的正则表达式中,你应该

^[0-9]{1,4}

也许它会解决您的问题。

我怀疑这是否可以用
Regex.Replace()
完成

或者,您可以匹配字符串并连接捕获的值

string onlyFirstNDigits = "";
foreach (Capture capture in Regex.Match(textBox.Text, @"(?:\D*(?<digit>\d)){0,4}").Groups["digit"].Captures)
{
    onlyFirstNDigits += capture.Value;
}
textBox.Text = onlyFirstNDigits;

似乎适合这份工作

在我看来,在这种情况下,不使用正则表达式要容易得多

var textBox = (TextBox)sender;

string text = string.Concat(
    textBox.Text.Where(c => char.IsDigit(c)).Take(4)
);

var cursorPosition = textBox.SelectionStart;
textBox.Text = text;
textBox.SelectionStart = cursorPosition;

您可以使用只接受数字的自定义控件(例如,调用
int currentStyle=GetWindowLong(this.Handle,GWL_样式);SetWindowLong(this.Handle,GWL_样式,currentStyle | ES_NUMBER);
中的
OnHandleCreated
)。ErrorProvider还将通知TextBox仅接受数字。然后,将
WM_PASTE
处理为
SubString()
文本并保存原始粘贴。剩下的是文本长度。很多方法:如果(textBox1.TextLength>3&(e.KeyValue>39&e.KeyCode!=Keys.Delete)){e.SuppressKeyPress=true;}
在KeyDown事件中,或者处理
WM\u KeyDown
消息,或者…使用NumericUpDown而不是TextBox.@Jimi-你能告诉我更多吗?@AlexanderPetrov-要求使用文本框。NumericUpDown仅适用于数值。我需要处理复制粘贴,如果它不是整数,我需要做一些事情。我的意思是,由于要处理多个行为,因此在事件处理程序中尝试使用单个方法解决所有可能的结果很容易出错,并且在某些条件改变时很难适应。我的建议是创建一个专门的自定义控件,它只接受数字(如上所述,或者更好地在
CreateParams
中设置样式),处理
WM_PASTE
,这样您就可以预定义粘贴的字符串>具有可以用属性定义的最大输入值时会发生什么情况,然后,当用户粘贴一些文本时引发一个事件:该事件将返回原始文本和修改后的文本。如果没有美元符号,它将无法工作,并且将插入符号放在框括号外将反转条件。是。但在我的情况下,我需要处理复制粘贴或原始输入,无论其长度如何,如果在文本框的属性上设置了最大长度,我将无法处理它,因为它不允许有任何超过最大长度的内容。
var textBox = (TextBox)sender;

string text = string.Concat(
    textBox.Text.Where(c => char.IsDigit(c)).Take(4)
);

var cursorPosition = textBox.SelectionStart;
textBox.Text = text;
textBox.SelectionStart = cursorPosition;