Iterating through tooltips
Guys, I have a windows form with a panel control and inside the panel control are several other controls with a System.Windows.Forms.Tooltip attached to them. How can I iterate through each tooltip and set the Active property of the tooltip to false? Tooltips, unlike other controls, are not actually controls. So I had this:
开发者_C百科foreach (System.Windows.Forms.Control ctrl in this.pnlControl.Controls)
{
if (ctrl.Name.StartsWith("tt")) // since all my tooltip names start with 'tt'
{
System.Windows.Forms.ToolTip TipControl=(System.Windows.Forms.ToolTip)ctrl;
TipControl.Active=false;
}
}
This does not work though. It gets an error because the ToolTip control is not inherited from System.Windows.Forms.Control. Any ideas?
EDIT: Okay Guys. I probably didn't go into enough detail to get the answer I needed. My problem is, I'm taking all the controls in my panel and moving them to a different panel. Once they are switched over, the tooltips are still attached to the controls, which is what I want. However I have no way to deactive or reactivate them once I move them since the form and the original panel no longer exist. However, I found a solution which I will post here.
How to add tool tips for two buttons? The correct way is NOT creating two instances of ToolTip in this way:
ToolTip tt1 = new ToolTip(); //or you can create one in the designer
tt1.ToolTipTitle = "test";
tt1.SetToolTip(button1, "caption1");
ToolTip tt2 = new ToolTip();
tt2.ToolTipTitle = "test2";
tt2.SetToolTip(button2, "caption2");
Remember that a ToolTip instance and a control are not one-on-one related. The right way for this example is:
ToolTip tt1 = new ToolTip(); //or you can create one in the designer
tt1.ToolTipTitle = "test";
tt1.SetToolTip(button1, "caption1");
tt1.SetToolTip(button2, "caption2");
To remove the tooltip of button2, use:
tt1.SetToolTip(button2,string.Empty);
For your case,we can use
foreach(Control c in this.Controls)
{
tt.SetToolTip(c,string.Empty);
}
Typically, you have a single ToolTip instance that handles the displaying of tool tips for all of your controls. That single ToolTip instance is just a regular member of your form. Simply set it's Active property to false.
Edit: OK, scrap my previous answer. Yes, ToolTip is a Component, not a Control, so it's not actually in the Panel at all. From your question, it sounds like you have one ToolTip instance and you use it for controls inside this Panel as well as for other controls, right? In that case the solution is simple: create a separate ToolTip instance and use that one for controls in the Panel, then just refer to it directly to deactivate it, eg.
ttPanel.Active = false;
Okay what I did was create a new class that is inherited from Control, like so:
public class TooltipMaster : System.Windows.Forms.Control
{
private System.Windows.Forms.ToolTip m_tooltip1;
private System.Windows.Forms.ToolTip m_tooltip2;
private System.Windows.Forms.ToolTip m_tooltip3;
private System.Windows.Forms.ToolTip m_tooltip4;
public System.Windows.Forms.ToolTip ToolTip1 {
get { return m_tooltip1; }
set { m_tooltip1 = value; }
}
public System.Windows.Forms.ToolTip ToolTip2 {
get { return m_tooltip2; }
set { m_tooltip2 = value; }
}
public System.Windows.Forms.ToolTip ToolTip3 {
get { return m_tooltip3; }
set { m_tooltip3 = value; }
}
public System.Windows.Forms.ToolTip ToolTip4 {
get { return m_tooltip4; }
set { m_tooltip4 = value; }
}
}
Then what I did was create an instance of this class inside my main form's Load event. Then I just assigned each of my 4 tooltips to the 4 tooltips in this class. Finally, I added this control to my panel. After doing all that, I could access the tooltips later by iterating through each control and looking for the TooltipMaster control. Hope this makes sense!
精彩评论