将焦点返回到主窗体

发布于 2024-12-01 11:59:30 字数 829 浏览 2 评论 0原文

我有一个带有两种表单的 C# 应用程序。

第一个是主窗体,应始终打开。

第二个是用户可以启用的预览窗格。

当用户选择显示预览窗格(菜单选项)时,预览窗格将打开。

这就是我想要的。

但是我想防止预览窗格获得焦点。否则,如果用户想要访问菜单(位于主窗体上),他们首先单击,看起来什么也没有发生(但实际上焦点从预览切换到主窗体)。 只有在第二次单击后,他们才能访问菜单。

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

如果预览获得焦点,只需将焦点设置到主窗体即可。

但是,我似乎无法从预览窗格访问主窗体。

要显示预览窗格,我只需执行以下操作(在主窗体上):

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

我尝试通过执行以下操作将焦点返回主窗体(在预览窗口上):

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

但正如所述,我似乎无法访问它。

表单消失

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.

Which is what I want.

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.

form gone

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

夏末染殇 2024-12-08 11:59:30

您需要将主窗体的实例传递给子窗体。因此,在调用 show 之前设置的预览表单上创建一个属性。然后从预览表单的实例访问主表单的实例

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

℡Ms空城旧梦 2024-12-08 11:59:30

如果您调用

preview.show();

as,

preview.show(this);

您可以使用preview.Parent访问预览对象内的主窗体。

If you invoke

preview.show();

as

preview.show(this);

you can access the main form inside preview object with preview.Parent.

猛虎独行 2024-12-08 11:59:30

您可以尝试,

对于 Form1 执行,

public static Form1 Current;

public Form1()
{
    InitializeComponent();
    Current = this;
}

然后从预览表单中,

Form1.Current.Focus();

You could try,

For Form1 do,

public static Form1 Current;

public Form1()
{
    InitializeComponent();
    Current = this;
}

Then from preview form,

Form1.Current.Focus();
画尸师 2024-12-08 11:59:30

使用 ShowDialog() 开始预览,然后调用 ShowDialog() 后的下一行调用 this.Focus()

use ShowDialog() to start preview, then the next line after calling ShowDialog() call this.Focus()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文