0

I have a Label Control then when mouse cursor is hovering around it for about 1second, it will show list of items in text format(the items are either plain text or it is also a control). It's like in the website Home/Contact Us/About Us/Help. What kind of control i'm going to use?

Like for example. www.ebay.com. Under "My Ebay", it will shows you the items, eg Summary/Bids/List/Messages...etc...

Hard Disk
  • 49
  • 2
  • 8
  • 3
    Ummmm... `ToolTip`? – Luaan Jun 09 '16 at 08:05
  • not a tooltip either, it it a list of items that hidden inside label control. it only show when mousecursor is in the controls area. it is like in the website where you can select in a list of items – Hard Disk Jun 09 '16 at 08:09
  • Why would that not be a tooltip? Other, obviously, than that your browser isn't running on winforms. Tooltips can be loaded dynamically! and they can even be owner-drawn to allow for various fonts etc.. – TaW Jun 09 '16 at 08:29

2 Answers2

3

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..

Community
  • 1
  • 1
TaW
  • 53,122
  • 8
  • 69
  • 111
1

Create ContextMenuStrip, set event handlers for items:

var contextMenu = new ContextMenuStrip();
contextMenu.Items.Add("Item 1");
contextMenu.Items.Add("Item 2");
contextMenu.Items.Add("Item 3");

Your ControlLabel:

var label = new Label { Parent = this, BorderStyle = BorderStyle.FixedSingle, Text = "Test" };

Show menu manually:

label.MouseHover += (s, e) =>
{
    contextMenu.Show(label, 0, label.Height);
};
Alexander Petrov
  • 13,457
  • 2
  • 20
  • 49