Modal - ShowDialog

These must be closed or hidden before continuing.
The FormClosing event will run regardless of the method used to close the form (me.close or corner cross)
If the form is displayed as a modal dialog box (using ShowDialog) clicking X in the upper right corner causes the form to be hidden and the DialogResult property to be set to DialogResult.Cancel.
This can be overridden by setting it to a different value in the FormClosing event.

Dim dlgDialog As New frmDialog() 
If dlgDialog.ShowDialog = System.Windows.Forms.DialogResult.Yes Then

End If

Modeless - Show

If the button event handlers call the Me.Dispose before they call Me.Close then the FormClosing event is not called.
The FormClosing event however is always called if you use the corner cross.

Dim dlgDialog As New frmDialog() 
Call dlgDialog.Show

Modeless - TopMost within Application

There is a property called TopMost but the problem with this is that it displays the windows form on top of everything (i.e. all application windows).
What you really want is for a windows form to just be the topmost in that particular application.
This is possible by passing in the windows handler for the parent application.
This is only necessary when a form is displayed as modeless but you want the form to remain on top of the active workbook or document.

In Excel the object model can provide you with this handle
In Word the object model cannot so we need to get the handle using windows API.

Dim objDialog As New frmDialog() 
Dim objNativeWindow As System.Windows.Forms.NativeWindow
Dim objHandle As System.IntPtr

   objNativeWindow = New System.Windows.Forms.NativeWindow

'Excel provides access to the window handle as part of the object model
   objHandle = New IntPtr(gExcelApplication.Hwnd)

'Word does not so you need to use the Windows API
   objHandle = clsWin32.FindWindow("OpusApp", gWordApplication.ActiveDocument.Name & " - " & gWordApplication.Caption)

'PowerPoint does not so you need to use the Windows API
   objHandle = clsWin32.FindWindow("PPFrameClass", gPowerPoint.ActivePresentation & " - " & gPowerPointApplication.Caption)

   objHandle = Nothing

   Call objDialog.Show(objNativeWindow)

You must remember to dispose of the handle when the windows form is closed.


Closing a Form

Me.Dispose ''this is essentail when the dialog has been displayed using ShowDialog  

FormClosing Event

This event occurs as a form is being closed.
The behaviour of this event is different depending on whether you have a modal or modeless windows form.

You can cancel this event to prevent the form closing by setting the Cancel property to Tru

© 2021 Better Solutions Limited. All Rights Reserved. © 2021 Better Solutions Limited TopPrevNext