You are here:About>Computing & Technology>Visual Basic> Using VB.NET> Disappearing Label Text
About.comVisual Basic
Newsletters & RSSEmail to a friendSubmit to Digg

Disappearing Label Text

From Dan Mabbutt,
Your Guide to Visual Basic.
FREE Newsletter. Sign Up Now!

Graham, an About Visual Basic reader from the UK, has been sending me bugs that are so interesting that I just have to share. This one is about text that magically "disappears" when it's assigned to the Text property of a Label component.

Graham had a Label component in his form that was filled in with a randomly generated character selected from the "symbols" part of the ASCII chart (decimal 33 to 42). This includes

033 or ! (exclamation mark)
034 or " (double quote)
035 or # (number sign)
036 or $ (dollar sign)
037 or % (percent)
038 or & (ampersand)
039 or ' (single quote)
040 or ( (left/opening parenthesis)
041 or ) (right/closing parenthesis)
042 or * (asterisk)

Graham said that the label was blank whenever a decimal 38 happened to be generated. To add confusion, when the same character was later concatenated into a string and assigned to a TextBox, it wasn't blank anymore. The code that created the TextBox.Text property looked like this:

TextBox1.Text = Label1.Text & Label2.Text & Label3.Text

Graham's question was, "Is it possible that you can't use a this character in a Label?"

Doubting that this could actually be the problem, I added some debugging statements to test the code that generated the random character as follows:

For Sign = 0 To 8
   ' The bolded code below was added to ensure that
   ' I could retrieve the random number that was actually used
   Dim myRnd As Double
   myRnd = Rnd()

   intSign = CInt(Int((11 * myRnd) + 32)) 'Generate Random Signs
   ' This stops the loop when an decimal 38 is generated
   'Debug.Assert(intSign <> 38)
   arrSign(Sign) = Chr(intSign)
Next

This made it easy to step through the program for a few additional steps and see what actually happened when decimal 38 was generated.

I was forced to admit, it looked like you couldn't use decimal 38 in the Text property of a Label. I decided that was impossible so I checked the documentation at MSDN and discovered the UseMnemonic property. Athough this property has been part of Visual Basic since at least VB 6, I wasn't familiar with it. According to MSDN, UseMnemonic ...

"True if the label doesn't display the ampersand character and underlines the character after the ampersand in its displayed text and treats the underlined character as an access key; otherwise, false if the ampersand character is displayed in the text of the control. The default is true."

My emphasis added. Since decimal 38 happens to be &. This means that in a 1 character wide Text property, the default is for the displayed text to disappear altogether.

Pretty sneaky, Microsoft!

My new email pal Graham is beloved of most of the people in the UK due to his interesting program bugs. Check out another bug in an article inspired by one of his programs using the GoTo statement. Find out why "GoTo is considered harmful" at OOP and GoTo.

 All Topics | Email Article | | |
Advertising Info | News & Events | Work at About | SiteMap | Reprints | HelpOur Story | Be a Guide
User Agreement | Ethics Policy | Patent Info. | Privacy Policy©2008 About, Inc., A part of The New York Times Company. All rights reserved.