I am looking for a way to deny the user to type certain symbols into a name, because later I want to use that name to create a default filename or folder.
A mask would be nice, but I haven't found one.
I can trim from both ends, but not from the middle, and also trimming moves the cursor to the beginning of the line.
Any ideas
Regards,
Guido
private void myTextBox_TextChanged(object sender, EventArgs e)
{
if (myTextBox.Text.Contains("*")) myTextBox.Undo();}
Trimming strings
Delmer Johnson
Your link does not really help me, since it's a different language and it assumes a lot of knowledge that I don't have.
TomHope
you can look with regular expressions for illegal characters. You can search the string after the user put it in or is putting it in and you will get a objects containing informations about the found things in the string in a match collection. Then you can cycle through the match collection and react on the characters. Here is a linkt to the regex namespace:
http://msdn.microsoft.com/library/default.asp url=/library/en-us/script56/html/reconintroductiontoregularexpressions.asp
Or you can try MaskedTextBox class .net 2.0
THaines
Got it! All that remained to do was to change the Deselect call into a select Call.
private void myTextBox_TextChanged(object sender, EventArgs e)
{int x=0; string s1, s2;
if (myTextBox.Text.Contains("*"))
{// myTextBox.Undo(); // goes crazy
x = myTextBox.Text.IndexOf("*");
s1 = myTextBox.Text.Substring(0, x);
s2 = myTextBox.Text.Substring(x + 1);
myTextBox.Text = s1 + s2;
myTextBox.Select(x,0);}}
Regards,
Guido
Sangeetha_AS
That is a lot shorter!
Regards,
Guido
bsoft16834
private void myTextBox_TextChanged(object sender, EventArgs e)
{
int x=0; string s1, s2;if (myTextBox.Text.Contains("*"))
{// myTextBox.Undo(); // goes crazy
x = myTextBox.Text.IndexOf("*");
s1 = myTextBox.Text.Substring(0, x);
s2 = myTextBox.Text.Substring(x + 1);
myTextBox.Text = s1 + s2;
myTextBox.DeselectAll();}}
David Silverlight
private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if(e.KeyChar == '*')
{
e.Handled = true;
}
}