2012-09-10 12 views
5

que escribir simplemente código:TextBox.TextChanged y "Ctrl-A"

using System.Windows.Forms; 

namespace Test01 
{ 
    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.textBox1 = new System.Windows.Forms.TextBox(); 
      this.checkBox1 = new System.Windows.Forms.CheckBox(); 
      this.SuspendLayout(); 
      // 
      // textBox1 
      // 
      this.textBox1.Location = new System.Drawing.Point(33, 32); 
      this.textBox1.Name = "textBox1"; 
      this.textBox1.Size = new System.Drawing.Size(186, 20); 
      this.textBox1.TabIndex = 0; 
      this.textBox1.Text = "Text"; 
      this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged); 
      // 
      // checkBox1 
      // 
      this.checkBox1.AutoSize = true; 
      this.checkBox1.Location = new System.Drawing.Point(38, 65); 
      this.checkBox1.Name = "checkBox1"; 
      this.checkBox1.Size = new System.Drawing.Size(80, 17); 
      this.checkBox1.TabIndex = 1; 
      this.checkBox1.Text = "checkBox1"; 
      this.checkBox1.UseVisualStyleBackColor = true; 
      // 
      // Form1 
      // 
      this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); 
      this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 
      this.ClientSize = new System.Drawing.Size(234, 86); 
      this.Controls.Add(this.checkBox1); 
      this.Controls.Add(this.textBox1); 
      this.Name = "Form1"; 
      this.Text = "Form1"; 
      this.ResumeLayout(false); 
      this.PerformLayout(); 

     } 

     void textBox1_TextChanged(object sender, System.EventArgs e) 
     { 
      if (this.checkBox1.Checked) 
       this.checkBox1.CheckState = CheckState.Unchecked; 
      else 
       this.checkBox1.CheckState = CheckState.Checked; 
     } 

     #endregion 

     private System.Windows.Forms.TextBox textBox1; 
     private System.Windows.Forms.CheckBox checkBox1; 
    } 
} 

Y:

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

me sale una ventana con el cuadro de texto ("texto") y la casilla de verificación. Cuando me centro en el cuadro de texto y presiono CtrlA casilla de verificación cambiar de estado, porque TextBox.TextChanged se ejecutó y textBox1_TextChanged se ejecutó. Pero no puedo entender por qué el evento TextChanged se produjo en CtrlA?

+1

me di cuenta de que esto no sucede si el texto ya está seleccionado, por lo que podría estar relacionado con la selección del texto en lugar de la combinación de teclas. –

+0

@JustinHarvey Sí, no sucede si el texto ya está seleccionado y presiono ctrl-a. No sucede cuando selecciono texto con el mouse o con el menú contextual, también. Pero, no puedo encontrar una explicación. – hired777

+0

Intenté depurar un poco, parece que el cuadro de texto recibe exactamente el mismo mensaje que cuando se escribe el texto ('WM_USER wparam: 0300075C lParam: 0006075C'). Entonces, el comportamiento parece "normal" en el lado .NET, y debería ser similar con una aplicación C/C++. Lamentablemente, no sé lo suficiente acerca de la API de Windows para profundizar más ... –

Respuesta

6

me encontré con el mismo problema, utilice el código de abajo:

private void textBox1_TextChanged(Object sender, EventArgs e) 
{ 
    if (textBox1.SelectedText == textBox1.Text && textBox1.Text != "") 
     return; 
} 
0

Puede suprimir este comportamiento mediante la adición de la condición a continuación:

void textBox1_TextChanged(object sender, System.EventArgs e) 
    { 
     if (ModifierKeys == Keys.Control) 
      return; 

     //rest of the code 
    } 
0

Solución para mi problema:

void textBox1_TextChanged(object sender, System.EventArgs e) 
{ 
    if (_oldText.Equals(this.textBox1.Text)) 
     return; 
    _oldText = this.textBox1.Text; 
    //rest of code 
} 

¡No puedo encontrar mejor solución.