How to get text of an input text box during onKeyPress?

前端 未结 12 1661
遥遥无期
遥遥无期 2020-11-28 05:12

I am trying to get the text in a text box as the user types in it (jsfiddle playground):

12条回答
  •  天命终不由人
    2020-11-28 05:57

    easy...

    In your keyPress event handler, write

    void ValidateKeyPressHandler(object sender, KeyPressEventArgs e)
    {
        var tb = sender as TextBox;
        var startPos = tb.SelectionStart;
        var selLen= tb.SelectionLength;
    
        var afterEditValue = tb.Text.Remove(startPos, selLen)
                    .Insert(startPos, e.KeyChar.ToString()); 
        //  ... more here
    }
    

提交回复
热议问题