Click here to Skip to main content
15,868,292 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi everyone!
I'm working on a window form app and this is a set of my codes.
C#
private void ToolTips(Control A)
{
    ToolTip toolTip = new ToolTip();
    toolTip.Show(A.Tag.ToString(), A);
}

private void pictureBox_MouseHover(object sender, EventArgs e)
{
    ToolTips(sender.???????);
}

I intend to share only one MouseHover Event with every control I have on my form.
Please help me out.
Thanks.
Posted
Updated 24-Dec-14 4:59am
v2

On your mouse hover event, call one of the System.Windows.Forms.ToolTip.Show methods (as you already done):
http://msdn.microsoft.com/en-us/library/system.windows.forms.tooltip.show%28v=vs.110%29.aspx[^].

But your problem is different. Apparently, you are well confused with programming basics, don't know well about methods, variable, members, instances and other elementary stuff. Your toolTip object is referenced by a local variable; it goes out of context after you call the ToolTips class; and the object is created again and again and eventually destroyed by GC. You don't need this method. Instead, you can create an instance private member of your class of the type ToolTip, just one; and call its Show method each time you want to show some text.

—SA
 
Share this answer
 
Comments
BillWoodruff 24-Dec-14 13:49pm    
+5
Sergey Alexandrovich Kryukov 24-Dec-14 17:07pm    
Thank you, Bill.
—SA
In addition to the excellent advice Sergey gave you, here, I'd like to add that once you do get your ToolTip created (once), and it's reusable: then you can write your MouseHover EventHandler like this:
C#
private void pictureBox_MouseHover(object sender, EventArgs e)
{
    Control theControl = sender as Control;

    if(theControl != null)
    {
        ToolTips(theControl);
    }
}
While you can correctly assume the test for 'null is not really needed, implementing it won't hurt anything and may just be helpful in debugging if there is a problem.

I assume that you are assigning every Control that you link to this MouseHover EventHandler a Tag property ... correct ?
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 24-Dec-14 17:12pm    
Not clear. How could ToolTips method reuse anything if it creates a new ToolTip object on every call? If you suggested some other implementation of ToolTips, it could work. In particular, the check for non-null control reference would be better to do inside this method...
—SA
BillWoodruff 24-Dec-14 21:25pm    
Hi Sergey, I thought I was being clear that I was referring to creating the instance of the ToolTip once, and then re-using that instance; in fact, I thought I was asserting the correctness of your suggestion :)

And, the check for non-null here is done in a method; it checks whether or not the argument to the method can be cast to a Control.

Take a break, Sergey, relax, enjoy Christmas :)
Sergey Alexandrovich Kryukov 24-Dec-14 21:56pm    
Yes, I would rather assume that, but this is not obvious from your solution post along — please look at it. I would advise to explicitly suggest a method different from ToolTips...
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900