none
display ampercent (&) in label

    Question

  • Hi,

    the ampercent (&)  character couldn't be displayed in a label when set it's text to '&'.

    I didn't find any info about this in the web..

     

    anyone familiar with that issue?

     

    Oren.

     

     

    Wednesday, April 25, 2007 12:54 PM

Answers

  • Did you try to set its text to '&&' ?
    Thursday, April 26, 2007 8:05 AM
  • Hi Oren,

     

    You are correct with your observation about the difference in behaviour of a textbox vs a label.

     

    A Compact Framework label is simply a small wrapper around a native label (static) control. As such you can use PInvoke functionality to dive down into some of the lower level aspects of this control.

     

    This control has a window style called SS_NOPREFIX which can be toggled to disable or enable the handling of & characters. With this windows style the label (static) control will not interpret & characters as shortcuts.

     

    Here is a code sample (assuming CF2.0) which will add the SS_NOPREFIX window style to a label control.

     

    Code Snippet

    [DllImport("coredll.dll")]

    private static extern int GetWindowLong(IntPtr hWnd, int index);

     

    [DllImport("coredll.dll")]

    private static extern void SetWindowLong(IntPtr hWnd, int index, int value);

     

    private static readonly int GWL_STYLE = -16;

    private static readonly int SS_NOPREFIX = 0x00000080;

     

    private void EnableLabelPrefix(Label lbl, bool enable)

    {

         int style = GetWindowLong(lbl.Handle, GWL_STYLE);

         if (enable)

              style &= ~SS_NOPREFIX; // Remove the SS_NOPREFIX style

         else

              style |= SS_NOPREFIX; // Add the SS_NOPREFIX style

     

         SetWindowLong(lbl.Handle, GWL_STYLE, style);

         lbl.Refresh();

    }

     

    If you place this code within a form, you can pass a label to the EnableLabelPrefix method. If you pass in false for the second parameter then the label should display & characters rather than interpeting them as shortcut identifiers.

     

    The approach shown above is essentially mimicing a property you will find on the label control within the full framework called 'UseMnemonic' which performs the same action and is possibly implemented using a similiar technique internally. This is another example of the Compact Framework removing functionality which is easy enough to obtain by manually coding it on the few occassions where it is required.

     

    As an alternative (and as a way to stay within the existing compact framework confines) you could simply to a string search and replace and replace any & character with two &&'s, as the following example demonstrates

     

    Code Snippet

    label2.Text = label2.Text.Replace("&", "&&");

     

    I hope this helps,

    Christopher Fairbairn

    Monday, May 14, 2007 1:51 PM

All replies

  • Did you try to set its text to '&&' ?
    Thursday, April 26, 2007 8:05 AM
  • Hi,

    setting the text to '&&' works.

     

    what’s make the diff? (beside the extra character.. Smile )

     

    Oren.

     

    Sunday, May 13, 2007 12:27 PM
  • Hi,

     

    Code Snippet

    what’s make the diff? (beside the extra character.. )

      

    It's a common technique to "escape" special characters by repeating them a second time.

     

    Within a C# string for instance the \ character is used to indicate special characters (such as new lines aka \n and tabs aka \t). So if you want  a \ within a string you need to insert two \'s in a row i.e. \\, otherwise the compiler would attempt to interpret the next character as a special one.

     

    Likewise strings used for button labels etc use the & character to represent that the next character is a keyboard shortcut. To override this behaviour you need two &&'s in a row. Since & isnt' a valid keyboard shortcut the API decides to display a single & instead.

     

    Hope this helps your understanding,

    Christopher Fairbairn

    Sunday, May 13, 2007 10:44 PM
  • I ran into this when setting a textbox text and a label text(converting from XML ‘amp;’ ),  the text box displays it as is (a single am ampersand ), following your explanation I guess that a text box doesn't have a shortcut and labels do have.

    Do I have to parse the string before setting the label text or there is an API for this ?

     

    many thanks,

    Oren.    

    Monday, May 14, 2007 6:28 AM
  • Hi Oren,

     

    You are correct with your observation about the difference in behaviour of a textbox vs a label.

     

    A Compact Framework label is simply a small wrapper around a native label (static) control. As such you can use PInvoke functionality to dive down into some of the lower level aspects of this control.

     

    This control has a window style called SS_NOPREFIX which can be toggled to disable or enable the handling of & characters. With this windows style the label (static) control will not interpret & characters as shortcuts.

     

    Here is a code sample (assuming CF2.0) which will add the SS_NOPREFIX window style to a label control.

     

    Code Snippet

    [DllImport("coredll.dll")]

    private static extern int GetWindowLong(IntPtr hWnd, int index);

     

    [DllImport("coredll.dll")]

    private static extern void SetWindowLong(IntPtr hWnd, int index, int value);

     

    private static readonly int GWL_STYLE = -16;

    private static readonly int SS_NOPREFIX = 0x00000080;

     

    private void EnableLabelPrefix(Label lbl, bool enable)

    {

         int style = GetWindowLong(lbl.Handle, GWL_STYLE);

         if (enable)

              style &= ~SS_NOPREFIX; // Remove the SS_NOPREFIX style

         else

              style |= SS_NOPREFIX; // Add the SS_NOPREFIX style

     

         SetWindowLong(lbl.Handle, GWL_STYLE, style);

         lbl.Refresh();

    }

     

    If you place this code within a form, you can pass a label to the EnableLabelPrefix method. If you pass in false for the second parameter then the label should display & characters rather than interpeting them as shortcut identifiers.

     

    The approach shown above is essentially mimicing a property you will find on the label control within the full framework called 'UseMnemonic' which performs the same action and is possibly implemented using a similiar technique internally. This is another example of the Compact Framework removing functionality which is easy enough to obtain by manually coding it on the few occassions where it is required.

     

    As an alternative (and as a way to stay within the existing compact framework confines) you could simply to a string search and replace and replace any & character with two &&'s, as the following example demonstrates

     

    Code Snippet

    label2.Text = label2.Text.Replace("&", "&&");

     

    I hope this helps,

    Christopher Fairbairn

    Monday, May 14, 2007 1:51 PM
  • Hi Again,

     

    very helpful,

    Thank you for the professional explanation.

     

    Oren.

    Monday, May 14, 2007 3:46 PM
  • Hi,

    just set the UseMnemonic attribute of a Label to "false" and see the magic!

    Thursday, October 11, 2007 1:29 PM
  • Hi,

     

     Subhabrata wrote:
    just set the UseMnemonic attribute of a Label to "false" and see the magic!

     

    The UseMnemonic property is not available within the .NET Compact Framework, it is only present within the Desktop version of the framework.

     

    Hope this helps,

    Christopher Fairbairn

    Thursday, October 11, 2007 9:34 PM
  • Hi,

    just set the UseMnemonic attribute of a Label to "false" and see the magic!

    I tried this out on VISUAL BASIC .NET 2008

     

    I've tried several ideas I found on the internet

         &&   /   "&&Ampersand"   /   Chr(CInt(&H38)   /   Chr(38)   /    "\&"
                    lblI2C_ack.UseMnemonic = False ' Version 1


                    lblI2C_ack.Text = "Address  &&Ampersand " & "\& " & "&&Ampersand" & Chr(38) & "Data Ack received"

      lblI2C_ack.Text = lblI2C_ack.Text.Replace("&", "&&") ' Version 2

     

    but none seems to work ... do I something wrong?

     

    Thanks on beforehand

    Sunday, March 13, 2011 11:37 AM