Results 1 to 2 of 2

Thread: How to pass a c# window(form) handle to a VB6 project or form?

  1. #1

    Thread Starter
    Registered User
    Join Date
    Jul 2015
    Posts
    1

    How to pass a c# window(form) handle to a VB6 project or form?

    We have an application where we launch a VB modal dialog from a c# form on a button click. Even though the VB dialog is a modal window we are able to go back to C# dialog on click and then the application hangs.

    Looks like the VB6 form does not have a parent and hence the issue. So we decided to pass the form handle from c# to VB and make our c# dialog as a parent to the VB dialog. Any help or a sample code would be really appreciated.

    Once we click search on our c# dialog, we call this:

    gPMSearch.Search();

    Private Sub Class_Initialize()
    'Load the search form
    Set Form = New frmMain
    Load Form
    End Sub

    The form is loaded as below:

    Me.Show vbModal

  2. #2
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: How to pass a c# window(form) handle to a VB6 project or form?

    Quote Originally Posted by guru.july View Post
    Looks like the VB6 form does not have a parent and hence the issue.
    Actually, ...

    Quote Originally Posted by Bonnie West View Post
    ... all top-level Forms in a VB6 process are owned by the unseen ThunderMain window.
    Note that Parent Windows and Owner Windows do not mean the same thing!

    In order for your VB6 Form to be modal to your C# Form, you'll need to change the owner window of the VB6 Form from ThunderMain to your C# Form. You will also want to disable and re-enable the C# Form when the VB6 Form loads and unloads. Here's an example of doing it from the VB6 side:

    Code:
    Private Enum Consts
        [C# Form hWnd] = &H12345678
    End Enum
    
    Private Const GWL_HWNDPARENT As Long = (-8&)
    
    Private Declare Function EnableWindow Lib "user32.dll" (ByVal hWnd As Long, ByVal bEnable As Long) As Long
    Private Declare Function SetWindowLongW Lib "user32.dll" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
    
    Private Sub Form_Load()
        SetWindowLongW hWnd, GWL_HWNDPARENT, [C# Form hWnd]
        EnableWindow [C# Form hWnd], False
    End Sub
    
    Private Sub Form_Unload(Cancel As Integer)
        EnableWindow [C# Form hWnd], -True
    End Sub
    To fully replicate the behavior of modal windows, you'll have to perform one final step. When your C# Form closes, you should also close its owned VB6 Form. This can be accomplished by sending the WM_CLOSE message to the VB6 Form.
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width