question

ShwetaGoel-4677 avatar image
0 Votes"
ShwetaGoel-4677 asked NicoZhu-MSFT answered

WPF: how can we restrict TextBox only allow entry 25 Characters?

I am using Maxlength property of textbox in WPF UWP Windows app. This property is working on laptop in both PC and tablet mode but it is not working on touchscreen tablet.

How to implement maxlength peroperty for touchscreen tablet. Please suggest.

windows-uwpwindows-wpf
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

NicoZhu-MSFT avatar image
0 Votes"
NicoZhu-MSFT answered

Hello @ShwetaGoel-4677, Welcome to Microsoft Q&A,

During the testing, Maxlength property could work in touchscreen tablet device, please check if set the correct Maxlength value for the application. And if you want to delete text manually when the text length long than the Maxlength , you could use the `InputInjector` to delete it.

 private void TextBox_TextChanging(TextBox sender, TextBoxTextChangingEventArgs args)
 {
        
     if (sender.Text.Length > 15)
     {
    
         InputInjector inputInjector = InputInjector.TryCreate();
         var info = new InjectedInputKeyboardInfo();
         info.VirtualKey = (ushort)(VirtualKey.Back);
         inputInjector.InjectKeyboardInput(new[] { info });
    
     }
    
 }



If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.



5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.