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

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

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

        确定何时以及在文本框中添加或删除哪个字符

        时间:2023-06-09
            • <bdo id='baxXy'></bdo><ul id='baxXy'></ul>

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

            • <tfoot id='baxXy'></tfoot>

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

                    <tbody id='baxXy'></tbody>
                  <legend id='baxXy'><style id='baxXy'><dir id='baxXy'><q id='baxXy'></q></dir></style></legend>
                • 本文介绍了确定何时以及在文本框中添加或删除哪个字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我在 WPF 应用程序中有一个简单的文本框.

                  I have a simple text box in a WPF application.

                  我需要知道何时在文本框中添加/删除了一个字符、哪个字符以及在何处添加或删除了该字符.

                  I need to know when a character was added/deleted in the text box, which character and where it was added or deleted.

                  我想过使用TextBox.KeyDown事件,但是有一些问题:

                  I thought about using the TextBox.KeyDown event, but it has some problems:

                  • 我不知道角色是在哪里添加或删除的.
                  • 我不知道如何确定添加了哪个字符(来自 KeyEventArgs).

                  有什么想法吗?

                  推荐答案

                  找到了解决方案.在 WPF 中,TextBox.TextChanged 事件有一个 TextChangedEventArgs.在这个类中,有一个名为 Changes 的属性.

                  Found the solution. In WPF, the TextBox.TextChanged event has a TextChangedEventArgs. In this class, there is a property named Changes.

                  这是我的代码:

                  private void textBox1_TextChanged(object sender, TextChangedEventArgs e)
                  {
                      foreach (var change in e.Changes)
                      {
                          if (change.AddedLength > 0 && change.RemovedLength == 0)
                          {
                              if (change.AddedLength == 1)
                              {
                                  AddCharacter(textBox1.Text[change.Offset], change.Offset);
                              }
                              else
                              {
                                  AddString(textBox1.Text.Substring(change.Offset, change.AddedLength), change.Offset);  
                              }
                          }
                          else if (change.AddedLength == 0 && change.RemovedLength > 0)
                          {
                              if (change.RemovedLength == 1)
                              {
                                  RemoveCharacter(change.Offset);
                              }
                              else
                              {
                                  RemoveString(change.Offset, change.RemovedLength + change.Offset);
                              }
                          }
                          else if (change.AddedLength == 1 & change.RemovedLength == 1)
                          {
                              ReplaceCharacter(change.Offset, textBox1.Text[change.Offset]);
                          }
                          else
                          {
                              ReplaceString(change.Offset, change.Offset + change.RemovedLength, textBox1.Text.Substring(change.Offset, change.AddedLength));
                          }
                      }
                  }
                  

                  现在我只需要等待两天就可以接受这个答案.:)

                  Now I just need to wait two days to accept this answer. :)

                  还是谢谢.

                  这篇关于确定何时以及在文本框中添加或删除哪个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:C#中的只读文本框 下一篇:具有 Like 子句功能的自动完成文本框示例 %hello% C# Windows

                  相关文章

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

                • <tfoot id='Tn0hL'></tfoot>

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

                    1. <legend id='Tn0hL'><style id='Tn0hL'><dir id='Tn0hL'><q id='Tn0hL'></q></dir></style></legend>
                        <bdo id='Tn0hL'></bdo><ul id='Tn0hL'></ul>