gomesh munda
gomesh munda

Reputation: 850

custom tooltip in c# .net win forms

I am looking to simulate a custom tooltip the like of you see in websites using c# .NET 4.5 windows forms.This tooltip will basically show status of some Tasks like how many tasks are pending,tasks in process, completed etc.To do this i am using a borderless win form.This winform will have some texts, images etc.I want it to reveal itself on button's mouseHover event and disappear on MouseLeave event.My problem is that on Mousehover event numerous instances of that tooltip form is getting generated and on MouseLeave they are not getting closed.My code is

        private void B_MouseHover(object sender, EventArgs e)
    {
        frmSecQStatToolTipDlg tooltip = new frmSecQStatToolTipDlg();
        tooltip.Location = this.PointToScreen(new Point(this.Left, this.Bottom));
        tooltip.Show();
    }

private void B_MouseLeave(object sender, EventArgs e)
    {
        frmSecQStatToolTipDlg tooltip = new frmSecQStatToolTipDlg();
        tooltip.Close();
    }

My code is not working, hence please tell me how to do this the correct way.Thanks

Upvotes: 0

Views: 1522

Answers (3)

Adrian
Adrian

Reputation: 2875

You're generating a new instance of the form class every time you get a hover event, and every time you get a leave event. If you want to continue to use this approach I would recommend you use a variable on your main form object to store the reference to your tooltip form. Secondly, you need to not generate a new instance whenever the event handler is called, but only when necessary. I would create your instance the first time your Hover event is called for a particular control, and then dispose of it when your Leave handler is called -- this is under the assumption that the tooltip dialog's constructor loads up different information for each control being hovered over. Like so:

frmSecQStatToolTipDlg f_tooltip;

private void B_MouseHover(object sender, EventArgs e)
{
    if(frmSecQStatToolTipDlg == null)
    {
        f_tooltip = new frmSecQStatToolTipDlg();
    }

    tooltip.Location = this.PointToScreen(new Point(this.Left, this.Bottom));
    tooltip.Show();
}

private void B_MouseLeave(object sender, EventArgs e)
{
    if(f_tooltip != null)
    {
        f_tooltip.Close();
        f_tooltip = null;
    }
}

Upvotes: 2

ehh
ehh

Reputation: 3480

Declare your tooltip once as readonly and use it without asking anytime if it is null or not. If you need to Dispose it, implement the IDisposable pattern: https://msdn.microsoft.com/en-us/library/b1yfkh5e(v=vs.110).aspx

private readonly frmSecQStatToolTipDlg _tooltip = new frmSecQStatToolTipDlg() ;

private void B_MouseHover(object sender, EventArgs e)
{    
    _tooltip.Location = this.PointToScreen(new Point(this.Left, this.Bottom));
    _tooltip.Show();
}

private void B_MouseLeave(object sender, EventArgs e)
{
    _tooltip.Hide();
}  

Upvotes: 0

Shaharyar
Shaharyar

Reputation: 12439

You should keep a global field for this form, and should not dispose or close it. Just hide it on some events and show again.

Sample Code:

frmSecQStatToolTipDlg tooltip;

private void B_MouseHover(object sender, EventArgs e)
{
    if(frmSecQStatToolTipDlg == null)
    {
        tooltip = new frmSecQStatToolTipDlg();
    }

    tooltip.Location = this.PointToScreen(new Point(this.Left, this.Bottom));
    tooltip.Show();
}

private void B_MouseLeave(object sender, EventArgs e)
{
    if(frmSecQStatToolTipDlg != null)
    {
        tooltip.Hide();
    }
}

With this logic you'll not have to create tooltip instance again and again and it will not take time to popup if you frequently do this activity.

Upvotes: 1

Related Questions