2009-11-13 105 views
17

我試圖在鼠標懸停在禁用的控件上時顯示工具提示。由於禁用的控件不處理任何事件,我必須在父窗體中執行此操作。我選擇通過處理父表單中的MouseMove事件來完成此操作。下面是執行該任務的代碼:在禁用的控件上顯示工具提示

void Form1_MouseMove(object sender, MouseEventArgs e) 
    { 
     m_toolTips.SetToolTip(this, "testing tooltip on " + DateTime.Now.ToString()); 
     string tipText = this.m_toolTips.GetToolTip(this); 
     if ((tipText != null) && (tipText.Length > 0)) 
     { 
      Point clientLoc = this.PointToClient(Cursor.Position); 
      Control child = this.GetChildAtPoint(clientLoc); 
      if (child != null && child.Enabled == false) 
      { 
       m_toolTips.ToolTipTitle = "MouseHover On Disabled Control"; 
       m_toolTips.Show(tipText, this, 10000); 
      } 
      else 
      { 
       m_toolTips.ToolTipTitle = "MouseHover Triggerd"; 
       m_toolTips.Show(tipText, this, 3000); 
      } 
     } 
    } 

該代碼的確處理禁用控件的工具提示顯示。問題是,當鼠標懸停在禁用的控件上時,工具提示會一直關閉並重新顯示。從我在工具提示中添加的顯示時間中,當鼠標位於父窗體之上時,大約每3秒調用一次MouseMove事件,所以工具提示每3秒更新一次。但是,當鼠標停用禁用的控件時,工具提示每隔1秒刷新一次。此外,當工具提示刷新上述表單時,只有短文更新。但是,當工具提示在禁用控件上方刷新時,工具提示窗口就會關閉,就好像鼠標移動到啓用的控件中一樣,工具提示應該關閉。但隨後工具提示馬上重新出現。

有人能告訴我這是爲什麼嗎?謝謝。

回答

6

答案竟然是簡單一點,但需要隨時應用。

void OrderSummaryDetails_MouseMove(object sender, MouseEventArgs e) 
{ 
     Control control = GetChildAtPoint(e.Location); 
     if (control != null) 
     { 
      string toolTipString = mFormTips.GetToolTip(control); 
      this.mFormTips.ShowAlways = true; 
      // trigger the tooltip with no delay and some basic positioning just to give you an idea 
      mFormTips.Show(toolTipString, control, control.Width/2, control.Height/2); 
     } 
} 
13

當鼠標碰到disbled控件時,只能在鼠標離開鼠標時隱藏它,只能顯示一次工具提示。 PLS,看看下面的代碼,它應呈現形式

private ToolTip  _toolTip = new ToolTip(); 
private Control  _currentToolTipControl = null; 

public Form1() 
{ 
    InitializeComponent(); 

    _toolTip.SetToolTip(this.button1, "My button1"); 
    _toolTip.SetToolTip(this.button2, "My button2"); 
    _toolTip.SetToolTip(this.textBox1, "My text box"); 
} 

private void Form1_MouseMove(object sender, MouseEventArgs e) 
{ 
    Control control = GetChildAtPoint(e.Location); 
    if (control != null) 
    { 
     if (!control.Enabled && _currentToolTipControl == null) 
     { 
      string toolTipString = _toolTip.GetToolTip(control); 
      // trigger the tooltip with no delay and some basic positioning just to give you an idea 
      _toolTip.Show(toolTipString, control, control.Width/2, control.Height/2); 
      _currentToolTipControl = control; 
     } 
    } 
    else 
    { 
     if (_currentToolTipControl != null) _toolTip.Hide(_currentToolTipControl); 
     _currentToolTipControl = null; 
    } 
} 

希望這有助於爲所有殘疾人控件工具提示消息,對於

+0

這似乎幾乎工作。一些控件的行爲正常,而其他控件仍在閃爍。 – 2009-11-17 16:28:01

+2

比DJ更適合我的回答。我添加了一個control.Visible檢查,所以我沒有彈出隱形控件的工具提示。我也將hide塊移到show block之前,並檢查是否(_currentToolTipControl!= null && _currentToolTipControl!= control){... Hide(); _currentToolTipControl = null; }(沒有這個,快速移動到另一個禁用的控件無法更新工具提示)。 – mheyman 2013-03-21 20:18:04

0

這是我如何解決這個問題

我有一個PIC32MX自動生成代碼的應用程序。

該應用程序有3個選項卡頁text = PWM,ADC和UART。

在每個標籤頁我有一個複選框文本=值RPa0

