让焦点回到主窗体 [英] Giving focus back to main form

查看:153
本文介绍了让焦点回到主窗体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个形式C#应用程序。

I have a C# app with two forms.

第一个是主要形式应始终打开。

The first one is the main form which should always be open.

第二个是一个预览窗格,用户可以启用

The second one is a preview pane which the user can enable.

当用户选择来显示预览窗格(菜单选项),预览窗格得开了。

When the user selects to show the preview pane (menu option), the preview pane gets opened.

这就是我想要的。

不过,我想,以防止在预览窗格从不断得到焦点。否则,如果用户要访问菜单(这是主要形式),他们第一次点击,它看起来像什么都没发生(但其实从预览的焦点开关 - >主)。
只有在第二次点击就可以进入菜单

However I want to prevent the preview pane from ever getting the focus. Otherwise if users want to access to menu (which is on the main form) they first click and it looks like nothing is happening (but in fact the focus switches from preview -> main). Only after the second click they can access the menu.

所以我想我有一个非常简单的解决方案:

So I thought I had a pretty simple solution:

如果预览曾经获得焦点只是将焦点设置到主窗体。

If the preview ever gets the focus just set the focus to the main form.

但它看起来像我不能从预览窗格访问的主要形式

However it looks like I cannot access the main form from the preview pane.

要显示预览窗格中我简单地做(在​​主窗体上):

To show the preview pane I simply do (on the main form):

QRcodeGenerator.QrCodePreview preview = new QRcodeGenerator.QrCodePreview();
preview.show();



我试着这样做(在预览窗口)给予重点后面的主要形式有:

I tried to give the focus back the main form by doing (on the preview window):

private void QrCodePreview_GotFocus(object sender, EventArgs e)
{
    QrCodeGenerator.QrCodeSampleApp.focus();
}



但正如说,它看起来像我不能访问它。

But as stated it looks like I cannot access it.

推荐答案

你会需要在MainForm的实例传递到子窗体。所以,创建您在打电话前戏设置预览窗体上的属性。然后,从预览的形式

youll need to pass the instance of the mainform to the child form. So create a property on the preview form that you set before you call show. Then access that instance of the main form from the instance of the preview form

这篇关于让焦点回到主窗体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