【问题标题】:Textbox don't allow typing " \ " 2 times after each other [duplicate]文本框不允许在彼此之后输入“\”2次[重复]
【发布时间】:2013-06-19 14:21:36
【问题描述】:

我遇到了一个问题,希望有人能帮助我:) 我有一个文本框,我想限制用户,这样就不允许有两个 \ 在彼此之后。 我将它用于文件夹。例如:C\temp\test\ 现在我想让它无法输入 C\temp\test\\

我已经尝试过搜索这个问题,但我找不到任何类似的东西。所以我希望这是可能的:)

这是我的文本框现在的代码

    private void textBox1_TextChanged(object sender, EventArgs e)
    {
        try
        {
            Regex regex = new Regex(@"[^C^D^A^E^H^S^T^]");
            MatchCollection matches = regex.Matches(textBox1.Text);
            if (matches.Count > 0)
            {
                MessageBox.Show("Character niet toegestaan!");
                textBox1.Text = "";
            }

            clsOpslagMedium objOpslag;  // definieert type object 
            objOpslag = new clsOpslagMedium();  // creert opject in memory
            objOpslag.DriveLetterString = textBox1.Text;
        }
        catch (Exception variableEx1)
        {
            MessageBox.Show("Foutmelding: " + variableEx1.Message);
        }
    }

我希望有人可以举一些例子,并且我提供了足够的信息:)

【问题讨论】:

  • 你为什么不直接使用textBox1.Text.Contains(@"\\") 来测试你是否有两个 \\next 呢?
  • 请不要发布两次完全相同的问题。
  • 对不起,pierre,我遇到了问题并发布了两次,我不是故意的
  • 这是个坏主意。首先,路径允许包含两个反斜杠,例如\\myserver\myshare\mydirectory\myfile.txt。其次,如果您希望用户浏览文件夹,请创建文件夹浏览器控件,而不是文本框。第三,无论如何,您都必须编写处理格式错误的路径的代码;除了两个反斜杠之外,还有很多方法可以使路径变形,所以不要费心限制输入。允许所有输入,然后在错误时给出一个好的错误消息。第五,捕获所有异常是一种不好的编程习惯;仅捕获您打算处理的异常。
  • 第六,在人们键入时消除双反斜杠会导致糟糕的情况。例如,假设我输入c:\d\e.txt,然后我意识到哎呀,我的意思是写c:\f\e.txt,所以我把光标放在d之后,按退格键,双反斜杠消失,现在我最终得到@ 987654327@ 或 c:f\e.txt,这两个都不是我想要的。你在这里太聪明了,并且产生了令人沮丧和不可预测的用户体验。

标签: c# textbox character


【解决方案1】:

一个简单的方法是简单地替换字符串。

private void textBox1_TextChanged(object sender, EventArgs e)
{
    //get the current cursor position so we can reset it 
    int start = textBox1.SelectionStart;

    textBox1.Text = Regex.Replace(textBox1.Text, @"\\\\+", @"\");

    //make sure the cursor does reset to the beginning
    textBox1.Select(start, 0);
}

围绕替换的额外代码确保光标不会重置到文本框的开头(当您设置 Text 属性时会发生这种情况)。

【讨论】:

  • 如果有人粘贴了 "c:\temp\\\" 会起作用吗?
  • 是的,它将用一个实例替换任意数量的连续反斜杠。
  • 他可以循环测试while (textBox1.Text.Contains(@"\\")){textBox1.Text = Regex.Replace(textBox1.Text, @"\\+", @"\");}
  • @Ricje20 - 你说“不允许打字”和“不能打字”。所以现在你正在改变要求......
  • 我认为你的模式匹配了不必要的匹配,@"\\\\+" 会更好。你需要加倍,因为正则表达式引擎也需要它们转义,就像你需要不包括 @ 一样(如果你不包括 @ 你的模式需要\\\\\\\\+
【解决方案2】:

a textreplace isn't working in my case. i need an error shown up when a user leaves the box when he types more then one \

如果这是您真正想要的,您需要使用ErrorProvider。在表单中添加一个,然后将以下代码添加到 texbox 的 Validating 事件中,并确保 CausesValidation 对于文本框是正确的

private void textBox1_Validating(object sender, CancelEventArgs e)
{
    if(Regex.IsMatch(textBox1.Text, @"\\\\+"))
    {
        e.Cancel = true;
        errorProvider1.SetError(textbox1, @"\\ Is not allowed");
    }
    else
    {
        errorProvider1.SetError(textbox1, null);
    }
}

如果他们输入错误,这将使! 显示在文本框旁边,并在他们尝试离开文本框时强制他们更正。

【讨论】:

    【解决方案3】:

    您需要找到所有\-sequences (\\,\\\,\\\\, ...) 并将其替换为\。您可以将正则表达式用于搜索序列

    示例:

          string test=@"c:\\\adas\\dasda\\\\\\\ergreg\\gwege";
           Regex regex = new Regex(@"\\*");
    
    
           MatchCollection matches = regex.Matches(test);
            foreach (Match match in matches)
            {
                if (match.Value!=string.Empty)
                    test = ReplaceFirst(test, match.Value, @"\");
            }
    

    【讨论】:

    • 你能举个小例子吗? ~谢谢你的回答!
    猜你喜欢
    • 1970-01-01
    • 2011-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多