locked
C#: Differences between TextChanged and OnTextChanged RRS feed

  • Question

  • Hello

    When I use TextChanged for textbox and I use " textBox1.TextChanged -= textBox1_TextChanged" and  textBox1.TextChanged += textBox1_TextChanged, I see slow movement of cursor in textbox.

    But when I use "protected override void OnTextChanged" and "if( mIgnoreOnTextChanged ) return", I do not see slow movement of cursor in textbox.

    Why?

    Please see https://social.msdn.microsoft.com/Forums/vstudio/en-US/c2e8b9fc-424d-4f22-8a96-74424e2c06bc/c-preventing-textchaned-in-ontextchenged?forum=winforms


    • Edited by ARZARE Saturday, June 4, 2016 10:44 AM
    Saturday, June 4, 2016 10:43 AM

Answers

  • Hi ARZARE,
    You could try to use the sample to know the difference between two events:

     public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
    
            private void Form1_Load(object sender, EventArgs e)
            {
                CustomTextBox customtxt = new CustomTextBox();
                customtxt.TextChanged += (obj, arg) => { MessageBox.Show("TextChanged triggered!"); };
                Button btn = new Button();
                btn.Click += (obj, arg) =>
                {
                    customtxt.BaseTextChangedEvent = !customtxt.BaseTextChangedEvent;
                    (obj as Button).Text = customtxt.BaseTextChangedEvent ? "Disabled" : "Enabled";
                };
                btn.Text = customtxt.BaseTextChangedEvent ? "Disabled" : "Enabled";
                btn.Location = new Point(customtxt.Location.X + customtxt.Width + 5, customtxt.Location.Y);
                this.Controls.Add(customtxt);
                this.Controls.Add(btn);
    
            }
    
        }
        public class CustomTextBox : TextBox
        {
            public bool BaseTextChangedEvent { get; set; } = false;
    
            protected override void OnTextChanged(EventArgs e)
            {
                base.OnTextChanged(e);
                if (BaseTextChangedEvent)
                    MessageBox.Show("OnTextChanged triggered!");
            }
        }
    }
    

    >>            base.OnTextChanged(e);
    Above code is call the base event which you have used on 'textBox1.TextChanged += textBox1_TextChanged', if you haven't defined the event, it not triggered. If you comment or remove this line, the textchanged event will not triggered.
    override (C# Reference)
    https://msdn.microsoft.com/en-us/library/ebca9ah3.aspx
    Regards,
    Moonlight


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Monday, June 6, 2016 5:59 AM

All replies

  • Hi ARZARE,
    You could try to use the sample to know the difference between two events:

     public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
    
            private void Form1_Load(object sender, EventArgs e)
            {
                CustomTextBox customtxt = new CustomTextBox();
                customtxt.TextChanged += (obj, arg) => { MessageBox.Show("TextChanged triggered!"); };
                Button btn = new Button();
                btn.Click += (obj, arg) =>
                {
                    customtxt.BaseTextChangedEvent = !customtxt.BaseTextChangedEvent;
                    (obj as Button).Text = customtxt.BaseTextChangedEvent ? "Disabled" : "Enabled";
                };
                btn.Text = customtxt.BaseTextChangedEvent ? "Disabled" : "Enabled";
                btn.Location = new Point(customtxt.Location.X + customtxt.Width + 5, customtxt.Location.Y);
                this.Controls.Add(customtxt);
                this.Controls.Add(btn);
    
            }
    
        }
        public class CustomTextBox : TextBox
        {
            public bool BaseTextChangedEvent { get; set; } = false;
    
            protected override void OnTextChanged(EventArgs e)
            {
                base.OnTextChanged(e);
                if (BaseTextChangedEvent)
                    MessageBox.Show("OnTextChanged triggered!");
            }
        }
    }
    

    >>            base.OnTextChanged(e);
    Above code is call the base event which you have used on 'textBox1.TextChanged += textBox1_TextChanged', if you haven't defined the event, it not triggered. If you comment or remove this line, the textchanged event will not triggered.
    override (C# Reference)
    https://msdn.microsoft.com/en-us/library/ebca9ah3.aspx
    Regards,
    Moonlight


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Monday, June 6, 2016 5:59 AM
  • Hello

    Thank you Moonlight Sheng

    Now if we use textBox and private void textBox1_TextChanged(object sender, EventArgs e) in Form, we use textBox1.TextChanged += textBox1_TextChanged.

    In this case, I see slow movement of cursor in textbox.Why?

    Tuesday, June 7, 2016 3:40 PM
  • Hi,

    >>In this case, I see slow movement of cursor in textbox.Why?

    Could you please share me the snipcode of the method? Without the code, it is hard to reproduce.

    Regards,

    Moonlight


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.



    Friday, June 10, 2016 2:10 AM