Windows窗体:检测聚焦控件的更改

4 .net c# winforms

我正在Windows窗体应用程序中实现复制粘贴.当用户更改应用程序中的focus元素时,我需要为这两个操作启用/禁用条形按钮.

我可以使用以下内容找到当前的聚焦控件:http://www.syncfusion.com/FAQ/windowsforms/faq_c41c.aspx#q1021q,但是如何检测聚焦控件是否已更改?

Pau*_*sik 10

在表单加载事件处理程序中,您还可以遍历表单中包含的所有控件,并为每个可聚焦控件添加Enter事件的事件处理程序:

    private void Form1_Load(object sender, EventArgs e)
    {
        foreach (Control control in Controls)
        {
            control.Enter += ControlReceivedFocus;
        }
    }

    void ControlReceivedFocus(object sender, EventArgs e)
    {
        Debug.WriteLine(sender + " received focus.");
    }
Run Code Online (Sandbox Code Playgroud)

  • 你也可能需要迭代子控件......`if(control.HasChildren)`... (6认同)