"&"-sign in label

J

jez

Hi all,

I'm reading an xml file into a DataSet. In that xml file there are & in the
form of " & " as otherwise the xml file cannot be validated. When I want
to display a label in my CF app that reads from the dataset mentioned
earlier, and an ampersand is read the label will show Text (with the t
underlinded) text instead of Text & text.

However (and this is where this problem really gets to my nerves:p) I can
display a textBox correctly with the same data! That is : the & will
display correctly!

There are two options :

1) I get some other way to write an ampersand in the xml file that can be
read properly from all my controls
2) I use a textBox (which i do at the moment) but I need a way to change the
background color of the textBox even though the textBox is read-only
(basically make it act like a label).

Anyone's got an answer to that ?

thanks in advance:)
 
J

Joe Hanna

Hi Jez,

The ampersand is used in labels to create an accelerator key in order to
jump to a field quickly or activate a button. Eg If "&Click Me" was
assigned to the Text property of a button, ALT-C keyboard combination would
activate the button. If you want the literal ampersand to appear, you need
to escape it with another ampersand ie. &&

Perhaps as you assign the string to the label you can change all instances
of " & " to " && ", which will force the ampersand to display (only once of
course).

eg. using VB.NET:

Me.Label1.Text = Replace(DataRowField, " & ", " && ")

HTH

Joe
 
M

Mark Erikson

The ampersand is used in labels to create an accelerator key in order to
jump to a field quickly or activate a button. Eg If "&Click Me" was
assigned to the Text property of a button, ALT-C keyboard combination would
activate the button. If you want the literal ampersand to appear, you need
to escape it with another ampersand ie. &&

Perhaps as you assign the string to the label you can change all instances
of " & " to " && ", which will force the ampersand to display (only once of
course).

eg. using VB.NET:

Me.Label1.Text = Replace(DataRowField, " & ", " && ")

HTH

Joe


Unfortunately, that's not the case. You can't show an ampersand in a
menu using the Compact Framework. See item 5.8 in this MSDN FAQ:
http://msdn.microsoft.com/mobility/prodtechinfo/devtools/netcf/FAQ/#5.8
..

Annoying, I know. Any of you Microsofties have any idea if this
behavior will be changed at some point?

Mark Erikson
http://www.isquaredsoftware.com
 
J

jez

joe,

thanks for your reply. I tried the method in C# as follows :
label.Text.Replace("&", "&&");

it doesn't seem to work.. no replacement is taking place - is there maybe
another method ?
I tried to do text replace of some other letter (just to try it out) and it
doesn't work either..

thanks again
 
N

Neville Lang

Mark,

Originally, your statement was true, ampersands could not be used in menus
with CF. However, with the release of the latest SP2, ampersands CAN be used
in menus as I now have them working in my PPC app.

Maybe Geoff needs to update the MSDN FAQ.

Regards,
Neville Lang
 
J

Joe Hanna

Jez,

Using label.Text.Replace("&", "&&") will not work as you are trying to
replace the Text that has already been interpreted in the Text Property by
the Label Control.

My original suggestion (in VB.NET) was actually performing the Replace on a
String that was to be assigned to the Text Property of the Label, and had,
therefore, not been interpreted by the Label at that point. By using the
assignment operator (=), you are effectively copying the resultant string
(after manipulation) into the Text property.

Try this in C#:

string myString = "String Containing Words & an Ampersand";

label1.Text = myString.Replace(" & ", " && ");

/// The previous line effectively does this: label1.Text = "String
Containing Words && an Ampersand";


I have tested this code and it works fine. Note the space on either side of
the ampersands in both parameter strings. This is to avoid changing
ampersands that are already paired.

Joe
 
J

jez

Joe,

thanks so much! It works perfectly!
I had a look at the official CF FAQ, and indeed it says that you can't
display an ampersand. I guess they should try to update it. It would have
been very unfortunate for me if I were not able to show an ampersand in a
label as many of the products that need to be shown in my labels contain an
ampersand..

thanks again!
jez
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top