Form CodeSample - Preventing Form from closing
From Visual WebGui Wiki
| This article will have a few sections added to it soon, based on the following article type skeleton: NewCodeSampleTemplate |
Contents |
Overview
This article presents a tip and a codesample on how you can prevent a Form, other than your mainform, from closing.
Windows Forms
In Windows Forms you would prevent any form from closing, by overriding the OnClosing method, somewhat like the following:
Protected Overrides Sub OnClosing(ByVal e As System.ComponentModel.CancelEventArgs) If Not chkMayClose.Checked Then e.Cancel = True Return Else MyBase.OnClosing(e) End If End Sub
Visual WebGui
In current implementation of Visual WebGui, this is also possible, but only if you register an event handler for the Closing event of the form. To do that, you can either guarantee that you will always register an event handler for the Closing event for all forms you need this, or you can add code like the following to your Form:
' Dummy event handler that does nothing Private Sub Dummy(ByVal sender As Object, ByVal e As EventArgs) End Sub ' Register our dummy event handler on Form Load Private Sub MyForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load AddHandler Me.Closing, AddressOf Dummy End Sub
And now you will have your OnClosing overridded method called whenever this form is closed. Note that this should work for all forms, except when you close your mainform by closing the browser's tab or window. In that case your OnClosing method will not be called. If you call Close() on the mainform programmatically, it will though.
Putting it all together
A full Visual WebGui sample, preventing a dialog form from closing until changes are saved, might look something like this
Imports Gizmox.WebGUI.Forms Public Class Form3 Dim blnProcessingClose As Boolean Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load AddHandler Me.Closing, AddressOf Dummy blnProcessingClose = False End Sub Private Sub Dummy(ByVal sender As Object, ByVal e As EventArgs) End Sub Private Sub GetConfirmClose(ByVal sender As Object, ByVal e As System.EventArgs) If CType(sender, Gizmox.WebGUI.Forms.Form).DialogResult = Gizmox.WebGUI.Forms.DialogResult.Yes Then Me.DialogResult = Gizmox.WebGUI.Forms.DialogResult.OK 'signal MainForm to save changes Me.Close() Else Me.DialogResult = Gizmox.WebGUI.Forms.DialogResult.Cancel blnProcessingClose = False End If End Sub Protected Overrides Sub OnClosing(ByVal e As System.ComponentModel.CancelEventArgs) If Not blnProcessingClose Then blnProcessingClose = True MessageBox.Show("Do you want to save your changes?", "You Have Unsaved Data!", MessageBoxButtons.YesNo, MessageBoxIcon.Question, AddressOf GetConfirmClose) e.Cancel = True ElseIf Me.DialogResult = Gizmox.WebGUI.Forms.DialogResult.Cancel Then blnProcessingClose = False e.Cancel = True Else blnProcessingClose = False MyBase.OnClosing(e) End If End Sub End Class
| The conversion of this code to C# has not been completed yet. Please feel free to contribute and add contents to the Wiki |
