ARTICLE

Enable/Disable data controls and ToolStrip command buttons in Windows Form

Posted by Tajwer Jalal Articles | Windows Forms VB.NET September 10, 2010
This article will provide help to develop simple database oriented application, which will discuss methods to control functionality (Enable, Disable) of data controls and ToolStrip command buttons.
Download Files:
 
Reader Level:

Introduction

This article will provide help to develop simple database oriented application, which will discuss methods to control functionality (Enable, Disable) of data controls and ToolStrip command buttons. 

Background

Before looking at how these methods works, let's take a moment to imagine what is required when you press add new button or edit button. When you press "add new"  button all controls on form should be ready to accept new data.( which mean all controls like textboxes, combo-boxes should be enabled and empty to accept new data.), likewise when you press edit button all controls like textboxes, combo-boxes etc should be enabled. 

Writing Methods

Add a module named mdlcontrolling.vb  in your vb.net project.

Add following codes, sub SetToolBarButtons will take ts as ToolStrip and some Boolean arguments to make ts buttons enable or disable according to given Boolean arguments.

Public Sub SetToolBarButtons(ByVal ts As ToolStrip, ByVal ChangeMode As BooleanByVal chkTop As BooleanByVal chkAdd As Boolean,
     ByVal chkEdit As Boolean, ByVal chkDel As Boolean, ByVal chkBottom As Boolean)
        'Toolbar Buttons
        ts.Items("cmdAddNew").Enabled = chkAdd  And Not ChangeMode
        ts.Items("cmdEdit").Enabled =   chkEdit And Not ChangeMode
        ts.Items("cmdDelete").Enabled = chkDel  And Not ChangeMode
        '=================================
        ' Change mode false will enable Revert and Save button
        'Toolbar Buttons
        ts.Items("cmdRevert").Enabled = ChangeMode
        ts.Items("CmdSave").Enabled = ChangeMode
        '================================
        'Toolbar Buttons
        ts.Items("cmdExit").Enabled = Not ChangeMode
        ts.Items("cmdPrintPreview").Enabled = Not ChangeMode
        ts.Items("cmdPrint").Enabled = Not ChangeMode
        '================================
        'Toolbar Navigation buttons
        '================================
        ts.Items("cmdTop").Enabled = Not (ChangeMode Or chkTop)
        ts.Items("cmdPrevious").Enabled = Not (ChangeMode Or chkTop)
        ts.Items("cmdNext").Enabled = Not (ChangeMode Or chkBottom)
        ts.Items("cmdLast").Enabled = Not (ChangeMode Or chkBottom)
End Sub
Public Sub DisableAllButtons(ByVal ts As ToolStrip)
        Dim i As Integer = 0
        For i = 0 To ts.Items.Count - 1
        If ts.Items(i).GetType().ToString =                
                               "System.Windows.Forms.ToolStripButton" Then
                ts.Items(i).Enabled = False
            End If
        Next
        'Enabled Exit Button
         ts.Items("cmdExit").Enabled = True
End Sub

Following Sub will make controls enable or disable according to chgmod.

Public Sub EnabledDisabledFormControls(ByVal frm As Control, ByVal chgmod As Boolean)
    For Each ctl As Control In frm.Controls
        If ctl.GetType().ToString = "System.Windows.Forms.RadioButton" Or _
            ctl.GetType().ToString = "System.Windows.Forms.DataGridView" Or _
            ctl.GetType().ToString = "System.Windows.Forms.TextBox" Or _
            ctl.GetType().ToString = "System.Windows.Forms.DateTimePicker" Or _
            ctl.GetType().ToString = "System.Windows.Forms.ComboBox" Or _
            ctl.GetType().ToString = "System.Windows.Forms.CheckBox" Then
            ctl.Enabled = chgmod
        End If
        If ctl.GetType().ToString = "System.Windows.Forms.TabControl" Or _
            ctl.GetType().ToString = "System.Windows.Forms.TabPage" Or _
            ctl.GetType().ToString = "System.Windows.Forms.GroupBox" Or _
            ctl.GetType().ToString = "System.Windows.Forms.Panel" Then
            Call EnabledDisabledFormControls(ctl,chgmod)
          End If
    Next
End Sub

Public Sub AddNewRecord(ByVal frm As Control)
    For Each ctl As Control In frm.Controls
      Select Case ctl.GetType().ToString
          Case "System.Windows.Forms.TextBox"
                    ctl.Text = ""
          Case "System.Windows.Forms.DateTimePicker"
                    ctl.Text = Now.Date
          Case "System.Windows.Forms.ComboBox"
                    ctl.Text = ""
          Case "System.Windows.Forms.TabControl"
                    AddNewRecord(ctl)
          Case "System.Windows.Forms.TabPage"
                    AddNewRecord(ctl)
          Case "System.Windows.Forms.GroupBox"
                    AddNewRecord(ctl)
          Case "System.Windows.Forms.Panel"
                    AddNewRecord(ctl)
      End Select
    Next
        ChangeMode = True
        SetToolBarButtons(frmMain.ToolStrip, dtRights, ChangeMode, ChkTop, ChkBottom)
End Sub

Login to add your contents and source code to this article
share this article :
post comment
 

Good article welcome on the vbdotnetheaven.com

Posted by Dinesh Beniwal Sep 14, 2010
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor