【问题标题】:Show tooltip of a disabled button which is inside a group box in Windows form?显示 Windows 表单中组框内的禁用按钮的工具提示?
【发布时间】:2013-10-08 13:08:53
【问题描述】:

我有一个组框设置可见 true。里面有 3 个按钮 .btn1 可见 true.btn 2 ,btn 3 可见 false..想要在确切位置显示这 3 个按钮的工具提示吗?

我现在在做什么

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

我尝试了 1 个按钮,但失败了……需要帮助

我什至尝试过

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

但它不适用于禁用的控件

【问题讨论】:

  • 您希望何时显示工具提示?我会考虑将事件 MouseHover 设置为 tooltip.Show() 并将 MouseLeave 设置为 tooltip.Hide()。但我不确定我是否完全理解您想要实现的目标。
  • 我正在尝试显示放置在组框内的禁用控件的工具提示..
  • 能否举个例子

标签: c# .net winforms button tooltip


【解决方案1】:

由于禁用按钮上的 MouseHover 事件不会触发,您可以在 form 上设置 MouseMove 事件并检查按钮位置,如下所示:

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

编辑(与 GroupBox 一起使用):

在您的 *.Designer.cs 类中,您应该添加这个委托(使用上面提到的相同回调):

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

试试这个,我认为它会工作。

【讨论】:

  • 参见上面的编辑 - 我添加了代码。这个对我有用。如果您仍然缺少某些信息,请告诉我。
  • ...可爱的家伙...它的作品完美...我忘记附加事件处理程序...它是否适用于组框内的多个按钮..
  • 只是一个小问题,,,说我的 grpbox 中有 2 个按钮..btn1 已启用..btn2 已禁用..我想为跟随你的两个人显示工具提示...现在当我我第一次将鼠标悬停在启用的 btn1 上 ..tooltip 没有显示 ...但是它显示的有 arter 吗?你能检查一次吗..
  • 我猜当工具提示显示并且鼠标光标悬停在它上面时会发生这种情况 - 导致它消失并再次调用 MouseMove。我现在没有很好的解决方案,但您可以将工具提示放在按钮边框之外。
猜你喜欢
  • 1970-01-01
  • 2018-05-10
  • 1970-01-01
  • 1970-01-01
  • 2020-07-11
  • 1970-01-01
  • 1970-01-01
  • 2011-12-14
  • 2023-01-22
相关资源
最近更新 更多