Adding Events To WinForms?

前端 未结 5 1973
臣服心动
臣服心动 2020-12-11 19:58

I have a TextBox on a WinForm and I want to execute some code every time someone presses a key inside of that TextBox. I\'m looking at the events properties me

5条回答
  •  旧巷少年郎
    2020-12-11 20:45

    These answers will have visual studio generate the event and bind it behind the scenes in the Designer.cs file.

    If you want to know how to bind events yourself, it looks like this.

    MyTextBox.KeyDown += new KeyEventHandler(MyKeyDownFunction)
    
    private function MyKeyDownFunction(object sender, KeyEventArgs e) {
        // your code
    }
    

    If done this way, the new KeyEventHandler() part is optional. You can also use lambdas to avoid boilerplate code.

    MyTextBox.KeyDown += (s, e) => {
        // s is the sender object, e is the args
    }
    

提交回复
热议问题