How would one enter special characters into a Label in C# (Windows Forms)?
If you try to write a "&" into a label you'll get a sort of underscore instead..
So what's the C# equivalent of "&"? ("\&" obviously doesn't work).
Two ways:
Escape it with another ampersand (&&).
Set UseMnemonic for that label to false. This causes all ampersands within the text to be taken literally so you don't need to double any of them. You'll lose the underlining and access key features though.
You can set the value either in the designer, or in code:
myLabel.UseMnemonic = false;
myLabel.Text = "Text&Text";
Add another ampersand in front of it, so: &&
Or have a look at the following: Label.UseMnemonic (MSDN documentation)
You can escape & by adding it two times, to try &&.
I don't know how to use '&' in the designer, but in the code you can use '&&' for showing one '&'
Try this:
((char)0x26).ToString()
Related
var phone = #"^\+(?:[0-9] ?){6,14}[0-9]$";
phone will then equal ^\\+(?:[0-9] ?){6,14}[0-9]$
I thought (and the examples I found seem to show) the # character meant to leave my string how I have it. Why is it doubling the \ and how do I stop it?
The visual studio debugger will show it as if it were doubled, since in C# a \ would precede an escape sequence. Don't worry - your string is unchanged.
It only looks like it's doubled in the debug inspectors.
Note that the strings shown in the inspectors don't start with # - they are showing how you would have to write the string if you were to do it without the #. The two forms are equivalent.
If you're really worried about the contents of the string, output it in a console app.
To reiterate in another way, the comparison
var equal = #"^\+(?:[0-9] ?){6,14}[0-9]$" == "^\\+(?:[0-9] ?){6,14}[0-9]$"
will always be true. As would,
var equal = #"\" == "\\";
If you examine the variables using the Text Visualizer, you will be shown the plain unescaped string, as it was when you declared it verbatim.
In my windows phone project, I would like the user to enter his phone number in xxx-xxx-xxxx format. The country code it not required. I tried to implement regex, but i am not getting it right. I just want it to be displayed to the user as he enters it, nothing more, nothing less. This is what I have used
^\(\d{3}\) ?\d{3}( |-)?\d{4}$
But no matter what i put in, I always get this error (in this case 5) "Unrecognized escape sequence". I noticed, this is only with reference to the oblique. When I add a "" after it, the error goes away, but I do not get what I want. Is there a special way to input numbers in the textbox in than manner, on the XAML level?
Thanks in advance!
Put your regex inside verbatim string and also put the space, hyphen inside a group and make it as optional.
#"^\(\d{3}\)([- ]?)\d{3}\1\d{4}$"
DEMO
For testing your RegEx you can use this site: http://www.regexlib.com/RETester.aspx?AspxAutoDetectCookieSupport=1.
For your xxx-xxx-xxxx format I would use it:^\d{3}-?\d{3}-?\d{4}$
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Which passwordchar shows a black dot (•) in a winforms textbox?
Unicode encoding for string literals in C++11
I want to use code to reveal the password or make it a dot like •
textBoxNewPassword.PasswordChar = (char)0149;
How can I achieve this?
http://blog.billsdon.com/2011/04/dot-password-character-c/ suggests '\u25CF';
Or try copy pasting this •
(not exactly an answer to your question, but still)
You can also use the UseSystemPasswordChar property to select the default password character of the system:
textBoxNewPassword.UseSystemPasswordChar = true;
Often mapped to the dot, and always creating a consistent user experience.
You need to look into using the PasswordBox control and setting the PasswordChar as *.
Example:
textBox1.PasswordChar = '*'; // Set a text box for password input
Wikipedia has a table of similar symbols.
In C#, to make a char literal corresponding to U+2022 (for example) use '\u2022'. (It's also fine to cast an integer literal as you do in your question, (char)8226)
Late addition. The reason why your original approach was unsuccessful, is that the value 149 you had is not a Unicode code point. Instead it comes from Windows-1252, and Windows-1252 is not a subset of Unicode. In Unicode, decimal 149 means the C1 control code "Message Waiting".
You could translate from Windows-1252 with:
textBoxNewPassword.PasswordChar =
Encoding.GetEncoding("Windows-1252").GetString(new byte[] { 149, })[0];
but it is easier to use the Unicode value directly of course.
In newer versions of .NET, you need to call:
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
before you can use something like Encoding.GetEncoding("Windows-1252").
textBoxNewPassword.PasswordChar = '\u25CF';
I'm trying to set an error message which contains an ampersand via an ErrorProvider.
First attempt went like this :
errorProvider.SetError(someControl, "You have not accepted the Terms & Conditions");
This does not display the ampersand character. Googling suggested the following :
errorProvider.SetError(someControl, "You have not accepted the Terms &&& Conditions");
It works (i.e. displays one ampersand), but I would like to understand why it works. Any ideas ?
EDIT : for the System.Windows.Forms.Label control, there is a property called : UseMnemonic which can be set to False. Does ErrorProvider have anything like this ?
“&” is a special character in forms that is meant to underline the following character. So if you use it like:
myButton.Text = "&Close";
it will underline the 'C' character which will also become a hotkey. If you don't want that to happen, you use double ampersand like '&&' in your text.
I want to validate string containing only numbers. Easy validation? I added RegularExpressionValidator, with ValidationExpression="/d+".
Looks okay - but nothing validated when only space is entered! Even many spaces are validated okay. I don't need this to be mandatory.
I can trim on server, but cannot regular expression do everything!
This is by design and tends to throw many people off. The RegularExpressionValidator does not make a field mandatory and allows it to be blank and accepts whitespaces. The \d+ format is correct. Even using ^\d+$ will result in the same problem of allowing whitespace. The only way to force this to disallow whitespace is to also include a RequiredFieldValidator to operate on the same control.
This is per the RegularExpressionValidator documentation, which states:
Validation succeeds if the input
control is empty. If a value is
required for the associated input
control, use a RequiredFieldValidator
control in addition to the
RegularExpressionValidator control.
A regular expression check of the field in the code-behind would work as expected; this is only an issue with the RegularExpressionValidator. So you could conceivably use a CustomValidator instead and say args.IsValid = Regex.IsMatch(txtInput.Text, #"^\d+$") and if it contained whitespace then it would return false. But if that's the case why not just use the RequiredFieldValidator per the documentation and avoid writing custom code? Also a CustomValidator means a mandatory postback (unless you specify a client validation script with equivalent javascript regex).
Your question is a little hard to follow, but if you are asking how to write a regular expresion which only accepts digits I think your mistake is in using forward-slash instead of backslash. Try
"\d+"
The RegularExpressionValidator is a nice wrapper for doing regex checks but it will not validate against an empty control. You could use a CustomValidator and then do the regular expression check in a javascript function that you attach to the validator.
It will validate against an empty (blank) control as long as you set the ValidateEmptyText property to true.
You can read more about the CustomValidators on MSDN here.
try to use Ajax FilteredTextbox, this will not allow space.......
http://www.asp.net/ajaxLibrary/AjaxControlToolkitSampleSite/FilteredTextBox/FilteredTextBox.aspx
Try using ^\d+$ to force the digits to fill the entire line.
^ = line start $ = line end