Displaying
Modal
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
Not Resizeable
this.FormBorderStyle = FormBorderStyle.FixedSingle;
Closing a Form
this.Dispose 'this is essential when the dialog has been displayed using ShowDialog
this.Close
Modeless
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)
objNativeWindow.AssignHandle(objHandle)
objHandle = Nothing
Call objDialog.Show(objNativeWindow)
You must remember to dispose of the handle when the windows form is closed.
objNativeWindow.ReleaseHandle()
Me.Dispose
Me.Close()
Form Displaying a Form
Private btnDisplayForm_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles btnDisplayForm.Click
Dim objAnotherForm As frmAnotherForm
objAnotherForm = New frmAnotherForm
objAnotherForm.Show(System.Windows.Forms.Controls.FromHandle(Me.Handle))
End Sub
Closing
Forms by default can be closed by pressing the cross (X) in the top right corner.
If you want to prevent (or notify) a user from using the cross you can use the FormClosing event.
FormClosing Event
This event occurs as a form is being closed.
private void frmDialog_FormClosing(
object sender,
FormClosingEventArgs e)
{
e.Cancel = true;
}
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 True
this.Dispose();
this.Close():
Pressing ESC
It is possible to close a windows form by pressing the ESC key
Select the entire form
Look in the Properties window and find the "CancelButton" property
In the drop-down choose "btnClose".
Messages.Question_FormCancelConfirmation
Messages.Question_FormSaveChanges
private void Method_FormClose()
private void Method_FormSave()
private void btnClose_Click(
object sender,
EventArgs e)
{
this._cancelButtonPressed = true;
this.Close();
}
© 2026 Better Solutions Limited. All Rights Reserved. © 2026 Better Solutions Limited TopPrevNext