user1934821
user1934821

Reputation: 757

How to access parent form?

Ok, so a lot of people have asked this question in one way or another, but there doesn't seem to be many answers out there apart from pass the parent object when calling the child. The problem is, I do not have access to the parent code.

Here is my situation.

Some code I have no access to, I just have use of the dll(the parent form), calls a function in my code (the child form).

My function makes a call to a 3rd party SDK(not so important) which needs the parent form as one of the parameters. Also, I have no access to the 3rd party code, only through some c++ libraries.

Can my child form ever know its parent, or is it doomed to be an orphan?

Upvotes: 3

Views: 5153

Answers (4)

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186668

In the most general case (since your form is somewhere in dll and you have to pass the parent form into 3rd party software) - WinAPI - you can retrieve the parent window handle with GetParent function

http://msdn.microsoft.com/en-us/library/windows/desktop/ms633510(v=vs.85).aspx

Something like that:

[DllImport("user32.dll",
           EntryPoint = "GetParent",
           CharSet = CharSet.Auto)]
internal static extern IntPtr GetParent(IntPtr hWnd); 

...

IntPtr parentHandle = GetParent(myForm.Handle); // <- If you have a form

...

IntPtr myFormHandle = ...
IntPtr myFormParentHandle = GetParent(myFormHandle); // <- If you have WHND only

// If there's a .net form with myFormParentHandle Handle you can retrieve it 
// Otherwise (e.g. form is not a .net one) you get null
Form parentForm = Control.FromHandle(myFormParentHandle) as Form;

Upvotes: 3

Matthew Watson
Matthew Watson

Reputation: 109537

Maybe I'm missing something, but you can just use ContainerControl.ParentForm

var parent = myForm.ParentForm;

Or (if you don't actually have a Form to interrogate, but have a Control instead):

var parent = myForm.Parent;

See http://msdn.microsoft.com/en-us/library/system.windows.forms.control.parent.aspx

Then the Windows API handle for that parent will be:

IntPtr handle = parent.Handle;

which you can pass to your 3rd party SDK.

See http://msdn.microsoft.com/en-us/library/system.windows.forms.control.handle.aspx

If you need to check that the parent is really a Form:

Form form = myForm.Parent as Form;

if (form != null)
    // Do something with form.

But like people said: You should just be able to use myForm.ParentForm

Upvotes: 2

user149113
user149113

Reputation: 46

It's better (for your mental sanity) to pass from the parent form all needed information to the child's forms so you only have to think in one way (parent form pass some information to the child, the child pass the information + more information to the next child and so on)

Do a form/class that depends of other forms is called hard coupling and its a problem to maintain software.

Upvotes: 0

Gusdor
Gusdor

Reputation: 14334

Does your form appear over the top of another? Try the Form.Owner property.

Are you absolutely sure that you want to do this? There may be a good reason that you are not given the opportunity to access the parent form. Take care that you do not break the framework or pattern by circumventing this restriction.

Upvotes: 0

Related Questions