Normalperson
Normalperson

Reputation: 3

C# Forms Mouse Hover Not firing in controls?

I am trying to write a code on Mouse_Hover for Button

private void Button_MouseHover(object sender, EventArgs e){
    Button b = (Button)sender;
    toolTip1.Show("Click ME!!", b);
}

But toolTip1 is not showing!!

after this I try to use MouseHover In another controls and it did not work and I try To use MessageBox , but it did not work even , and I double check Events for Button (and other controls) in properties tab.

Upvotes: 0

Views: 1968

Answers (2)

Amin Golmahalleh
Amin Golmahalleh

Reputation: 4216

This is a way to solve your problem:

public partial class Form1 : Form
{
    private System.Windows.Forms.ToolTip toolTip1;

    public Form1()
    {
        InitializeComponent();
        this.components = new System.ComponentModel.Container();
        this.toolTip1 = new System.Windows.Forms.ToolTip(this.components);

        Button myBtn = new Button();
        this.Controls.Add(myBtn);

        myBtn.Location = new Point(10, 10);
        myBtn.MouseEnter += new EventHandler(myBtn_MouseEnter);
        myBtn.MouseLeave += new EventHandler(myBtn_MouseLeave);
    }


    void myBtn_MouseEnter(object sender, EventArgs e)
    {
        Button btn = (sender as Button);
        if (btn != null)
        {
            this.toolTip1.Show("Hello!!!", btn);
        }
    }

    void myBtn_MouseLeave(object sender, EventArgs e)
    {
        Button btn = (sender as Button);
        if (btn != null)
        {
            this.toolTip1.Hide(btn);
        }
    }

Upvotes: 1

Caius Jard
Caius Jard

Reputation: 74730

You don't have to wire up controls to use the mouse hover event in order to use a tooltip. MSDN has some useful guidance, the essence of which is "add a tooltip to your form, assign the tooltip to the button"

Upvotes: 1

Related Questions