<small id='MnUt8'></small><noframes id='MnUt8'>

      <tfoot id='MnUt8'></tfoot>

        <bdo id='MnUt8'></bdo><ul id='MnUt8'></ul>
      <i id='MnUt8'><tr id='MnUt8'><dt id='MnUt8'><q id='MnUt8'><span id='MnUt8'><b id='MnUt8'><form id='MnUt8'><ins id='MnUt8'></ins><ul id='MnUt8'></ul><sub id='MnUt8'></sub></form><legend id='MnUt8'></legend><bdo id='MnUt8'><pre id='MnUt8'><center id='MnUt8'></center></pre></bdo></b><th id='MnUt8'></th></span></q></dt></tr></i><div id='MnUt8'><tfoot id='MnUt8'></tfoot><dl id='MnUt8'><fieldset id='MnUt8'></fieldset></dl></div>
      <legend id='MnUt8'><style id='MnUt8'><dir id='MnUt8'><q id='MnUt8'></q></dir></style></legend>

        在不触发 TextChanged 事件的情况下更改文本框文本

        时间:2023-06-10
        <i id='DGj1S'><tr id='DGj1S'><dt id='DGj1S'><q id='DGj1S'><span id='DGj1S'><b id='DGj1S'><form id='DGj1S'><ins id='DGj1S'></ins><ul id='DGj1S'></ul><sub id='DGj1S'></sub></form><legend id='DGj1S'></legend><bdo id='DGj1S'><pre id='DGj1S'><center id='DGj1S'></center></pre></bdo></b><th id='DGj1S'></th></span></q></dt></tr></i><div id='DGj1S'><tfoot id='DGj1S'></tfoot><dl id='DGj1S'><fieldset id='DGj1S'></fieldset></dl></div>

                <tfoot id='DGj1S'></tfoot>
                  <bdo id='DGj1S'></bdo><ul id='DGj1S'></ul>
                  <legend id='DGj1S'><style id='DGj1S'><dir id='DGj1S'><q id='DGj1S'></q></dir></style></legend>
                    <tbody id='DGj1S'></tbody>

                  <small id='DGj1S'></small><noframes id='DGj1S'>

                  本文介绍了在不触发 TextChanged 事件的情况下更改文本框文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我在 C# 中的应用程序有一个带有 txt_TextChanged 事件的 Textbox.

                  My application in C# has a Textbox with a txt_TextChanged event.

                  private void txt_TextChanged(object sender, EventArgs e)
                  {
                    //Do somthin
                  }
                  

                  但有一个特定部分我想更改 txt.Text 而不触发 txt_TextChanged 事件.

                  But there's one specific part that I want to change txt.Text without firing the txt_TextChanged event.

                  txt.Text ="somthing" //Don't fire txt_TextChanged
                  

                  我该怎么做?

                  推荐答案

                  没有直接的方法来阻止 text 属性引发事件,但是您的事件处理程序可以使用标志来确定天气或不执行任务.这可能比附加和分离事件处理程序更有效.这可以通过页面中的变量甚至专门的类包装器来完成

                  There is no direct way to prevent the raising of events for the text property, however your event handler can use a flag to determine weather or not to perform a task. This i likely to be more efficient than attaching and detaching the event handler. This can be done by a variable within the page or even a specialized class wrapper

                  带变量:

                  skipTextChange = true;
                  txt.Text = "Something";
                  
                  protected void TextChangedHandler(object sender, EventArgs e) {
                    if(skipTextChange){ return; }
                    /// do some stuffl
                  }
                  

                  使用专门的事件处理程序包装器

                  With specialized event handler wrapper

                     var eventProxy = new ConditionalEventHandler<EventArgs>(TextBox1_TextChanged);
                      TextBox1.TextChanged = eventProxy.EventAction;
                  
                      eventProxy.RaiseEvents = false;
                      TextBox1.Text = "test";
                  
                  
                      public void TextBox1_TextChanged(object sender, EventArgs e) {
                         // some cool stuff;
                      }
                  
                      internal class ConditionalEventHadler<TEventArgs> where TEventArgs : EventArgs
                  {
                     private Action<object,TEventArgs> handler;
                  
                      public bool RaiseEvents {get; set;}
                  
                      public ConditionalEventHadler(Action<object, TEventArgs> handler)
                      {
                          this.handler = handler; 
                      }
                  
                      public void EventHanlder(object sender, TEventArgs e) {
                        if(!RaiseEvents) { return;}
                        this.handler(sender, e);
                      }
                  }
                  

                  这篇关于在不触发 TextChanged 事件的情况下更改文本框文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:禁用在 TextBox 中选择文本 下一篇:具有相同宽度字符的 C# .NET 多行文本框

                  相关文章

                  <small id='HJO3h'></small><noframes id='HJO3h'>

                • <i id='HJO3h'><tr id='HJO3h'><dt id='HJO3h'><q id='HJO3h'><span id='HJO3h'><b id='HJO3h'><form id='HJO3h'><ins id='HJO3h'></ins><ul id='HJO3h'></ul><sub id='HJO3h'></sub></form><legend id='HJO3h'></legend><bdo id='HJO3h'><pre id='HJO3h'><center id='HJO3h'></center></pre></bdo></b><th id='HJO3h'></th></span></q></dt></tr></i><div id='HJO3h'><tfoot id='HJO3h'></tfoot><dl id='HJO3h'><fieldset id='HJO3h'></fieldset></dl></div>

                • <legend id='HJO3h'><style id='HJO3h'><dir id='HJO3h'><q id='HJO3h'></q></dir></style></legend>
                    <tfoot id='HJO3h'></tfoot>
                      <bdo id='HJO3h'></bdo><ul id='HJO3h'></ul>