开发者

Giving focus back to main form

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 fo开发者_高级运维cus 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.

Giving focus back to main form


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


If you invoke

preview.show();

as

preview.show(this);

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


You could try,

For Form1 do,

public static Form1 Current;

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

Then from preview form,

Form1.Current.Focus();


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