What you see is best implemented by a ToolTip
.
To add a ToolTip
to a Control you usually do this:
- Add a
ToolTip
from the toolbox to the Form
- Now each control on the form as a new property field in the properties pane:
"ToolTip" on "yourToolTipName"
- Set a tooltip text for each control you want to show a tool tip.
This is really simple.
Therefore many folks believe that that is all a ToolTip
can do.
But it can be created and modified dynamically and styled in many ways. Showing a list of items is no problem at all..
To load it with data dynamically you code the MouseHover
event of your Label
:
private void yourLabel_MouseHover(object sender, EventArgs e)
{
toolTip1.SetToolTip(yourLabel, yourData);
}
Of course you may want to call a function to load the right and current data for each of your Labels
..: string loadData(Label lbl)
.
If you want to you can easily ownerdraw the ToolTip
. First code its Popup
event:
private void toolTip1_Popup(object sender, PopupEventArgs e)
{
toolTip1.BackColor = Color.LightGoldenrodYellow; // pick a Color if you want to
toolTip1.OwnerDraw = true;
}
Then the Draw
event:
private void toolTip1_Draw(object sender, DrawToolTipEventArgs e)
{
using (Font font = new Font("Consolas", e.Font.SizeInPoints))
{
e.DrawBackground();
e.DrawBorder();
e.Graphics.DrawString(e.ToolTipText, font, Brushes.DarkMagenta, e.Bounds);
}
Note that the ToolTip
will automatically resize to display the text. Also note that you can embed \n
characters to create line breaks.
I chose the monospaced font Consolas
, so I can create nicely aligned columns of text. Also note that if you want to enlarge the font you should add enough room via extra lines and/or space, this is because the size of the ToolTip
area is calculated from its original font size and you can't pick a different Font for it.. See here for a little more on that..
Also note that you do not need to set the property in the designer at all. The MouseHover
does all you need..