如何在C#winform中引用当前控件(通常具有焦点的控件)?
我习惯于在access vba中引用当前控件,如何在C#winform中这样做?我不确定是否有更好的方法,但p\Invoke为我解决了这个问题:如何在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
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:)你做事情很困难。