22 February, 2007

Resolving strange label behavior

I ran into a problem recently where I have something very much like map in my Windows Desktop application. The map-type thingy is bound to a dataset. The app draws items in a picture box based on the data in the dataset. What I needed was a label that would display information when the user moved the mouse over one of the items drawn on the form (like a tool tip).

Naturally, I used a dynamically created System.Windows.Forms.Label object. The user has the ability to determine what is displayed in the mouse over label. They could go to a settings form and set to display static text, values from data columns, or a combination of static text and column values (i.e. ID=234 - ID being static, 243 being the value from the dataset).

The problem I ran into was that the label generated and displayed properly when a data value was declared, but would only display the label border when only static text was to be displayed. The label has a border of BorderStyle.FixedSingle and the BackColor of Color.FromARGB(255,255,225) - this is the color of the standard ToolTip in Windows. but what was being displayed was just the outline of the label. No BackColor, no text. The label color would be whatever color was under where the label was supposed to be.

On a last ditch effort, I set the Label.Font property to this.Font. That fixed the problem. I don't know why it fixed the problem. I don't know what happened to the default font for the label, nor why the label BackColor was displaying as either Color.Transparent, or Color.Empty or whatever it was doing. But I do know that this fixed the problem.

I hope this helps someone out there.

Thanks for visiting!