3

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

Community
  • 1
  • 1
PeeHaa
  • 71,436
  • 58
  • 190
  • 262
  • +1 for the pic and sad face, hope these answers help you, they sound right to me but is it wrong your sadness made me smile =D – Paul C Aug 26 '11 at 15:43
  • @CodeBlend: It's not only wrong, it's something mental. It's called: sociopathy :) – PeeHaa Aug 26 '11 at 15:50

4 Answers4

2

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

kmcc049
  • 2,783
  • 17
  • 13
  • @mkcc049: I ended up with a combination of your answer and Arun's. – PeeHaa Aug 26 '11 at 16:59
  • Yeah I was going to suggest that use his method of invocation and wrap the Parent property in your own one cast to the right type so you had access to all members of the parent form. – kmcc049 Aug 26 '11 at 21:10
2

If you invoke

preview.show();

as

preview.show(this);

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

Arun
  • 2,493
  • 15
  • 12
  • I tried this but it doesn't work. Even if I fix your typo. `preview.Parent` should be `this.Parent` if used in preview pane I think. – PeeHaa Aug 26 '11 at 16:49
  • I ended up with a combination of your answer and kmcc049's. – PeeHaa Aug 26 '11 at 16:59
  • Sorry, yeah it should be this.Parent when inside preview object. Thank you! – Arun Aug 26 '11 at 17:04
  • I realized that I made a mistake in pointing at a wrong property. The property to be used is "Owner", not "Parent" - apologies! – Arun Aug 29 '11 at 20:47
1

You could try,

For Form1 do,

public static Form1 Current;

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

Then from preview form,

Form1.Current.Focus();
Chuck Savage
  • 11,775
  • 6
  • 49
  • 69
0

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

MbaiMburu
  • 875
  • 1
  • 10
  • 19