轻松创建数据录入表单,启用/禁用数据录入表单上的控件





4.00/5 (1投票)
轻松创建数据录入表单,启用/禁用数据录入表单上的控件
步骤 1
在你的 VB.NET 项目中添加一个名为 mdlcontrolling.vb 或任何描述性名称的模块。
第二步
复制网站上的以下代码。Sub SetToolBarButtons
将接受 "ts
" 作为 ToolStrip
以及一些布尔参数。 布尔参数将根据给定的条件设置按钮的状态。 例如,ChangeMode
将启用所有数据控件以接受数据,并且只有 ToolStrip
的保存或取消按钮将启用,而其余按钮将禁用。 ChkAdd
可用于确定特定用户是否有权添加新记录,同样 chkEdit
用于编辑权限,chkdel
用于删除权限。
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
以下 Sub
将根据 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
在转到添加新记录时,清空所有控件。
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
现在在你的项目中添加一个窗体,并添加 toolstrip tsMain
,添加按钮 cmdAddNew
、cmdEdit
、cmdDelete
、cmdSave
、cmdRevert
、cmdTop
、CmdPrevious
、cmdNext
、cmdBottom
、cmdPrint
、cmdExit
... 在你的窗体中添加以下代码(例如,这里 Form1.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