... edit October 20, 2013 ...
Summary:
1. In WinForms, in a UserControl: the ToolTip Component, which is an Extender Provider ... unlike other Components, and Controls ... will not function if you change the Parent of the UserControl to another Form (it will, however, function if you change the Parent of the UserControl to another ContainerControl on the same Form). By "not function," I mean: the ToolTips set at design-time on the UserControl's internal Controls will no longer appear at run-time.
2. The reasons why the ToolTip Component appears to become disabled if its hosting UserControl's Parent is changed to another Form are not clear. Quite possibly, the dependency the ToolTip Component has on its internal pointers to Site and Container are affected.
How to "migrate" a ToolTip in a UserControl if the UserControl's Parent is changed to another Form:
0. for educational purposes:
a. assume we have a WinForm App, with a main Form, 'MainForm, with two Panels on it, 'panel1, 'panel2
b. the App has a UserControl, 'userControl1, which has one Button, and two TextBoxes on it, and has a ToolTip component added to it: button1, textBox1, textBox2, toolTip1.
c. one instance of the UserControl1, 'userControl1, has been drag-dropped into the main Form's 'panel1.
d. the App creates another Form, 'Form1
1. define a public method in the UserControl like this:
public void ChangeParent(Control newParent)
{
toolTip1.RemoveAll();
this.Parent.Controls.Remove(this);
newParent.Controls.Add(this);
switch (newParent.Name)
{
case "panel1":
toolTip1.SetToolTip(button1, "button1 in panel1");
toolTip1.SetToolTip(textBox1, "textBox1 in panel1");
toolTip1.SetToolTip(textBox2, "textBox2 in panel1");
break;
case "panel2":
toolTip1.SetToolTip(button1, "button1 in panel2");
toolTip1.SetToolTip(textBox1, "textBox1 in panel2");
toolTip1.SetToolTip(textBox2, "textBox2 in panel2");
break;
case "Form1":
toolTip1.SetToolTip(button1, "button1 in Form1");
toolTip1.SetToolTip(textBox1, "textBox1 in Form1");
toolTip1.SetToolTip(textBox2, "textBox2 in Form1");
break;
}
}
Since the ToolTips for the Controls in the UserControl are not created at design-time: call the 'ChangeParent method in the UserControl's 'Load EventHandler to initialize them:
private void UserControl1_Load(object sender, EventArgs e)
{
ChangeParent(Parent);
}
Discussion:
1. since 'ChangeParent is a Public method, it can be called from anywhere that has a reference to an instance of the UserControl 'userControl1. So, if you moved the UserControl to another Form, and wanted to then move it back to its original Parent Form, the second Form would have to have a pointer to the original Form, or the ContainerControl on the original Form where they wished to re-site the UserControl (in this example, that would be 'panel1 in the instance of the MainForm).
2. You can see that what's done here is very much a "hack:" the ToolTips are getting cleared and restored every time the Parent of the UserControl is changed.
Conclusion: Use at your own risk, this kind of hack could well be affected in the future by some change by MS. And, as I stress in the original response to this thread (below), it's my opinion that "migrating" Controls from one ControlContainer, or Form, to another is not good programming practice ... in the long run.
... end edit October 2013 ...
While you have discovered you
can remove a Control from a Form, and then add it to the ControlCollection of another Form, make it visible, and use it, there is a
structural problem possible with using
certain Components when you "migrate" Controls this way, and a ToolTip, even though it doesn't
appear in the ToolBox for Visual Studio
as a Component,
is a Component.
But, before talking about the special nature of a ToolTip, I'd like to say that
I think moving Controls from one container, Form, UserControl, to another, or from one TabPage to another TabPage, etc., is a very bad idea, and, inherently, is going to lead to problems later on.
If you want to add a new UserControl to Form3, at run-time, with its ToolTip on a Button, exactly the way you configured the UserControl at design-time: just create a new instance of the UserControl and add it to the Form.
Form3 f3 = new Form3();
UserControl1 newUC1 = new UserControl1();
f3.Controls.Add(newUC1);
newUC1.Location = new Point(50,50);
f3.Show();
If you really need to modify, at run-time, various properties or behaviors of the UserControl, then create Public Properties in the UserControl that expose the objects, or fields you need to manipulate:
public UserControl1()
{
InitializeComponent();
UCToolTip = toolTip1;
UCButton = button1;
}
public ToolTip UC1ToolTip { get; set; }
public Button UC1Button { get; set; }
Then you can do something in your code like:
newUC.UCToolTip.SetToolTip(newUC.UCButton, "Papa's got a brand new bag.");
And now, here's a lot of esoteric detail about Components, and the ToolTip:
Components have a dependency on the special private variable System.ComponentModel you see in every Designer.cs file: private System.ComponentModel.IContainer components = null; When a UserControl, or Form, has Components placed on it, only then is that variable initialized: this.components = new System.ComponentModel.Container();
Some Components in a UserControl, like the Timer, will "survive" migration from one host Control to another with no problem.
But, the ToolTip is a special kind of Component that, potentially, <i>extends</i> capability to every Control on a Form, and the Form, itself. "Under the hood," a ToolTip has its own collection, a ComponentCollection, maintained in its Container.Components field. And, a ToolTip is "sited" in its Container in rather a special way, the "site" itself being an instance of the ISite interface. When a ToolTip is intialized in a Designer.cs Class, you'll note it's done with the form of its constructor that passes its host Control into an IContainer variable.
When you remove a UserControl with a ToolTip from some Form, or UserControl's, ControlCollection, it appears the ToolTip "site" information is "lost." While you might think you could restore that information, and I've tried to do that as an experiment: well, all I can say is that I can't achieve that through a variety of exotic means I've tried. The 'Container property of a ToolTip is read-only, but you can set the 'Site property.