tags:

views:

35

answers:

1
+2  Q: 

FindForm and WPF

I have an application that has a WinForm(myForm).

In that WinForm there are a WPF UserControl(myWPF).

In that WPF UserControl there are a WinForm UserControl(myWinCtrl).

The myWinCtrl has a custom WinGrid(myWinGrid).

myWinGrid does internally a this.FindForm() and.... does not find myForm...

Is there a way to override the FindForm method in order that it includes the possibility of a WPF intermediary layer?

myGrid => myWinFormUC => Host => WpfUC => Host => myWinForm

So, I need myGrid finds myWinForm

A: 

You could use PInvoke with GetAncestor to directly get the top level form containing your control.

At that point, you could compare the resulting IntPtr to the handles in each form of Application.OpenForms to find the actual Windows Forms Form instance.


A possible brute force approach could be to use Control.PointToScreen with the center of your control, then search your Application.OpenForms for any active forms containing that point. The tricky part would be handling if you had overlapping forms, but only one should be active, so if this is in response to a UI action, the active form at that location should be the correct form...

Reed Copsey
not works very well... GetAncenstor returns IntPtr, but this is not a good one..., also System.Windows.Forms.Integration.WindowsFormsHost does not have a "Handler" property
serhio
@serhio Define "good one" please.
Jamie Keeling
@Jamie Keeling: the GetControl(thisIntPtr) should be at least, a form.
serhio