TextBox allows Text more than the MaxLength when Text is Assigned

前端 未结 2 979
南方客
南方客 2021-01-27 11:09

TextBox\'s MaxLength was set to 5. It works fine that it does not allows to enter or paste the Text more than the MaxLength. But it allows when the Tex

2条回答
  •  半阙折子戏
    2021-01-27 11:40

    Is it the behaviour of TextBox?

    yes, it is behaviour of text box. From MSDN

    This property does not affect characters that are added programmatically.

    How to handle it in a generic way?

    You can control it progmatically by first checking the length of string and then assigning. Like,

    if(stringtoAssign.Length > yourTextBox.MaxLength )
    {
        //or throw an exception, mesagebox. What ever you want
        yourTextBox.Text = stringToAssign.SubString(0,yourTextBox.MaxLength);
    }
    

提交回复
热议问题