2009-11-13 16 views
17

Estoy tratando de mostrar una información sobre herramientas cuando el mouse pasa sobre un control deshabilitado. Como un control deshabilitado no maneja ningún evento, tengo que hacer eso en el formulario principal. Elegí hacer esto manejando el evento MouseMove en el formulario principal. Aquí está el código que hace el trabajo:Mostrar información sobre herramientas sobre un control deshabilitado

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

El código maneja la información sobre herramientas para el control deshabilitado. El problema es que cuando el mouse pasa sobre un control deshabilitado, la información sobre herramientas se cierra y se vuelve a mostrar. A partir del tiempo de visualización que agregué en la información sobre herramientas, cuando el mouse está sobre el formulario principal, se llama al evento MouseMove aproximadamente cada 3 segundos, por lo que la información sobre herramientas se actualiza cada 3 segundos. Pero cuando el mouse está sobre un control desactivado, la información sobre herramientas se actualiza cada 1 segundo. Además, cuando la información sobre herramientas se actualiza por encima del formulario, solo el texto se actualiza con un breve flash. Pero cuando la información sobre herramientas se actualiza por encima de un control desactivado, las ventanas de información sobre herramientas se cierran como si el mouse se moviera hacia un control habilitado y se supone que la información sobre herramientas se cerrará. pero luego la información sobre herramientas vuelve a aparecer de inmediato.

¿Puede alguien decirme por qué es esto? Gracias.

Respuesta

6

La respuesta resultó ser un poco más simple , pero debe aplicarse en todo momento.

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

puede mostrar la información sobre herramientas solo una vez cuando el mouse toca el control blando y luego lo oculta cuando el mouse sale de él. Pls, echar un vistazo al siguiente código, que debe mostrar un mensaje de texto de ayuda para todos los controles deshabilitados en el formulario

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

esperanza que esta ayuda, que se refiere a

+0

Parece que casi funciona. Algunos controles se comportan correctamente mientras que otros siguen parpadeando. –

+2

Funciona mejor que la respuesta de DJ para mí. Agregué un control. Comprobación visible para que no aparecieran consejos sobre controles invisibles. También moví el bloque hide a antes del bloque show y verifiqué si (_currentToolTipControl! = Null && _currentToolTipControl! = Control) {... Hide(); _currentToolTipControl = null; } (sin esto, un movimiento rápido a otro control deshabilitado no actualiza la información sobre herramientas). – mheyman

0

Aquí es cómo resolver este problema

tengo una aplicación que genera código automáticamente para un PIC32MX.

La aplicación tiene 3 páginas de tab text text = PWM, ADC y UART.

En cada página de la ficha Tengo un texto de casilla de verificación = RPA0

es, cuando un periférico utiliza RPA0, se evita La intención del otro periférico el uso de ese pin, por su desactivación en las otras páginas, y un texto de información sobre herramientas debe aparecer en las casillas de verificación deshabilitadas que dicen (ejemplo "Usado por PWM") qué periférico está usando ese pin.

El problema es que el texto de información sobre herramientas no aparecerá en una casilla deshabilitada.

Para resolver el problema, simplemente eliminé el texto de las casillas de verificación y las etiquetas insertadas con el texto que debería tener la casilla de verificación.

Cuando se marca una casilla de verificación, las otras casillas de verificación están deshabilitadas y la etiqueta al lado toma un texto de sugerencia de herramienta.

Como la etiqueta está habilitada, aparece el texto de información sobre herramientas, incluso en una casilla de verificación deshabilitada.

Doble el trabajo, la mitad de la complejidad.

Aquí está el código y el diseñador para 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

Probé muchos, pero terminé usando este sencillo truco que creo que es más eficaz.

Crear una subclase (CustomControl con el control de la base solo en ella) que se extiende control de usuario

continuación, en lugar de establecer "habilitadas" propiedad en false crear un método que desactiva simplemente basecontrol en ella en lugar de CustomControl conjunto.

Establecer la información sobre herramientas en CustomControl aún podrá disparar eventhandlers estableciendo el control base deshabilitado. Esto funciona siempre que CustomControl esté en uso en lugar de codificar en cada formulario que utilice.

Aquí está la pista .. :)

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

He creado un nuevo control de usuario que sólo contiene un botón.

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); 
    } 
} 
Cuestiones relacionadas