2

I have two variables label and its value "LABEL_NAME=" and value and its value "A_B" I use replace function to remove "_" from label and replace it with space like

Dim label as String = "LABEL_NAME="
Dim value as String = "A_B"
label=label.Replace("_"," ")

and assign its value to LABEL control

dynamillabel.content=label & value

I expect the result as "LABEL NAME=A_B" but I get

"LABEL NAME=AB"

I am replacing _ of label variable , It removes "_" of value variable as well.

Edit : I want to concatenate two variables and assign it to Label.content . but before concatenating , I want to replace first variables "_" with space. when I do so It removes second variables "_" and above it the snippet I have used.

happy
  • 2,550
  • 17
  • 64
  • 109
  • 1
    And underscore in a Label indicates that it can be used as a keyboard shortcut (so it would mean that pressing ALT would put an underscore under the sign B and would activate any action that might be bound to it, or activate the textbox linked to this label), put 2 underscores in the text and it should be fine (kind of escape the underscore). – Icepickle Aug 30 '15 at 07:51
  • @varocarbas it only disappears in the sense that it thinks it should use a keyboard shortcut (so the underscore is still present in the value and in the content, it just gets replaced to act as a keyboard shortcut) – Icepickle Aug 30 '15 at 08:01
  • 1
    @varocarbas I don't know, the menus / labels / buttons / ... in winforms use & as a keyboard shortcut, so I guess it's something that you could have seen before ;) – Icepickle Aug 30 '15 at 08:12
  • Summary of what I have learned in the last minutes: there are some "reserved characters" in VB.NET (Winforms & WPF) for accessing controls, communicated via the string being displayed to user (e.g., for a Label; the properties content & Text for WPF & Winforms respectively); "_" in WPF and "&" in Winforms. If you type these characters without escaping them, they would not be displayed. For example: in case of writing Label1.content = "_555" (WPF) and Label1.Text = "&555" (Winforms), Label1 would only show "555". Although I don't like WPF (and do like Winforms), this seems a .NET "peculiarity". – varocarbas Aug 30 '15 at 08:28
  • Clarification (just in case): I am very experienced in .NET (mainly in Winforms, but even in WPF); and in quite a few other languages. And I haven't ever seen such a functionality anywhere (I think that I haven't ever used "&" or "_" in the displayed text of a control). I think that all this goes against the OO-programing and the separation of functionalities on account of properties (why not adding a new property?). Particularly curious in the very informative Visual Studio. Anyway... all we are here to learn (although next time I would prefer to learn something truly relevant :)). – varocarbas Aug 30 '15 at 15:57

2 Answers2

2

The underscore in WPF is used as a indicator for a keyboard shortcut. It replaces the & sign from winforms as in XAML & is invalid, and it would make it a bit annoying to write & for a keyboard shortcut

If you wish to use an underscore in your content, you should escape the underscore with another underscore.

Changing your code to this, should do the trick of showing your underscore:

Dim label as String = "LABEL_NAME="
Dim value as String = "A__B"
label=label.Replace("_"," ")

dynamillabel.content=label & value

You will now see the underscore

A similar question was also asked here, where you could use a custom template to disable this functionality

Disable WPF label accelerator key (text underscore is missing)

Community
  • 1
  • 1
Icepickle
  • 12,689
  • 3
  • 34
  • 48
2

An underscore is special in a Label. You need an extra Replace():

Dim text = label & value
text = text.Replace("_", "__")    
dynamillabel.content=text

Why it is special is explained pretty well in the MSDN article for Label, I'll just quote it:

This class provides both functional and visual support for access keys (also known as mnemonics). It is frequently used to enable quick keyboard access to controls such as a TextBox. To assign a Label to a Control, set the Target property to the control that should get focus when the user presses the access key. Setting the target also causes Microsoft UI Automation to use the text of the label as the name of the targeted control. For more information, see Accessibility.

To set the access key, add an underscore before the character that should be the access key. If your content has multiple underscore characters, only the first one is converted into an access key; the other underscores appear as normal text.

Consider TextBlock as an alternative.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536