的意圖是,當一個外設使用值RPa0,其他外圍防止 從使用銷,通過禁用它在另一頁,以及工具提示文本必須彈出 對禁用複選框說(例如「由PWM使用」) 什麼外設正在使用該引腳。

問題是工具提示文本不會在禁用的複選框上彈出。

爲了解決這個問題,我剛剛刪除了複選框的文本,並在複選框應該有的文本中插入標籤。

當選中複選框時,其他複選框將被禁用,並且旁邊的標籤將採用工具提示文本。

由於標籤已啓用,即使在禁用的複選框上也會彈出工具提示文本。

雙重工作,一半的複雜性。

這裏是代碼和設計師爲C#2010

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; 

namespace WindowsFormsApplication1 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void Form1_Load(object sender, EventArgs e) 
     { 

     } 

     private void cb_ADC_RPA0_CheckedChanged(object sender, EventArgs e) 
     { 
      /* Disable pin on other peripherals */ 
      cb_UART_RPA0.Enabled = !((CheckBox)sender).Checked; 
      cb_PWM_RPA0.Enabled = !((CheckBox)sender).Checked; 

      SetTootTip((CheckBox)sender, lbl_PWM_RPA0, lbl_UART_RPA0, "ADC"); 

     } 



     private void cb_PWM_RPA0_CheckedChanged(object sender, EventArgs e) 
     { 
      /* Disable pin on other peripherals */ 
      cb_UART_RPA0.Enabled = !((CheckBox)sender).Checked; 
      cb_ADC_RPA0.Enabled = !((CheckBox)sender).Checked; 

      SetTootTip((CheckBox)sender, lbl_ADC_RPA0, lbl_UART_RPA0, "PWM"); 
     } 

     private void cb_UART_RPA0_CheckedChanged(object sender, EventArgs e) 
     { 
      /* Disable pin on other peripherals */ 
      cb_ADC_RPA0.Enabled = !((CheckBox)sender).Checked; 
      cb_PWM_RPA0.Enabled = !((CheckBox)sender).Checked; 
      SetTootTip((CheckBox)sender, lbl_ADC_RPA0, lbl_PWM_RPA0, "UART"); 

     } 

     void SetTootTip(CheckBox sender, Label lbl1, Label lbl2, string text) 
     { 
      /* Update tooltip on the other labels */ 
      if (sender.Checked) 
      { 
       toolTip1.SetToolTip(lbl1, "Used by " + text); 
       toolTip1.SetToolTip(lbl2, "Used by " + text); 
      } 
      else 
      { 
       toolTip1.SetToolTip(lbl1, ""); 
       toolTip1.SetToolTip(lbl2, ""); 
      } 
     } 
    } 
} 


namespace WindowsFormsApplication1 
{ 
    partial class Form1 
    { 
     /// <summary> 
     /// Required designer variable. 
     /// </summary> 
     private System.ComponentModel.IContainer components = null; 

     /// <summary> 
     /// Clean up any resources being used. 
     /// </summary> 
     /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> 
     protected override void Dispose(bool disposing) 
     { 
      if (disposing && (components != null)) 
      { 
       components.Dispose(); 
      } 
      base.Dispose(disposing); 
     } 

     #region Windows Form Designer generated code 

