-3

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

Ricje20
  • 3
  • 6
  • why don't you just use `textBox1.Text.Contains(@"\\")` to test if you got 2 \\next each other ? – Rémi Jun 19 '13 at 14:25
  • Please do not post twice the exact same question. – Pierre-Luc Pineault Jun 19 '13 at 14:28
  • Sorry pierre, i bumped into a problem and posted it tiwce, i didn't mean to do this – Ricje20 Jun 19 '13 at 14:42
  • 3
    This is a bad idea. First, paths are allowed to contain two backslashes, eg `\\myserver\myshare\mydirectory\myfile.txt`. Second, if you want users to browse folders, **create a folder browser control**, not a text box. Third, you're going to have to write code that handles malformed paths regardless; there are lots more ways to malform a path than just two backslashes, so don't bother restricting the input. Allow all the input and then give a good error message if its bad. Fifth, catching all exceptions is a bad programming practice; catch only the exceptions you intend to handle. – Eric Lippert Jun 19 '13 at 14:51
  • 1
    Sixth, eliminating double backslashes as people type leads to bad situations. For example, suppose I type `c:\d\e.txt` and then I realize whoops, I meant to write `c:\f\e.txt`, so I put the cursor after the `d`, hit backspace, the double-backslash disappears and now I end up with either `c:\fe.txt` or `c:f\e.txt`, neither of which is what I want. You're trying to be too clever here, and producing a user experience that is frustrating and unpredictable. – Eric Lippert Jun 19 '13 at 14:54
  • @EricLippert I think he just poorly worded what he wanted in the original question, he left a comment in keyboardP's answer which makes me think he really just wants to use a `ErrorProvider` and a check on the control's `Validating` event (and I added a answer explaining how to do do). – Scott Chamberlain Jun 19 '13 at 14:59
  • It would make things a Ton easier if i could post a screenshot >.< but i don't have enough reputation – Ricje20 Jun 25 '13 at 07:07

3 Answers3

3

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

keyboardP
  • 68,824
  • 13
  • 156
  • 205
0

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, @"\");
        }
Frank59
  • 3,141
  • 4
  • 31
  • 53
0

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.

Community
  • 1
  • 1
Scott Chamberlain
  • 124,994
  • 33
  • 282
  • 431