Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在C#winform中引用当前控件(通常具有焦点的控件)?_C#_Winforms - Fatal编程技术网

如何在C#winform中引用当前控件(通常具有焦点的控件)?

如何在C#winform中引用当前控件(通常具有焦点的控件)?,c#,winforms,C#,Winforms,我习惯于在access vba中引用当前控件,如何在C#winform中这样做?我不确定是否有更好的方法,但p\Invoke为我解决了这个问题: private static Control GetFocusedControl() { Control focused = null; var handle = GetFocusedControlHandle(); if (handle != IntPtr.Zero) { focused = Cont

我习惯于在access vba中引用当前控件,如何在C#winform中这样做?

我不确定是否有更好的方法,但p\Invoke为我解决了这个问题:

private static Control GetFocusedControl()
{
    Control focused = null;
    var handle = GetFocusedControlHandle();

    if (handle != IntPtr.Zero)
    {
        focused = Control.FromHandle(handle);
    }

    return focused;
}

// ...
[DllImport("user32.dll")]
private static extern IntPtr GetFocusedControlHandle();

也可以使用窗体的属性

我使用codekaizen的代码,并将其与计时器和几个控件(DataGridView、面板以及面板中的按钮和复选框)一起放入表单中。在计时器的滴答声事件中添加了此代码:

private void timer1_Tick(object sender, EventArgs e)
{
    label1.Text = ActiveControl.Name;
    label2.Text = GetFocusedControl().Name;
}

当我从一个控件点击到另一个控件时,他们报告了相同的活动控件。

哇,我希望有更好的控件是的。。。或者微软忘记了一个明显的特性,但是你聪明的代码是+1:)你做事情很困难。