Problem display ampersands (&) in a panel

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I'm allowing the user to enter a string of text which then gets displayed in
a panel. It works great except that if they enter 1 ampersand then it does
not display correctly. So I thought it was just a matter of using the String
Replace function to fix the problem.

I've tried: text.Replace("&", "&&");

but it doesn't work. I've also tried other variations but can't get it to
work.

Does anyone have any ideas?
 
Two ampersands should do the trick. In the debugger, did you check to see
that the resulting string has two ampersands in it?

Post code.

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
 
Nick,

Here's the little function I wrote:

public static string FixPanelText(string text)
{
if (text.IndexOf("&") != -1)
text.Replace("&", @"&" + @"&");

return text;
}


The code you see on the primary line is the last thing I've tried. I also
tried:
text.Replace("&", "&&");
and:
text.Replace("&", @"&&");

and some other combinations but nothing seems to work.

I have an additional question for you: When I step through the code, I
should be able to clearly see that 'text' has two ampersands [after the
Replace statement], correct?

--
Robert W.
Vancouver, BC
www.mwtech.com
 
Robert W. said:
Here's the little function I wrote:

public static string FixPanelText(string text)
{
if (text.IndexOf("&") != -1)
text.Replace("&", @"&" + @"&");

return text;
}

That's the problem. String.Replace doesn't change the contents of the
string you call it on - it can't, as strings are immutable. Instead, it
returns a new string with the replacement carried out.

(In fact, the above would hang forever if you provided any text with an
ampersand, even if it were working - because if you start with
"foo&bar" and change it to "foo&&bar" you'll still have an ampersand in
there, so the loop would continue, etc.

Your method body should just be:

return text.Replace ("&", "&&");
 
Jon,

Of course, thank you. I need to drink more coffee, I think!

But just so you know, that function actually did not hang when passed an
ampersand.

--
Robert W.
Vancouver, BC
www.mwtech.com
 
Robert W. said:
Of course, thank you. I need to drink more coffee, I think!

But just so you know, that function actually did not hang when passed an
ampersand.

Oops - you're right, I thought it was a while loop rather than an "if".
Doh!
 
Hi Robert,


i suppose you set the Text prperty of a label.
Then you should set the UseMnemonic Property if the label to false.

Christof
 
Back
Top