VB.NET: Win32 Hwnd to IWin32Window for use with Form.Show(Owner)

I'm converting an Excel COM Addin that was written in VB6 to VB.NET 2008. I have a form that I would like to make TopMost but only to Excel. I also have Excel's Win32 Hwnd but I get it using late binding so I'm not sure if it's OK to store it as an integer.

Dim xlHwnd As Integer = oAddin.ExcelApp.Hwnd ' (I'm using Addin Express 2009)
Dim myForm = New frmOneOfMyWindowsForms
myForm.TopMost = True
myForm.Show(xlHwnd) ' ***

*** This won't compile because I'm not allowed to convert an Integer (xlHwnd) to an IWin32Window in the myForm.Show statement.

How do I convert an Hwnd to an IWin32Window?

I suspect I have to pass an object that implements IWin32Window to Form.Show() but I have been unable to find any examples in VB.NET.

Thanks.
ou81aswellAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CodeCruiserCommented:
0
ou81aswellAuthor Commented:
Thanks!

I'm looking for VB.net examples. Those are C#.
0
CodeCruiserCommented:
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

ou81aswellAuthor Commented:
Thanks again!
That link produces a 404 type error for me.
0
CodeCruiserCommented:
Their site is down i think. Try this

http://www.dotnetspider.com/Convert/Csharp-To-Vb.aspx
0
ou81aswellAuthor Commented:
Thanks.

I used that tool to convert the following wrapper class from C# to VB:

public class WindowWrapper : System.Windows.Forms.IWin32Window
{
    public WindowWrapper(IntPtr handle)
    {
        _hwnd = handle;
    }

    public IntPtr Handle
    {
        get { return _hwnd; }
    }

    private IntPtr _hwnd;
}

The resulting VB.NET code was:

Public Class WindowWrapper
         Inherits System.Windows.Forms.IWin32Window
    Public  Sub New(ByVal handle As IntPtr)
        _hwnd = handle
    End Sub

    Public ReadOnly Property Handle() As IntPtr
        Get
                 Return _hwnd
        End Get
    End Property

    Private _hwnd As IntPtr
End Class

This class won't compile because of an error with the statement:

         Inherits System.Windows.Forms.IWin32Window

"Classes can inherit only from other classes."

I'm guessing that I have to define an Interface and an implementation.
0
CodeCruiserCommented:
Change the word inherits to "Implements"
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ou81aswellAuthor Commented:
Thanks. This is the resulting class.

Imports System.Windows.Forms

Public Class Win32WindowWrapper

    Implements System.Windows.Forms.IWin32Window

    Private _hwnd As IntPtr

    Public Sub New(ByVal handle As IntPtr)
        _hwnd = handle
    End Sub

    Public ReadOnly Property Handle() As IntPtr Implements IWin32Window.Handle
        Get
            Return _hwnd
        End Get
    End Property

End Class

I can use it as follows:

' xlHwnd is Excel's Application.Hwnd (Integer)
Dim owner as New Win32WindowWrapper(xlHwnd)
myForm.Show(owner)

This answers my question.

Many thanks.

Now I have to figure out how to toggle the state of "always on top of owner" when a form been shown with myForm.Show(owner), from code within that form. I presume I would have a checkbox on my Form that allows the user to toggle this state.
0
CodeCruiserCommented:
Glad to help :-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.