Textbox don't allow typing “ \ ” 2 times after each other [duplicate]

☆樱花仙子☆ 提交于 2019-12-14 03:36:42

问题


I bumped into a problem, i hope someone can help me out :) i got a textbox, and i want to limit users so that it isn't allowed to have two \ after each other. i'm using it for folders. for example: C\temp\test\ now i want to make it not possible to type C\temp\test\\

i've tried searching some around for this problem but i couldn't find anyting like this. so i hope it's possible :)

heres a code of my textbox how it is now

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

I hope someone can give some examples and that I provided enough information :)


回答1:


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 \

If this is what you really want you need to use a ErrorProvider. Add one to your form then add the following code to the texbox's Validating event and be sure that CausesValidation is true for the textbox

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

This will make a ! show up next to the text box if they type wrong and force them to correct it when they attempt to leave the text box.




回答2:


An easy way would be to simply replace the string.

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

The extra code surrounding the replace ensures the cursor doesn't reset to the start of the textbox (which happens when you set the Text property).




回答3:


You need to find all \-sequences (\\,\\\,\\\\, ...) and replace that on \. You can use regex for search sequences

Sample:

      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, @"\");
        }


来源:https://stackoverflow.com/questions/17193770/textbox-dont-allow-typing-2-times-after-each-other

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!