Step 1
Add a module named mdlcontrolling.vb or any descriptive name in your VB.NET project.
Step 2
Add the following codes copying from the site. Sub SetToolBarButtons
will take "ts
" as ToolStrip
and some Boolean arguments. Boolean arguments will set the button's state according to the given condition. E.g. ChangeMode
will enable all data controls to accept data, and only save or cancel buttons of ToolStrip
will turn enabled while rest will become disabled. ChkAdd
can be used to determine that specific user has rights to add new record, likewise chkEdit
for editing rights and chkdel
for deleting rights.
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)
ts.Items("cmdAddNew").Enabled = chkAdd And Not ChangeMode
ts.Items("cmdEdit").Enabled = chkEdit And Not ChangeMode
ts.Items("cmdDelete").Enabled = chkDel And Not ChangeMode
ts.Items("cmdRevert").Enabled = ChangeMode
ts.Items("CmdSave").Enabled = ChangeMode
ts.Items("cmdExit").Enabled = Not ChangeMode
ts.Items("cmdPrintPreview").Enabled = Not ChangeMode
ts.Items("cmdPrint").Enabled = Not ChangeMode
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
The following Sub
will make all data controls enabled or disabled 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 controls added in container controls
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
Empty all Controls when going to add a new record.
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
Now add a form to your project and add toolstrip tsMain
, add buttons cmdAddNew
, cmdEdit
, cmdDelete
, cmdSave
, cmdRevert
, cmdTop
, CmdPrevious
, cmdNext
, cmdBottom
, cmdPrint
, cmdExit
...
Add the following codes in your form (i.e. Form1.vb here you may replace name according to your form or map code accordingly).
Imports System.Data.SqlClient
Public Class Form1
Dim conStr As String = "Data Source=.;Initial Catalog=Northwind;Integrated Security=True"
Dim Con As New SqlConnection
Dim da As SqlDataAdapter
Dim dsMain As New DataSet
Dim intRow As Integer = 0
Dim chkTop As Boolean
Dim chkBottom As Boolean
Dim ChaneMode As Boolean = False
Dim chgEdit As Boolean = False
Dim chkAdd As Boolean = True
Dim chkEdit As Boolean = True
Dim chkDel As Boolean = True
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim sSql As String = "Select * from Categories order by categoryid"
con.ConnectionString = conStr
con.Open()
da = New SqlDataAdapter(sSql, con)
da.Fill(dsMain, "Categories")
If dsMain.Tables(0).Rows.Count > 0 Then
ShowData(intRow)
End If
chkTop = True
chkBottom = False
ChaneMode = False
EnabledDisabledFormControls(Me, ChaneMode)
SetToolBarButtons(tsMain, ChaneMode, chkAdd, chkEdit, chkDel, chkTop, chkBottom)
End Sub
Private Sub ShowData(ByVal intRow As Integer)
txtRowIndex.Text = intRow
txtCategoryId.Text = dsMain.Tables(0).Rows(intRow).Item("CategoryID").ToString
txtCategoryName.Text = dsMain.Tables(0).Rows(intRow).Item("CategoryName").ToString
txtCategoryDescription.Text = dsMain.Tables(0).Rows(intRow).Item("Description").ToString
End Sub
Private Sub cmdTop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdTop.Click
Try
If dsMain.Tables(0).Rows.Count > 0 Then
intRow = 0
chkTop = True
chkBottom = False
SetToolBarButtons(tsMain, ChaneMode, chkAdd, chkEdit, chkDel, chkTop, chkBottom)
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
ShowData(intRow)
End Sub
Private Sub cmdPrevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdPrevious.Click
Try
If dsMain.Tables(0).Rows.Count > 0 Then
If intRow > 0 Then
intRow -= 1
ChkTop = False
If intRow = 0 Then
chkTop = True
End If
End If
chkBottom = False
SetToolBarButtons(tsMain, ChaneMode, chkAdd, chkEdit, chkDel, chkTop, chkBottom)
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
ShowData(intRow)
End Sub
Private Sub cmdNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdNext.Click
Try
If dsMain.Tables(0).Rows.Count > 0 Then
If intRow < dsMain.Tables(0).Rows.Count - 1 Then
intRow += 1
chkBottom = False
If intRow = dsMain.Tables(0).Rows.Count - 1 Then
chkBottom = True
End If
End If
chkTop = False
SetToolBarButtons(tsMain, ChaneMode, chkAdd, chkEdit, chkDel, chkTop, chkBottom)
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
ShowData(intRow)
End Sub
Private Sub cmdLast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdLast.Click
Try
If dsMain.Tables(0).Rows.Count > 0 Then
intRow = dsMain.Tables(0).Rows.Count - 1
chkTop = False
chkBottom = True
SetToolBarButtons(tsMain, ChaneMode, chkAdd, chkEdit, chkDel, chkTop, chkBottom)
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
ShowData(intRow)
End Sub
Private Sub cmdAddNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAddNew.Click
chgEdit = False
ChaneMode = True
EnabledDisabledFormControls(Me, ChaneMode)
AddNewRecord(Me)
SetToolBarButtons(tsMain, ChaneMode, chkAdd, chkEdit, chkDel, chkTop, chkBottom)
txtCategoryId.Enabled = False
End Sub
Private Sub cmdEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdEdit.Click
ChaneMode = True
chgEdit = True
EnabledDisabledFormControls(Me, ChaneMode)
SetToolBarButtons(tsMain, ChaneMode, chkAdd, chkEdit, chkDel, chkTop, chkBottom)
txtCategoryId.Enabled = False
End Sub
Private Sub cmdRevert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdRevert.Click
chgEdit = False
ChaneMode = False
EnabledDisabledFormControls(Me, ChaneMode)
SetToolBarButtons(tsMain, ChaneMode, chkAdd, chkEdit, chkDel, chkTop, chkBottom)
End Sub
Private Sub txtRowIndex_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtRowIndex.Click
End Sub
Private Sub txtRowIndex_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtRowIndex.TextChanged
Try
If txtRowIndex.Text = "" Then Exit Sub
If CDbl(txtRowIndex.Text) < 0 Then
MsgBox("Row Index can not be Negative")
End If
If CInt(txtRowIndex.Text) > dsMain.Tables(0).Rows.Count - 1 Then
txtRowIndex.Text = dsMain.Tables(0).Rows.Count - 1
ElseIf CInt(txtRowIndex.Text) < 0 Then
txtRowIndex.Text = 0
Else
End If
intRow = CInt(txtRowIndex.Text)
ShowData(intRow)
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub txtRowIndex_Validated(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtRowIndex.Validated
End Sub
Private Sub cmdSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSave.Click
Try
Dim cb As New SqlCommandBuilder(da)
Dim dr As DataRow
If chgEdit = True Then
dr = dsMain.Tables("Categories").Rows(intRow)
dr.Item("CategoryName") = txtCategoryName.Text
dr.Item("Description") = txtCategoryDescription.Text
da.Update(dsMain, "Categories")
dsMain.AcceptChanges()
Else
dr = dsMain.Tables("Categories").NewRow
dr.Item("CategoryName") = txtCategoryName.Text
dr.Item("Description") = txtCategoryDescription.Text
dsMain.Tables("Categories").Rows.Add(dr)
da.Update(dsMain, "Categories")
dsMain.AcceptChanges()
intRow = dsMain.Tables(0).Rows.Count - 1
End If
chgEdit = False
ChaneMode = False
EnabledDisabledFormControls(Me, ChaneMode)
SetToolBarButtons(tsMain, ChaneMode, chkAdd, chkEdit, chkDel, chkTop, chkBottom)
ShowData(intRow)
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub cmdExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdExit.Click
Me.Close()
End Sub
End Class