ActiveControl只能指向SplitContainer (转)

因为splitContainer也是控件Control,所以窗体的ActiveControl为splitContainer也是合理的,但是可能我们对这样的窗口控件为ActiveControl并不太喜欢,我们需要的是像TextBox,Button等最基本的控件为ActiveControl,所以可以如下获取窗体的ActiveControl:

C# code?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
protected override void OnClick(EventArgs e)
{
    Control activeControl = this.getActiveControl();
    base.OnClick(e);
}
 
private Control getActiveControl()
{
    Control activeControl = this.ActiveControl;
    while ((activeControl as ContainerControl) != null)
    {
        activeControl = (activeControl as ContainerControl).ActiveControl;
    }
    return activeControl;
}

posted on 2014-03-26 14:32  猎骑兵  阅读(301)  评论(0)    收藏  举报

导航