Form Concepts CodeSample - Preventing Browser mainform close if page dirty

From Visual WebGui Wiki

Jump to: navigation, search


Overview

This codesample will demonstrate how you can use the Form.BeforeUnloadMessage helping you stop a user from leaving the form if some conditions are met.

You will not get any event fired, but by setting the property to some string value, the user is unable to navigate away from the form without getting notified. The notification is a MessageBox asking the user to confirm if he wants to leave the page or not. It will also be shown if the user tries to close the browser.

VB.NET Code

Public Class PreventCloseIfDirty
 
    Sub New()
        MyBase.New()
        ' This call is required by the Windows Form Designer.
        InitializeComponent()
 
        ' Add any initialization after the InitializeComponent() call.
        SetupControls()
    End Sub
 
    Friend WithEvents lblPageStatus As Gizmox.WebGUI.Forms.Label
    Friend WithEvents btnClean As Gizmox.WebGUI.Forms.Button
    Friend WithEvents txtData As Gizmox.WebGUI.Forms.TextBox
    Friend WithEvents btnRedirect As Gizmox.WebGUI.Forms.Button
    Friend WithEvents btnTransfer As Gizmox.WebGUI.Forms.Button
    Friend WithEvents Label1 As Gizmox.WebGUI.Forms.Label
    Friend WithEvents Label2 As Gizmox.WebGUI.Forms.Label
 
    Private Sub SetupControls()
        Me.lblPageStatus = New Gizmox.WebGUI.Forms.Label
        Me.btnClean = New Gizmox.WebGUI.Forms.Button
        Me.txtData = New Gizmox.WebGUI.Forms.TextBox
        Me.btnRedirect = New Gizmox.WebGUI.Forms.Button
        Me.btnTransfer = New Gizmox.WebGUI.Forms.Button
        Me.Label1 = New Gizmox.WebGUI.Forms.Label
        Me.Label2 = New Gizmox.WebGUI.Forms.Label
        '
        'lblPageStatus
        '
        Me.lblPageStatus.Location = New System.Drawing.Point(12, 121)
        Me.lblPageStatus.Name = "lblPageStatus"
        Me.lblPageStatus.Size = New System.Drawing.Size(237, 23)
        Me.lblPageStatus.TabIndex = 0
        Me.lblPageStatus.Text = "No changes unsaved"
        Me.lblPageStatus.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
        '
        'btnClean
        '
        Me.btnClean.Location = New System.Drawing.Point(12, 12)
        Me.btnClean.Name = "btnClean"
        Me.btnClean.Size = New System.Drawing.Size(237, 23)
        Me.btnClean.TabIndex = 0
        Me.btnClean.Text = "Set page clean (no unsaved changes)"
        '
        'txtData
        '
        Me.txtData.BorderStyle = Gizmox.WebGUI.Forms.BorderStyle.Fixed3D
        Me.txtData.Location = New System.Drawing.Point(12, 98)
        Me.txtData.Name = "txtData"
        Me.txtData.Size = New System.Drawing.Size(237, 20)
        Me.txtData.TabIndex = 0
        '
        'btnRedirect
        '
        Me.btnRedirect.Location = New System.Drawing.Point(15, 179)
        Me.btnRedirect.Name = "btnRedirect"
        Me.btnRedirect.Size = New System.Drawing.Size(234, 23)
        Me.btnRedirect.TabIndex = 0
        Me.btnRedirect.Text = "Redirect to WebGui"
        '
        'btnTransfer
        '
        Me.btnTransfer.Location = New System.Drawing.Point(15, 208)
        Me.btnTransfer.Name = "btnTransfer"
        Me.btnTransfer.Size = New System.Drawing.Size(234, 49)
        Me.btnTransfer.TabIndex = 0
        Me.btnTransfer.Text = "Transfer to new instance of form - will NOT be prevented"
        '
        'Label1
        '
        Me.Label1.Location = New System.Drawing.Point(12, 260)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(237, 23)
        Me.Label1.TabIndex = 0
        Me.Label1.Text = "Try F5 for refreshing form"
        Me.Label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
        '
        'Label2
        '
        Me.Label2.Location = New System.Drawing.Point(12, 69)
        Me.Label2.Name = "Label2"
        Me.Label2.Size = New System.Drawing.Size(237, 26)
        Me.Label2.TabIndex = 0
        Me.Label2.Text = "Enter data and tab out to make page dirty"
        Me.Label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
        '
        'PreventCloseIfDirty
        '
        Me.Controls.Add(Me.Label2)
        Me.Controls.Add(Me.Label1)
        Me.Controls.Add(Me.btnTransfer)
        Me.Controls.Add(Me.btnRedirect)
        Me.Controls.Add(Me.txtData)
        Me.Controls.Add(Me.btnClean)
        Me.Controls.Add(Me.lblPageStatus)
        Me.Size = New System.Drawing.Size(269, 336)
        Me.Text = "PreventCloseIfDirty"
 
    End Sub
 
    Private Sub PreventCloseIfDirty_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        lblPageStatus.Text = "No changes unsaved"
        Me.BeforeUnloadMessage = ""
    End Sub
 
    Private Sub FlagDirty()
        lblPageStatus.Text = "Remember to save changes"
        Me.BeforeUnloadMessage = "There are unsaved changes on page"
    End Sub
 
    Private Sub txtData_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtData.TextChanged
        FlagDirty()
    End Sub
 
    Private Sub btnRedirect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRedirect.Click
        Context.Redirect("http://www.visualwebgui.com")
    End Sub
 
    Private Sub btnTransfer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTransfer.Click
        Context.Transfer(New PreventCloseIfDirty)
    End Sub
End Class

C# Code

Personal tools