Textbox don't allow typing “ \ ” 2 times after

2019-09-25 06:37发布

This question is an exact duplicate of:

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 :)

3条回答
欢心
2楼-- · 2019-09-25 07:21

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, @"\");
        }
查看更多
Ridiculous、
3楼-- · 2019-09-25 07:29

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.

查看更多
乱世女痞
4楼-- · 2019-09-25 07:29

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).

查看更多
登录 后发表回答