如果从外部应用程序返回则运行事件

时间:2019-04-04 10:31:35

标签: c# winforms

我具有以下功能,如果我跳回到应用程序,它将返回给我应用程序的选定控件。 现在,我要调用所选控件的事件。

实际上,如果选择了应用程序,我只想选择所选文本框控件的文本。 为此任务,我编写了一些_Enter事件,但是如果您跳回应用程序,则它们不会被触发

因此,我试图获得应用程序的主动控制并触发我准备的事件,但是运行事件失败。活动控件可以是10个文本框之一。请帮忙。

protected override void WndProc(ref Message m)
{
    //react to switching from an external application
    const int WM_ACTIVATEAPP = 0x001C;
    switch (m.Msg)
    {
        case WM_ACTIVATEAPP:
            {
                if (m.WParam.ToInt32() == 1)
                {
                    //null blocker
                    var temp = this.ActiveControl;
                    if (temp != null)
                    {
                        //if you have a splitcontainer, it is always the active control...
                        if (temp.GetType() == typeof(SplitContainer))
                        {
                            var containerControl = (SplitContainer)ActiveControl;
                            if (containerControl.ActiveControl is TextBox)
                            {
                                //how can i call the _Enter event of the selected control?
                                ((TextBox)containerControl.ActiveControl).Enter += ???;
                            }
                        }
                    }
                }
                break;
            }
    }
    // proceed with default processing
    base.WndProc(ref m);  
}

事件都是相同的:

private void tB_IMEI_Enter(object sender, EventArgs e)
{
    tB_IMEI.SelectionStart = 0;
    tB_IMEI.SelectionLength = tB_IMEI.TextLength;
}

编辑: BugFinder的评论起到了作用。 找到了一个用于Form Activate的旧事件,但它是在SplitContainer实施之前进行的。 我已修复它以与SplitContainer控件一起使用。 谢谢Bugfinder!

工作解决方案:

private void focusAllTheText(object sender, EventArgs e)
{
    TextBox myBox = ((TextBox)sender);
    myBox.SelectAll();
}
private void SearchProvision_Activated(object sender, EventArgs e)
{
    if (ActiveControl != null && ActiveControl is SplitContainer)
        focusAllTheText(((SplitContainer)ActiveControl).ActiveControl, EventArgs.Empty);
}

0 个答案:

没有答案
相关问题