2009-11-13 3 views
17

J'essaie d'afficher une info-bulle lorsque la souris survole un contrôle désactivé. Comme un contrôle désactivé ne gère aucun événement, je dois le faire dans le formulaire parent. J'ai choisi de le faire en gérant l'événement MouseMove dans le formulaire parent. Voici le code qui fait le travail:Affichage de l'info-bulle sur un contrôle désactivé

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

Le code ne gère l'affichage des infobulles pour le contrôle désactivé. Le problème est que lorsque la souris survole un contrôle désactivé, l'info-bulle continue à se fermer et à s'afficher à nouveau. À partir de l'heure d'affichage ajoutée dans l'info-bulle, lorsque la souris est au-dessus du formulaire parent, l'événement MouseMove est appelé à peu près toutes les 3 secondes, de sorte que l'info-bulle est mise à jour toutes les 3 secondes. Mais lorsque la souris dépasse un contrôle désactivé, l'info-bulle est actualisée toutes les secondes. En outre, lorsque l'info-bulle est actualisée au-dessus du formulaire, seul le texte est mis à jour avec un bref flash. Mais lorsque l'info-bulle est actualisée au-dessus d'un contrôle désactivé, les fenêtres de l'info-bulle se referment comme si la souris se déplaçait dans un contrôle activé et que l'info-bulle était censée être fermée. mais alors l'info-bulle réapparaît tout de suite.

Quelqu'un peut-il me dire pourquoi? Merci.

Répondre

6

La réponse est avéré être un peu plus simple , mais devait être appliqué à tout moment.

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

Vous pouvez afficher l'info-bulle une seule fois lorsque la souris atteint le contrôle disbled et la masquer lorsque la souris la quitte. Pls, jetez un oeil sur le code ci-dessous, il faut montrer un message d'info-bulle pour tous les contrôles désactivés sur le formulaire

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

espérons que cette aide, ce qui est

+0

Cela semble fonctionner presque. Certaines commandes se comportent correctement tandis que d'autres clignotent toujours. –

+2

Fonctionne mieux que DJ répondre pour moi. J'ai ajouté une commande control.Visible afin de ne pas afficher de conseils sur les contrôles invisibles. J'ai également déplacé le bloc de masquage avant le bloc show et vérifié si (_currentToolTipControl! = Null && _currentToolTipControl! = Control) {... Hide(); _currentToolTipControl = null; } (sans cela, un déplacement rapide vers un autre contrôle désactivé ne met pas à jour l'info-bulle). – mheyman

0

Voici comment je résoudre ce problème

J'ai une application qui génère du code automatiquement pour une PIC32MX.

L'application a 3 onglets Pages text = PWM, ADC et UART.

Sur chaque page de l'onglet J'ai un Check texte Box = RPA0

L'intention est, lorsqu'un périphérique utilise RPA0, l'autre périphérique est empêché d'utiliser cette broche, en désactivant sur les autres pages, et un texte d'info-bulle doit apparaître sur les cases à cocher désactivées indiquant (exemple "Utilisé par PWM") quel périphérique utilise cette broche.

Le problème est que le texte de l'info-bulle ne s'affiche pas dans une case à cocher désactivée.

Pour résoudre le problème, je viens de supprimer le texte des cases à cocher et des étiquettes insérées avec le texte que la case à cocher devrait avoir.

Lorsqu'une case à cocher est cochée, les autres cases à cocher sont désactivées et l'étiquette à côté de celle-ci prend un texte d'info-bulle. Lorsque l'étiquette est activée, le texte de l'info-bulle s'affiche, même si la case est désactivée.

Doublez le travail, la moitié de la complexité.

Voici le code et le concepteur 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

J'ai essayé beaucoup, mais fini par utiliser cette astuce simple que je pense qu'il est plus efficace.

Créer une sous-classe (CustomControl avec contrôle juste base elle) qui étend UserControl

alors au lieu de « DEBLOQUE » propriété false créer une méthode qui désactive juste basecontrol en elle au lieu de CustomControl tout.

Définir l'info-bulle sur CustomControl permet toujours de déclencher des gestionnaires d'événements qui désactivent le contrôle de base. Cela fonctionne partout où CustomControl est utilisé plutôt que de coder sur tous les formulaires que vous utilisez.

Voici l'indice .. :)

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

J'ai créé un nouveau UserControl qui contient uniquement un bouton.

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