Pregunta

I am using textboxes to send text via SendKeys, but when I insert special characters in the textbox, my application crashes. For example, when I put in a '+' in the textbox, I get this error: SendKeys string '+' is not valid.

I need a solution to send special characters with SendKeys, this is a part of my code:

SendKeys.Send(dropDownEffectsLeft1.SelectedItem.ToString() + dropDownEffectsRight1.SelectedItem.ToString() + txt1.Text);

It's all about the textbox called txt1

I think I need something like a Regex to check if my txt contains any special characters, and that I will do with:

Regex specialChar = new Regex(@"^[a-zA-Z0-9_@.-]*$");

I really appreciate your help.

¿Fue útil?

Solución

From MSDN SendKeys:

The plus sign (+), caret (^), percent sign (%), tilde (~), and parentheses () have special meanings to SendKeys. To specify one of these characters, enclose it within braces ({}). For example, to specify the plus sign, use "{+}". To specify brace characters, use "{{}" and "{}}". Brackets ([ ]) have no special meaning to SendKeys, but you must enclose them in braces. In other applications, brackets do have a special meaning that might be significant when dynamic data exchange (DDE) occurs.

Code

So you just need a regex to replace those characters:

string txt = Regex.Replace(txt1.Text, "[+^%~()]", "{$0}");
SendKeys.Send(txt);

Test

I tested the code and I have an online test you can check out for the regex [+^%~()]

  • Input: Plus + Caret ^ Percent % Tilde ~ Parenthis ( )
  • Output Plus {+} Caret {^} Percent {%} Tilde {~} Parenthis {(} {)}

Otros consejos

There's one additional character that I have discovered which needs to be escaped - ~ (the tilde). I created a text file which includes all low and high ASCII and use this code to process the text:

              foreach (char c in pString) {
                 if (c.ToString() == "(")
                    SendKeys.SendWait("{(}");
                 else if (c.ToString() == ")")
                    SendKeys.SendWait("{)}");
                 else if (c.ToString() == "^")
                    SendKeys.SendWait("{^}");
                 else if (c.ToString() == "+")
                    SendKeys.SendWait("{+}");
                 else if (c.ToString() == "%")
                    SendKeys.SendWait("{%}");
                 else if (c.ToString() == "~")
                    SendKeys.SendWait("{~}");
                 else if (c.ToString() == "{")
                    SendKeys.SendWait("{{}");
                 else if (c.ToString() == "}")
                    SendKeys.SendWait("{}}");
                 else
                    SendKeys.SendWait(c.ToString());
              }

here's the test text:

the quick brown fox jumped over the lazy dog 123-456-7890 THE QUICK BROWN FOX JUMPED OVER THE LAZY DOG. The quick, brown dog: jumped over the; lazy dog! ¿Did the dog jump? ¡The value was ~200+- – that was a dash ! Of course there is that strange sign under - lowercase - the tilde `. [This is a footnote] but I cannot come up with an excuse for a {curly brace}. Of course, we know that the (left parentheses nor the right parentheses can be ignored). We need to email edgar@something.com to have him send me $1 which is about 50% of the actual value. Obviously there are 3 modifiers which will fail: ^ the caret; + the plus sign; % the percentage sign;. We also need to look at the & ampersand, the * (asterisk) and the _ underscore; then there is the = equal sign and the: colon. What will happen if we insert a tab in the middle of the sentence? Then of course we can have a | vertical bar or a \ back slash not to mention a / forward slash. The above was all a single line which was followed by a single new line to begin the sentence. Okay, what have I missed I # pound sign

And this starts a new paragraph by saying that after the last utterance. On top of all of that there is the in credible ball of wax that comes after the above which is - at least mostly - "low ASCII" - which covers the opening and closing quotation marks, but not the 'curly' ones - which are apostrophes. Then of course there is the "high ASCII": € ‚ ƒ „ … † ‡ ˆ ‰ Š ‹ Œ Ž ‘ ’ “ ” • – — ˜ ™ š › œ ž Ÿ ¡ ¢ £ ¤ ¥ ¦ § ¨ © ª « ¬ ­ ® ¯ ° ± ² ³ ´ µ ¶ · ¸ ¹ º » ¼ ½ ¾ ¿ À Á Â Ã Ä Å Æ Ç È É Ê Ë Ì Í Î Ï Ð Ñ Ò Ó Ô Õ Ö × Ø Ù Ú Û Ü Ý Þ ß à á â ã ä å æ ç è é ê ë ì í î ï ð ñ ò ó ô õ ö ÷ ø ù ú û ü ý þ ÿ not to mention tens, if not hundreds of thousands of Unicode characters!

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top