SoFunction
Updated on 2025-03-07

A brief discussion on PreviewTextInput in c# WPF

Today, when I was using the TextInput event of TextBox, I found that the event could not be triggered by any means. Then I was puzzled and finally found the answer on MSDN: The TextInput event may have been marked as being processed by the internal implementation of the composite control. For example,TextBoxIt is such a control: the TextInput event has been marked as processed during its combination. This is done because the control needs to interpret certain types of inputs (such as arrow keys) as having special meanings for the control. If you use the PreviewTextInput event to attach a handler to the text, you will get better results. This technology can deal with most cases where the control combination identifies this event as processed and prevents the handler from receiving the event during the event routing process. That is, the bubble event has already =true during the combination; so the event cannot be routed to this layer, so it cannot be responded, so it should be used when using TextInput.PreviewTextInputEvents rather thanTextInputEvent. If "12" has been entered in the current text box and then 3 needs to be entered again, this event will be triggered. At this time, we will =true, then TextBox will not show any, so this also gives us a lot of inspiration. For example, we can judge whether the current input value is the number we want in this event.

private void This_TextInput(object sender, TextCompositionEventArgs e)
    {
      string newText = ;
      int number;
      if ((, out number) && number > 5)
      {
         = false;
      }
      else
      {
         = true;//(This line of code is very important, which determines whether the content currently entered can be displayed)      }      
      
    }

So when our TextBox determines whether it is the result we need to decide whether to display the string, we need to use the TextInput event instead of the TextChanged event. This is the added character. For example, when entering "How are you" in the input method, this event will be triggered three times, namely: You, OK, or Is it? Of course, when I first used it, I used an alternative method, using the TextChanged event, and posted the code here. In fact, this is a big mistake. Of course, the function can be fully implemented, and the code here is also posted for reference only.

private void tb_tempreture_TextChanged(object sender, TextChangedEventArgs e)
        {
            string currentText = this.tb_tempreture.Text;
            if (!(currentText))
            {
                int length = ;
                string lastChar = (length - 1);
                Regex regex = new Regex("^[0-9]*$");
                Match match = (lastChar);
                bool isNumber = !(());
                int ascall = (int)(lastChar);
                //If the input is not Chinese or English or a silence, the following warning will pop up                if (!(ascall == 045 || ascall == 043 || isNumber))
                {
                    ("character" + "'" + lastChar + "'" + "Not a valid input," + "Please enter numbers, or enter '+' and '-' that represent temperature!", "hint", , );                   
                    this.tb_tempreture.Text = (0, length - 1);                   
                    this.tb_tempreture.Select(,0);//After each change, the cursor position is set to the end of the text                }
            }          
 
        }

Here is to determine whether the input is a number and the "+" and "-" sign. There are two points to note here: 1 When using (), you actually convert the result into the Unicode character corresponding to char, so pay attention to the usage of (). 2 When the MessageBox pops up, the input focus of the input box will automatically return to the front, so the focus must be manually moved to the latest input position. This.tb_tempreture.Select(,0);//After each change, the cursor position is set to the last part of the text.

The above is a brief discussion of the detailed content of PreviewTextInput in c# WPF. For more information about PreviewTextInput in c# WPF, please pay attention to my other related articles!