     /// <summary> 
     /// Required method for Designer support - do not modify 
     /// the contents of this method with the code editor. 
     /// </summary> 
     private void InitializeComponent() 
     { 
      this.components = new System.ComponentModel.Container(); 
      this.tabControl1 = new System.Windows.Forms.TabControl(); 
      this.tpPWM = new System.Windows.Forms.TabPage(); 
      this.tpUART = new System.Windows.Forms.TabPage(); 
      this.tpADC = new System.Windows.Forms.TabPage(); 
      this.cb_PWM_RPA0 = new System.Windows.Forms.CheckBox(); 
      this.cb_ADC_RPA0 = new System.Windows.Forms.CheckBox(); 
      this.lbl_PWM_RPA0 = new System.Windows.Forms.Label(); 
      this.lbl_ADC_RPA0 = new System.Windows.Forms.Label(); 
      this.toolTip1 = new System.Windows.Forms.ToolTip(this.components); 
      this.lbl_UART_RPA0 = new System.Windows.Forms.Label(); 
      this.cb_UART_RPA0 = new System.Windows.Forms.CheckBox(); 
      this.tabControl1.SuspendLayout(); 
      this.tpPWM.SuspendLayout(); 
      this.tpUART.SuspendLayout(); 
      this.tpADC.SuspendLayout(); 
      this.SuspendLayout(); 
      // 
      // tabControl1 
      // 
      this.tabControl1.Controls.Add(this.tpPWM); 
      this.tabControl1.Controls.Add(this.tpUART); 
      this.tabControl1.Controls.Add(this.tpADC); 
      this.tabControl1.Location = new System.Drawing.Point(12, 12); 
      this.tabControl1.Name = "tabControl1"; 
      this.tabControl1.SelectedIndex = 0; 
      this.tabControl1.Size = new System.Drawing.Size(629, 296); 
      this.tabControl1.TabIndex = 0; 
      // 
      // tpPWM 
      // 
      this.tpPWM.Controls.Add(this.lbl_PWM_RPA0); 
      this.tpPWM.Controls.Add(this.cb_PWM_RPA0); 
      this.tpPWM.Location = new System.Drawing.Point(4, 22); 
      this.tpPWM.Name = "tpPWM"; 
      this.tpPWM.Padding = new System.Windows.Forms.Padding(3); 
      this.tpPWM.Size = new System.Drawing.Size(621, 270); 
      this.tpPWM.TabIndex = 0; 
      this.tpPWM.Text = "PWM"; 
      this.tpPWM.UseVisualStyleBackColor = true; 
      // 
      // tpUART 
      // 
      this.tpUART.Controls.Add(this.cb_UART_RPA0); 
      this.tpUART.Controls.Add(this.lbl_UART_RPA0); 
      this.tpUART.Location = new System.Drawing.Point(4, 22); 
      this.tpUART.Name = "tpUART"; 
      this.tpUART.Padding = new System.Windows.Forms.Padding(3); 
      this.tpUART.Size = new System.Drawing.Size(621, 270); 
      this.tpUART.TabIndex = 1; 
      this.tpUART.Text = "UART"; 
      this.tpUART.UseVisualStyleBackColor = true; 
      // 
      // tpADC 
      // 
      this.tpADC.Controls.Add(this.lbl_ADC_RPA0); 
      this.tpADC.Controls.Add(this.cb_ADC_RPA0); 
      this.tpADC.Location = new System.Drawing.Point(4, 22); 
      this.tpADC.Name = "tpADC"; 
      this.tpADC.Padding = new System.Windows.Forms.Padding(3); 
      this.tpADC.Size = new System.Drawing.Size(621, 270); 
      this.tpADC.TabIndex = 2; 
      this.tpADC.Text = "ADC"; 
      this.tpADC.UseVisualStyleBackColor = true; 
      // 
      // cb_PWM_RPA0 
      // 
      this.cb_PWM_RPA0.AutoSize = true; 
      this.cb_PWM_RPA0.Location = new System.Drawing.Point(17, 65); 
      this.cb_PWM_RPA0.Name = "cb_PWM_RPA0"; 
      this.cb_PWM_RPA0.Size = new System.Drawing.Size(15, 14); 
      this.cb_PWM_RPA0.TabIndex = 0; 
      this.cb_PWM_RPA0.UseVisualStyleBackColor = true; 
      this.cb_PWM_RPA0.CheckedChanged += new System.EventHandler(this.cb_PWM_RPA0_CheckedChanged); 
      // 
      // cb_ADC_RPA0 
      // 
      this.cb_ADC_RPA0.AutoSize = true; 
      this.cb_ADC_RPA0.Location = new System.Drawing.Point(17, 65); 
      this.cb_ADC_RPA0.Name = "cb_ADC_RPA0"; 
      this.cb_ADC_RPA0.Size = new System.Drawing.Size(15, 14); 
      this.cb_ADC_RPA0.TabIndex = 1; 
      this.cb_ADC_RPA0.UseVisualStyleBackColor = true; 
      this.cb_ADC_RPA0.CheckedChanged += new System.EventHandler(this.cb_ADC_RPA0_CheckedChanged); 
      // 
      // lbl_PWM_RPA0 
      // 
      this.lbl_PWM_RPA0.AutoSize = true; 
      this.lbl_PWM_RPA0.Location = new System.Drawing.Point(38, 65); 
      this.lbl_PWM_RPA0.Name = "lbl_PWM_RPA0"; 
      this.lbl_PWM_RPA0.Size = new System.Drawing.Size(35, 13); 
      this.lbl_PWM_RPA0.TabIndex = 1; 
      this.lbl_PWM_RPA0.Text = "RPA0"; 
      // 
      // lbl_ADC_RPA0 
      // 
      this.lbl_ADC_RPA0.AutoSize = true; 
      this.lbl_ADC_RPA0.Location = new System.Drawing.Point(38, 66); 
      this.lbl_ADC_RPA0.Name = "lbl_ADC_RPA0"; 
      this.lbl_ADC_RPA0.Size = new System.Drawing.Size(35, 13); 
      this.lbl_ADC_RPA0.TabIndex = 2; 
      this.lbl_ADC_RPA0.Text = "RPA0"; 
      // 
      // lbl_UART_RPA0 
      // 
      this.lbl_UART_RPA0.AutoSize = true; 
      this.lbl_UART_RPA0.Location = new System.Drawing.Point(37, 65); 
      this.lbl_UART_RPA0.Name = "lbl_UART_RPA0"; 
      this.lbl_UART_RPA0.Size = new System.Drawing.Size(35, 13); 
      this.lbl_UART_RPA0.TabIndex = 4; 
      this.lbl_UART_RPA0.Text = "RPA0"; 
      // 
      // cb_UART_RPA0 
      // 
      this.cb_UART_RPA0.AutoSize = true; 
      this.cb_UART_RPA0.Location = new System.Drawing.Point(16, 65); 
      this.cb_UART_RPA0.Name = "cb_UART_RPA0"; 
      this.cb_UART_RPA0.Size = new System.Drawing.Size(15, 14); 
      this.cb_UART_RPA0.TabIndex = 5; 
      this.cb_UART_RPA0.UseVisualStyleBackColor = true; 
      this.cb_UART_RPA0.CheckedChanged += new System.EventHandler(this.cb_UART_RPA0_CheckedChanged); 
      // 
      // Form1 
      // 
      this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); 
      this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 
      this.ClientSize = new System.Drawing.Size(758, 429); 
      this.Controls.Add(this.tabControl1); 
      this.Name = "Form1"; 
      this.Text = "Form1"; 
      this.Load += new System.EventHandler(this.Form1_Load); 
      this.tabControl1.ResumeLayout(false); 
      this.tpPWM.ResumeLayout(false); 
      this.tpPWM.PerformLayout(); 
      this.tpUART.ResumeLayout(false); 
      this.tpUART.PerformLayout(); 
      this.tpADC.ResumeLayout(false); 
      this.tpADC.PerformLayout(); 
      this.ResumeLayout(false); 

     } 

