Toon Casteele
Toon Casteele

Reputation: 2579

Is there a way to set a control as a tooltip?

I'm wondering if it's possible to use ToolTip.SetToolTip or something similar to open a control as a tooltip instead of just a string (i.e. SetToolTip(controlToWhichToAdd, panelToDisplayAsToolTip) instead of passing a string as your second parameter).

If this isn't possible I'm guessing next best thing is displaying a panel on the mouse location on mouse_enter event on the control and removing it (or making it invisible) on mouse_leave.

Or are there other practices that make this possible in an easier way?

Upvotes: 1

Views: 453

Answers (3)

Icemanind
Icemanind

Reputation: 48736

This is not possible out of the box. You have two choices. First option is to override the Draw Event, which will let you customize how the tooltip looks. Here is an example of this. Be sure you set the OwnerDraw property to true if you use this method!

Although the first method will work if you just need some simple customization, the second option will work best if you need more flexible options. The second option is to do what you already suggested and create your own sort of tooltip. Simply put, you would first create an event handler for the MouseEnter event. When that event fires, you'd enable a Timer. This timer would be the delay that occurs before the tooltip is show. Then finally, you'd just make your panel appear at the mouse coordinates.

Suppose you have a form with a button and timer on it and you want the button to have a tooltip that is a panel:

public partial class Form1 : Form
{
    private Panel _myToolTipPanel;

    private void Form1_Load(object sender, EventArgs e)
    {
        _myToolTipPanel = new Panel {Visible = false};
        Controls.Add(_myToolTipPanel);

        Label myLabel = new Label();
        myLabel.Text = "Testing";
        _myToolTipPanel.Controls.Add(myLabel);
    }

    private void button1_MouseEnter(object sender, EventArgs e)
    {
        timer1.Enabled = true;
    }

    private void button1_MouseLeave(object sender, EventArgs e)
    {
        timer1.Enabled = false;
        _myToolTipPanel.Visible = false;
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        timer1.Enabled = false;
        Point position = Cursor.Position;
        Point formPoisition = PointToClient(position);

        _myToolTipPanel.Visible = true;
        _myToolTipPanel.Location = formPoisition;
    }
}

Now of course you will have to do some beautifying of the tooltip, but this is the general idea!

Upvotes: 1

Geoff
Geoff

Reputation: 8878

You could write a handler for the Tooltip.Popup event, and cancel the popup to display your own panel. You'd need to clean it up at the appropriate time, though.

For example:

private void ToolTip1_Popup(Object sender, PopupEventArgs e)
{
  e.Cancel = true;
  //Do work here to display whatever control you'd like
}

If you're just looking for more formatting options in the tooltip display, an alternative is something like this CodeProject entry, which implements an HTML-enabled tooltip:

enter image description here

Upvotes: 1

aked
aked

Reputation: 5835

One Approach could be inheriting the ToolTip control and then override the SetToolTip and Show methods . Inside the SetToolTip the private method - SetToolTipInternal needs to be re-written , but most of the functionality could be reuse - it uses the Mouse Events ( leave , move) to bind region. but since tooltip uses internal's of windows to show the baloon window. you will have to override quite a bit of code.

but this could be time consuming and needs quite a bit of testing.

Upvotes: 1

Related Questions