Blue Theme Orange Theme Green Theme Red Theme
 
DevExpress Free UI Controls
Home | Forums | Videos | Photos | Blogs | Beginners | Advertise with Us
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
Discover the top 5 tips for understanding .NET Interop
Search :       Advanced Search »
Home » Windows Forms » Enable/Disable data controls and ToolStrip command buttons in Windows Form

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

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.

Page Views : 2928
Downloads : 49
Rating :
 Rate it
Level : Beginner
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
WindowsApplication1.zip
 
 
DevExpress Free UI Controls
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 


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

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Tajwer Jalal
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
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.
Dynamic PDF
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.
Discover the top 5 tips for understanding .NET
Ricky Leeks presents the top 5 tips for understanding .NET Interoperability. Learn more.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
 Comments
Good Article by Dinesh On September 14, 2010
Good article welcome on the vbdotnetheaven.com
Reply | Email | Modify 
Mindcracker MVP Summit 2012
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.