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 :)
You need to find all
\
-sequences (\\
,\\\
,\\\\
, ...) and replace that on\
. You can use regex for search sequencesSample:
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 thatCausesValidation
is true for the textboxThis 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.An easy way would be to simply replace the string.
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).