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 Boolean, ByVal chkTop As Boolean, ByVal
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