-5

I bumped into a problem, i hope someone can help me out :)
I got a TextBox, and i want to limit users, so that they can't write multiple \ one after another.
I'm using it for folders. For instance: C\temp\test\
Now I want to prevent input like: C\temp\test\\\

I've tried searching around for this problem, but I couldn't find anything like this, so I hope it's possible :)

I don't really have any code to show, but here's the code for my TextBox:

    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 I provided enough information :)

Nolonar
  • 5,962
  • 3
  • 36
  • 55
Ricje20
  • 3
  • 6

1 Answers1

0

If the textbox contains \\, it is invalid:

if (textBox1.Text.Contains(@"\\"))
{
     MessageBox.Show("Error!");
}
Ahmed KRAIEM
  • 10,267
  • 4
  • 30
  • 33
  • @X.L.Ant I almost, ***almost*** downvoted this answer for putting a wrong answer. `textBox1.Text.Contains("\\")` will always hold true, when you write paths into `textBox1`. The original answer was correct. – Nolonar Jun 19 '13 at 14:32
  • Shouldn't this be `textBox1.Text.Contains(@"\\")` or `textBox1.Text.Contains("\\\\"))`? edit; you fixed it and I am a slow typer. – AutomatedChaos Jun 19 '13 at 14:34
  • Hey, it doesn't work. i hope you can help me out som further. i tried doing if (textBox2.Text.Contains("\\, \\, \\\\, \\\\, \\\\, \\\\\\, \\\\\\\\")) { MessageBox.Show("error"); } – Ricje20 Jun 19 '13 at 14:34
  • @Ricje20 you know, '\\\' contains '\\' as do '\\\\' and so on... no need to test anything else than '\\'. Just try with what's provided. – xlecoustillier Jun 19 '13 at 14:39
  • @Ricje20 Your `Contains` will only return true, if your TextBox contains that string, for instance: `"hello \\, \\, \\\\, \\\\, \\\\, \\\\\\, \\\\\\\\ world"` would work, but not `"hello \\ world"` or `"hello \\\\ world"` – Nolonar Jun 19 '13 at 14:43