2012-09-10 30 views
5

tôi viết đơn giản mã:TextBox.TextChanged và "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; 
    } 
} 

Và:

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

tôi nhận được một cửa sổ với textbox ("Text") và hộp kiểm. Khi tôi tập trung vào hộp văn bản và nhấn CtrlTrạng thái chuyển đổi hộp kiểm, vì TextBox.TextChanged raise và textBox1_TextChanged execution. Nhưng, tôi không thể hiểu tại sao sự kiện TextChanged được tăng lên trên CtrlA?

+1

tôi đã thông báo rằng nó không xảy ra nếu văn bản đã được chọn, vì vậy có thể làm gì với lựa chọn của văn bản chứ không phải là tổ hợp phím. –

+0

@JustinHarvey Có nó không xảy ra nếu văn bản đã được chọn và tôi nhấn ctrl-a. Nó không xảy ra khi tôi chọn văn bản bằng chuột hoặc với menu ngữ cảnh. Nhưng, tôi không thể tìm được lời giải thích. – hired777

+0

Tôi đã thử gỡ lỗi một chút, có vẻ như hộp văn bản nhận được chính xác cùng một thông báo như khi văn bản được nhập ('WM_USER wparam: 0300075C lParam: 0006075C'). Vì vậy, các hành vi có vẻ 'bình thường' trên bên .NET, và nên được tương tự với một ứng dụng C/C++. Thật không may, tôi không biết đủ về API Windows để khai thác thêm nữa ... –

Trả lời

6

tôi gặp cùng một vấn đề, sử dụng mã này dưới đây:

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

Bạn có thể ngăn chặn hành vi này bằng cách thêm điều kiện dưới đây:

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

     //rest of the code 
    } 
0

Giải pháp cho vấn đề của tôi:

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

tôi có thể t `tìm giải pháp tốt hơn.

Các vấn đề liên quan