Can I assign a method to multiple Form-based Events?

前端 未结 3 1596
没有蜡笔的小新
没有蜡笔的小新 2021-01-21 03:31

I\'m constructing a Form and it has several numericUpDown controls, several checkbox controls and some text boxes etc. Each control has a event method (CheckedChanged, ValueCha

3条回答
  •  臣服心动
    2021-01-21 04:03

    Yes, you don't want to write code like this. You don't have to, the Application.Idle event is ideal to update UI state. It runs every time after Winforms retrieved all pending messages from the message queue. So is guaranteed to run after any of the events you currently subscribe. Make it look like this:

        public Form1() {
            InitializeComponent();
            Application.Idle += UpdateTextLabel;
            this.FormClosed += delegate { Application.Idle -= UpdateTextLabel; };
        }
    
        void UpdateTextLabel(object sender, EventArgs e) {
            // etc..
        }
    

提交回复
热议问题