0

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

3 Answers 3

2

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;
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

No need to call Dispose after close. Dispose will be called automatically since the form is not shown modally. If it was shown modally, Dispose is required.
1

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.

3 Comments

(frmSecQStatToolTipDlg == null):--> frmSecQStatToolTipDlg is a type which is not valid in the given context
That's a slight typo. I believe Shaharyar means tooltip in place of frmSecQStatToolTipDlg.
Thanks Shaharyar and Adrian
0

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();
}  

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.