Form CodeSample - Preventing Form from closing
Categories: Forms, Skinnable and Inherited Controls
|
Tags: Developers, Events, Navigation, 1. Beginner, 2. Intermediate, Pre v6.3, v6.3, v6.4 and Later, 3. Advanced
Revision:
1
Posted:
20/Sep/2009
Updated:
04/Dec/2010
Status:
Publish
Types: Code
|
This article will have a few sections added to it soon, based on the following article type skeleton: CodeSample OverviewThis article presents a tip and a codesample on how you can prevent a Form, other than your mainform, from closing. Windows FormsIn 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 WebGuiIn 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 togetherA 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. ReferencesForum threadsIssues
About the author
Related Articles
|
Forms
|
|
|
Tags:
Developers, Windows & Dialogs, C#, XML, 1. Beginner, 2. Intermediate, 3. Advanced, Pre v6.3, v6.3, v6.4 and Later
|
|
|
Tags:
Architects, Developers, Windows & Dialogs, 1. Beginner, 2. Intermediate, 3. Advanced, Pre v6.3, v6.3, v6.4 and Later
|
|
|
Tags:
Developers, Theme, 2. Intermediate, 3. Advanced, Pre v6.3, v6.3, v6.4 and Later
|
|
|
Tags:
Architects, Developers, Windows & Dialogs, 1. Beginner, 2. Intermediate, 3. Advanced, Pre v6.3, v6.3, v6.4 and Later
|
|
|
Tags:
Architects, Developers, Navigation, Windows & Dialogs, 1. Beginner, 2. Intermediate, 3. Advanced, Pre v6.3, v6.3, v6.4 and Later
|
|
|
Tags:
Developers, Events, Windows & Dialogs, 1. Beginner, 2. Intermediate, Pre v6.3, v6.3, v6.4 and Later, 3. Advanced
|
|
|
|