Ram
Ram

Reputation: 933

'&' is being shown as '_' in a string c#

I want to set the error message to text box using error provider.

 string strNotAllowedError = "The following characters are not allowed:  \' # & < > or \"";
 errorProvider1.SetError(this.txtbx1, strNotAllowedError);

but the on the screen error message is become like : The following characters are not allowed:'# _ < > or" Here & is replaced by _ Please let me know how to show & in error message as it is.

Upvotes: 0

Views: 1101

Answers (4)

Suresh Kumar Veluswamy
Suresh Kumar Veluswamy

Reputation: 4363

can you try using &&& instead of & in the error message. Here is thread discussing about this http://www.codeproject.com/Forums/1649/Csharp.aspx?df=90&mpp=25&sort=Position&spc=Relaxed&select=3225091&tid=3225091

Upvotes: 0

Damien_The_Unbeliever
Damien_The_Unbeliever

Reputation: 239754

You need to double up the & character.

Traditionally, & in strings (that are going to be displayed in the windows UI) is used to precede the keyboard accelerator character. Which when rendered is underlined. If you'd not had a space after the &, you'd have seen whatever character followed underlined, rather than seeing it as the & being replaced by a _.

Upvotes: 2

Rahul Tripathi
Rahul Tripathi

Reputation: 172518

You may try this by doubling the ampersand &&:

string strNotAllowedError = "The following characters are not allowed:  \' # && < > or \"";

instead of this:

string strNotAllowedError = "The following characters are not allowed:\' # & < > or \"";

Upvotes: 1

Ehsan
Ehsan

Reputation: 32681

Use &&

this should do it

 string strNotAllowedError = "The following characters are not allowed:  \' # && < > or \"";
 errorProvider1.SetError(this.textBox1, strNotAllowedError);

Upvotes: 1

Related Questions