Search code examples
c#.netwinformsbuttontooltip

Show tooltip of a disabled button which is inside a group box in Windows form?


I have a group box set visible true.Have 3 button inside it .btn1 visible true.btn 2 ,btn 3 visible false..want to show tooltip for this 3 button in exact position?

What i am doing now

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace WindowsFormsApplication3
{
    public partial class Form1 : Form
    {

        private ToolTip toolTip = new ToolTip();
        private bool isShown = false;
        ToolTip toolTip1 = new ToolTip();
        bool IsShown = false;

        public Form1()
        {
            InitializeComponent();

            button1.Enabled = false;
            toolTip1.InitialDelay = 0;
        }
        private void Form1_MouseMove_1(object sender, MouseEventArgs e)
        {
            //if (button1 == this.GetChildAtPoint(e.Location))
            //{
            //    if (!isShown)
            //    {
            //        toolTip.Show("MyToolTip", this, e.Location);
            //        isShown = true;
            //    }
            //}
            //else
            //{
            //    toolTip.Hide(textBox1);
            //    isShown = false;
            //}
            Control ctrl = this.GetChildAtPoint(e.Location);

            if (ctrl != null)
            {
                if (ctrl == this.button1 && !IsShown)
                {
                    string tipstring = this.toolTip1.GetToolTip(this.button1);
                    this.toolTip1.Show(tipstring, this.button1, this.button1.Width / 2, this.button1.Height / 2);
                    IsShown = true;
                }
            }
            else
            {
                this.toolTip1.Hide(this.button1);
                IsShown = false;
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
              // Set up the delays for the ToolTip.
            toolTip1.AutoPopDelay = 5000;
            toolTip1.InitialDelay = 1000;
            toolTip1.ReshowDelay = 500;

            // Force the ToolTip text to be displayed whether or not the form is active.
            toolTip1.ShowAlways = true;

            // Set up the ToolTip text for the Button and Checkbox.
            toolTip1.SetToolTip(this.button1, "My button1");
        }
    }
}

I have tried with 1 button it failed ...need help on the same

I even tried

http://msdn.microsoft.com/en-us/library/system.windows.forms.tooltip.aspx

But it didn't work for disabled controls


Solution

  • Because MouseHover event on disabled button won't fire, you could set MouseMove event on your form and check for buttons position, like this way:

    private void Form1_MouseMove(object sender, MouseEventArgs e)
    {
        if (e.X >= button.Location.X && e.X < button.Location.X + button.Width
            && e.Y >= button.Location.Y && e.Y <= button.Location.Y + button.Height)
        {
            if (!isShown)
            {
                tt.Show("MyToolTip", button, button.Width / 2, button.Height / 2);
                isShown = true;
            }
    
        }
        else
        {
            tt.Hide(button);
            isShown = false;
        }
    }
    

    EDIT (to work with GroupBox):

    In your *.Designer.cs class you should add this delegate (with the same callback mentioned above):

    this.groupBox1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseMove);
    

    Try this I think it will work.