Tip problem

I am trying to programmatically set a tooltip for a label (added at runtime) in a UserControl on a form. The button used to run the code is located in the user control itself. The problem is that when I click the button, a tooltip is not assigned. However, if I use basically the same code in the parent form and put it behind the button in the parent form, I can assign a tooltip to the shortcut in the parent form. Also, if I add a shortcut to the user control before launching, it also works.

The following code is from a button on a user control that is in the main form.

   private void button1_Click(object sender, EventArgs e)
        {
            Label lblTest = new Label();
            lblTest.Text = "Test";
            ToolTip tt = new ToolTip();
            tt.SetToolTip(lblTest, "ToolTipTest");
            this.Controls.Add(lblTest);
            lblTest.Location = new Point(10, 10);
        }

Any help would be greatly appreciated.

+3
source
2

. :

ToolTip tt = null;

private void button1_Click(object sender, EventArgs e)
{
    Label lblTest = new Label();
    lblTest.Text = "Test";
    tt = new ToolTip();
    this.Controls.Add(lblTest);
    lblTest.MouseHover += new EventHandler(label_Hover);
    lblTest.Location = new Point(10, 10);
}

private void label_Hover(object sender, EventArgs e)
{
    tt.Show((Label)sender, "Tooltip");
}

, , / .

+2

, ToolTip Click. Click:

ToolTip tt = new ToolTip();
private void button1_Click(object sender, EventArgs e) 
{  
   // and so on...
0

All Articles