简体   繁体   English

TextBox.TextChanged和“CTRL-a”

[英]TextBox.TextChanged and “CTRL-a”

I write simply code: 我只写代码:

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

And: 和:

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

I get a windows with textbox("Text") and checkbox. 我得到一个带文本框(“文本”)和复选框的窗口。 When i focus on textbox and press Ctrl A checkbox toggle state, because TextBox.TextChanged raised and textBox1_TextChanged executed. 当我专注于文本框并按Ctrl A复选框切换状态,因为TextBox.TextChanged被提升并且textBox1_TextChanged被执行。 But, i can`t understand why TextChanged event raised on Ctrl A ? 但是,我不明白为什么在Ctrl A上引发了TextChanged事件?

I met the same problem, use this code below: 我遇到了同样的问题,使用下面的代码:

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

You can suppress this behaviour by adding the condition below: 您可以通过添加以下条件来抑制此行为:

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

        //rest of the code
    }

Workaround for my problem: 我的问题的解决方法:

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

I can`t find better solution. 我找不到更好的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TextBox.TextChanged事件限制为32000个字符? - TextBox.TextChanged event limited to 32000 chars? UWP等待Textbox.Textchanged事件触发 - UWP wait for Textbox.Textchanged event to fire 如何使 Silverlight TextBox.TextChanged 事件同步触发? - How to make Silverlight TextBox.TextChanged event fire synchronously? 如何禁用按钮,然后在textbox.textchanged事件后启用它? - How to disable button and then enable it after textbox.textchanged event? TextBox.TextChanged事件在Windows Phone 7模拟器上触发两次 - TextBox.TextChanged event firing twice on Windows Phone 7 emulator 如何在jquery onkeyup上触发TextBox.TextChanged事件? - How to fire TextBox.TextChanged event on jquery onkeyup? WebForms Textbox.TextChanged 事件在按钮单击时触发两次 - WebForms Textbox.TextChanged event is firing twice on button click 使ctrl-a适用于只读文本框 - Make ctrl-a work for readonly textbox 使用Rx将受限制的TextBox.TextChanged与TextBox.GotFocus /(delayed)LostFocus一起引发System.OperationCanceledException - Using Rx to join a throttled TextBox.TextChanged with TextBox.GotFocus/(delayed)LostFocus throws a System.OperationCanceledException 在sql表中搜索文本值,并将其显示在textbox.textChanged事件的列表框中 - Search for text value in sql table and display it in listbox on textbox.textChanged event
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM