views:

288

answers:

2

I have a dialog box with controls that is popped up and when a control is moused over the controls a tooltip is displayed. However, if I close the box then re-display it no tooltips will work. Here is a portion of my code. I am initializing tooltipOn when the form is loaded to null. I have done a trace and tooltip1.Show() does get called the second time it simply never displays. Any idea why?

private void Panel1_MouseMove(object sender, MouseEventArgs e)
{
    Control ctrl = null;

    if (sender == Panel1)
        ctrl = ((Control)sender).GetChildAtPoint(e.Location);
    else
        ctrl = (Control)sender;

    if (ctrl != null)
    {
        if (tooltipOn != ctrl)
        {
            toolTip1.Show(toolTip1.GetToolTip(ctrl), ctrl, ctrl.Width / 2, ctrl.Height / 2);
            tooltipOn = ctrl;
        }
    }
    else
    {
        toolTip1.Hide(this);
        tooltipOn = null;
    }
}
+1  A: 

Maybe because you can't show the tooltip twice on two different controls?

Try this inside your if statement:

if (tooltipOn != ctrl)
{
    //your moving the tooltip to a different control, 
    //hide it from the other first.
    if (tooltipOn != null)
        toolTip1.Hide(tooltipOn);  

    toolTip1.Show(
        toolTip1.GetToolTip(ctrl), ctrl, ctrl.Width / 2, ctrl.Height / 2
    );

    tooltipOn = ctrl;
}

If that doesn't work I would try newing up an entirely different tool tip altogether to make sure each control get's their own during the event.

Joseph
That worked absolutely! I'm still not sure exactly what was going on though. Could you explain to me why the code worked the first time my form is shown but not the second?
novacara
Actually you know what? This only works if I haven't moused over the control the first time the form was displayed.
novacara
I think it has to do with the fact that you're trying to use the same tooltip across multiple controls. Typically each control gets their own tooltip. However, the way you're doing it you only create one tooltip (from what I can tell), and then just move it around to which ever control has the mouse over it.
Joseph
Yes that is true. However, I have way too many controls to give them each their own tooltip. I think it is more a tooltip is buggy problem then anything. That's why I don't understand why your code worked because tooltipOn is already null when that if statement is hit. I tried hiding the tooltip for each ctrl when closing the form and also deactivating/activating the tooltip on a controls mouseEnter event. What I end up with is weird behavior where the tooltips will show up-one by one-after hovering for a very long time-some longer than others. If I could just understand why its being weird...
novacara
What I would do is create a tooltip for each control, and get rid of them when the mouse moves. I've had to do this in the past on a winforms project and it's a little tricky, but it works and it keeps the UI snappy. You'll need to create all new tooltips in the event, though, to get it to work.
Joseph
+1  A: 

Ok so...after playing around the solution to this problem for anyone in the future who finds this post useful is posted below. Why this is necessary is beyond me.

Change

toolTip1.Show(toolTip1.GetToolTip(ctrl), ctrl, ctrl.Width / 2, ctrl.Height / 2);

To

toolTip1.Show(toolTip1.GetToolTip(ctrl), ctrl, ctrl.Width / 2, ctrl.Height / 2);
toolTip1.Hide(ctrl);
toolTip1.Show(toolTip1.GetToolTip(ctrl), ctrl, ctrl.Width / 2, ctrl.Height / 2);
novacara