Adding Events To WinForms?

前端 未结 5 1970
臣服心动
臣服心动 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:51

    You need to add an event handler for that event. So in the properties menu, double-click on the field beside the KeyDown event and Visual Studio will create an event handler for you. It'll look something like this:

    private void textBox1_KeyDown(object sender, KeyEventArgs e)
    {
        // enter your code here
    }
    

    You can also subscribe to events yourself without using the Properties window. For example, in the form's constructor:

    textBox1.KeyDown += HandleTextBoxKeyDownEvent;
    

    And then implement the event handler:

    private void HandleTextBoxKeyDownEvent(object sender, KeyEventArgs e)
    {
        // enter your code here
    }
    

提交回复
热议问题