Click here to Skip to main content
15,887,135 members
Articles / Programming Languages / Visual Basic
Tip/Trick

Data Entry Form made Easy, Enabled / Disabled controls on Data Entry Form

Rate me:
Please Sign up or sign in to vote.
4.00/5 (1 vote)
18 Sep 2010CPOL 11.9K   3   2
Data Entry Form made Easy, Enabled / Disabled controls on Data Entry Form

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.



VB
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


The following Sub will make all data controls enabled or disabled according to chgmod.



VB
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 Ifif 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.



VB
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).

VB
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
                ' txtRowIndex.Text = 0
            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
                'dr.RowState = DataRowState.Added
                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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Program Manager
Pakistan Pakistan
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralReason for my vote of 4 Good explanation, a nice step toward... Pin
Minhajul Shaoun2-Oct-10 6:54
Minhajul Shaoun2-Oct-10 6:54 
GeneralMate, post this one as an article/blog with sample project w... Pin
thatraja15-Sep-10 19:13
professionalthatraja15-Sep-10 19:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.