     #endregion 

     private System.Windows.Forms.TabControl tabControl1; 
     private System.Windows.Forms.TabPage tpPWM; 
     private System.Windows.Forms.Label lbl_PWM_RPA0; 
     private System.Windows.Forms.CheckBox cb_PWM_RPA0; 
     private System.Windows.Forms.TabPage tpUART; 
     private System.Windows.Forms.TabPage tpADC; 
     private System.Windows.Forms.Label lbl_ADC_RPA0; 
     private System.Windows.Forms.CheckBox cb_ADC_RPA0; 
     private System.Windows.Forms.ToolTip toolTip1; 
     private System.Windows.Forms.CheckBox cb_UART_RPA0; 
     private System.Windows.Forms.Label lbl_UART_RPA0; 
    } 
} 
2

我試過很多,但最終採用這種簡單的技巧,我認爲這是更有效的。

創建一個子類(CustomControl與它只是基本控制),它擴展用戶控件

然後,而不是設置的「已啓用」屬性設置爲false創造出禁用方法只是在它BASECONTROL整個CustomControl代替。

在CustomControl上設置工具提示仍然可以觸發設置禁用basecontrol的eventhandlers。這適用於CustomControl正在使用的地方,而不是在您使用的每種表單上進行編碼。

這裏是暗示.. :)

public partial class MyTextBox : UserControl 
{ 
    ... 
    ... 
    ... 


    public void DisableMyTextBox() 
    { 
     this.txt.Enabled = false; //txt is the name of Winform-Textbox from my designer 
     this.Enabled = true; 
    } 

    public void EnableMyTextBox() 
    { 
     this.txt.Enabled = true; 
     this.Enabled = true; 
    } 

    //set the tooltip from properties tab in designer or wherever 
} 
0

我創建了一個新的用戶控件只包含一個按鈕。

public partial class TooltipButton : UserControl 
{ 
    public TooltipButton() 
    { 
     InitializeComponent(); 
    } 

    public new bool Enabled 
    { 
     get { return button.Enabled; } 
     set { button.Enabled = value; } 
    } 

    [Category("Appearance")] 
    [Description("The text displayed by the button.")] 
    [EditorBrowsable(EditorBrowsableState.Always)] 
    [Browsable(true)] 
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)] 
    [Bindable(true)] 
    public override string Text 
    { 
     get { return button.Text; } 
     set { button.Text = value; } 
    } 

    [Category("Action")] 
    [Description("Occurs when the button is clicked.")] 
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)] 
    public new event EventHandler Click; 

    private void button_Click(object sender, EventArgs e) 
    { 
     // Bubble event up to parent 
     Click?.Invoke(this, e); 
    } 
} 
相關問題