Search KB Filter article types
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
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.
ReferencesForum threadsIssues

About the author

Related Articles

Forms  
Title Update Author
Tags: Developers, Windows & Dialogs, C#, XML, 1. Beginner, 2. Intermediate, 3. Advanced, Pre v6.3, v6.3, v6.4 and Later
11/Jan/2007    2007/01/11
Tags: Architects, Developers, Windows & Dialogs, 1. Beginner, 2. Intermediate, 3. Advanced, Pre v6.3, v6.3, v6.4 and Later
04/Dec/2010    2010/12/04
Tags: Developers, Theme, 2. Intermediate, 3. Advanced, Pre v6.3, v6.3, v6.4 and Later
23/July/2010    2010/07/23
Tags: Architects, Developers, Windows & Dialogs, 1. Beginner, 2. Intermediate, 3. Advanced, Pre v6.3, v6.3, v6.4 and Later
04/Dec/2010    2010/12/04
Tags: Architects, Developers, Navigation, Windows & Dialogs, 1. Beginner, 2. Intermediate, 3. Advanced, Pre v6.3, v6.3, v6.4 and Later
24/May/2010    2010/05/24
Tags: Developers, Events, Windows & Dialogs, 1. Beginner, 2. Intermediate, Pre v6.3, v6.3, v6.4 and Later, 3. Advanced
23/July/2010    2010/07/23
.NET Web, Cloud and Mobile application delivery platform | Sitemap | Terms of Use | Privacy Statement | Copyright © 2005-2011 Visual WebGui®       Visual WebGui weblog on ASP.NET Visual WebGui Group on LinkedIn Visual WebGui updates on Twitter Visual WebGui Page on Facebook Visual WebGui YouTube Channel Visual WebGui Platform News RSS