📄 user.vb
字号:
Me.btnNext.Size = New System.Drawing.Size(32, 21)
Me.btnNext.TabIndex = 3
Me.btnNext.Text = ">"
'
'TextBox3
'
Me.TextBox3.Location = New System.Drawing.Point(104, 16)
Me.TextBox3.Name = "TextBox3"
Me.TextBox3.Size = New System.Drawing.Size(80, 21)
Me.TextBox3.TabIndex = 2
Me.TextBox3.Text = ""
'
'btnPrevious
'
Me.btnPrevious.Location = New System.Drawing.Point(72, 16)
Me.btnPrevious.Name = "btnPrevious"
Me.btnPrevious.Size = New System.Drawing.Size(32, 21)
Me.btnPrevious.TabIndex = 1
Me.btnPrevious.Text = "<"
'
'btnFirst
'
Me.btnFirst.Location = New System.Drawing.Point(40, 16)
Me.btnFirst.Name = "btnFirst"
Me.btnFirst.Size = New System.Drawing.Size(32, 21)
Me.btnFirst.TabIndex = 0
Me.btnFirst.Text = "|<"
'
'Form12
'
Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
Me.ClientSize = New System.Drawing.Size(304, 269)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.GroupBox1, Me.GroupBox3})
Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle
Me.MaximizeBox = False
Me.Name = "Form12"
Me.Text = "用户管理"
Me.GroupBox1.ResumeLayout(False)
Me.GroupBox2.ResumeLayout(False)
Me.GroupBox3.ResumeLayout(False)
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub FillDataView()
Try
objDataView = Nothing
'清空原来的数据视图
objDataSet = New DataSet()
'实例化一个数据集对象
objSqlConnection.Open()
'打开数据库连接
objDataAdpter = New SqlDataAdapter(UserSqlstr, objSqlConnection)
'将数据库的数据映射到数据适配器
objDataAdpter.Fill(objDataSet, "UserInfo")
'填充数据集的数据
objDataView = New DataView(objDataSet.Tables("UserInfo"))
'初始化数据视图
objCurrencyMannager = CType(Me.BindingContext(objDataView), CurrencyManager)
'获取设置当前窗体的绑定管理对象
objSqlConnection.Close()
'关闭数据连接,这一点很重要
objDataSet = Nothing
'清空数据集的数据
Catch
End Try
End Sub
Private Sub BindsField()
Me.TextBox1.DataBindings.Clear()
Me.TextBox2.DataBindings.Clear()
Me.ComboBox1.DataBindings.Clear()
Me.CheckBox1.DataBindings.Clear()
Me.CheckBox2.DataBindings.Clear()
Me.CheckBox3.DataBindings.Clear()
Me.CheckBox4.DataBindings.Clear()
'清除所有的数据绑定
'否则不能显示所有的数据而只能显示修改后的数据
Me.TextBox1.DataBindings.Add("Text", objDataView, "UserName")
Me.TextBox2.DataBindings.Add("Text", objDataView, "Password")
Me.ComboBox1.DataBindings.Add("SelectedIndex", objDataView, "Pepdom")
Me.CheckBox1.DataBindings.Add("Checked", objDataView, "ModTable")
Me.CheckBox2.DataBindings.Add("Checked", objDataView, "ModClerk")
Me.CheckBox3.DataBindings.Add("Checked", objDataView, "ModPreOrder")
Me.CheckBox4.DataBindings.Add("Checked", objDataView, "ModExOrder")
'重新进行数据绑定
End Sub
Private Sub ShowCurrentPosition()
Me.TextBox3.Text = _
objCurrencyMannager.Position + 1 & " of " & _
objCurrencyMannager.Count
'显示当前数据绑定的管理对象的数据位置
End Sub
Private Sub Form12_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
FillDataView()
BindsField()
ShowCurrentPosition()
Me.btnAdd.Enabled = False
Me.GroupBox2.Enabled = False
End Sub
Private Sub btnFirst_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles btnFirst.Click
objCurrencyMannager.Position = 0
ShowCurrentPosition()
'显示第一条记录
End Sub
Private Sub btnPrevious_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles btnPrevious.Click
objCurrencyMannager.Position -= 1
If objCurrencyMannager.Position = 0 Then
objCurrencyMannager.Position = objCurrencyMannager.Count - 1
End If
ShowCurrentPosition()
'显示前一条数据,并判断是否已经到了最初一条记录
End Sub
Private Sub btnNext_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles btnNext.Click
objCurrencyMannager.Position += 1
If objCurrencyMannager.Position = objCurrencyMannager.Count - 1 Then
objCurrencyMannager.Position = 0
End If
ShowCurrentPosition()
'显示后一条数据,并判断是否已经到了最后一条记录
End Sub
Private Sub btnLast_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles btnLast.Click
objCurrencyMannager.Position = objCurrencyMannager.Count - 1
ShowCurrentPosition()
'显示最后一条记录
End Sub
Private Sub btnNew_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles btnNew.Click
Me.TextBox1.ReadOnly = False
Me.TextBox2.Text = ""
If Me.ComboBox1.SelectedIndex = 0 Then
Me.CheckBox1.Checked = False
Me.CheckBox2.Checked = False
Me.CheckBox3.Checked = False
Me.CheckBox4.Checked = False
End If
If Me.ComboBox1.SelectedIndex = 1 Then
Me.CheckBox1.Checked = True
Me.CheckBox2.Checked = True
Me.CheckBox3.Checked = True
Me.CheckBox4.Checked = True
End If
If Me.ComboBox1.SelectedIndex = 2 Then
Me.CheckBox1.Checked = False
Me.CheckBox2.Checked = False
Me.CheckBox3.Checked = False
Me.CheckBox4.Checked = False
Me.GroupBox2.Enabled = True
End If
btnAdd.Enabled = True
'新建一条记录
End Sub
Private Sub btnUpdate_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles btnUpdate.Click
chk1 = CInt(Me.CheckBox1.CheckState)
chk2 = CInt(Me.CheckBox2.CheckState)
chk3 = CInt(Me.CheckBox3.CheckState)
chk4 = CInt(Me.CheckBox4.CheckState)
Dim i As Integer = Me.ComboBox1.SelectedIndex
Dim intPosition As Integer = objCurrencyMannager.Position
Sqlstr = "UPDATE UserInfo SET Password=" & _
"'" & Me.TextBox2.Text & "',Pepdom='" & i & "',ModTable=" & _
"'" & chk1 & "',ModClerk='" & chk2 & "',ModPreOrder='" & chk3 & "'," & _
"ModExOrder='" & chk4 & "'"
UpdateData(Sqlstr)
FillDataView()
BindsField()
objCurrencyMannager.Position = intPosition
ShowCurrentPosition()
MsgBox("已经成功更新了该用户资料", MsgBoxStyle.OkOnly + _
MsgBoxStyle.Exclamation, "更新成功")
End Sub
Private Sub btnSearch_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles btnSearch.Click
If Me.ComboBox1.SelectedIndex = 2 Then
Me.GroupBox2.Enabled = True
Else
Me.GroupBox2.Enabled = False
End If
Dim intPosition As Integer
objDataView.Sort = "Pepdom"
intPosition = objDataView.Find(Me.ComboBox1.SelectedIndex)
objCurrencyMannager.Position = intPosition
ShowCurrentPosition()
End Sub
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles ComboBox1.SelectedIndexChanged
If Me.ComboBox1.SelectedIndex = 2 Then
Me.GroupBox2.Enabled = True
Else
Me.GroupBox2.Enabled = False
End If
End Sub
Private Sub btnDelete_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles btnDelete.Click
Dim intPosition As Integer
intPosition = Me.BindingContext(objDataView).Position - 1
If intPosition < 0 Then
intPosition = 0
End If
Sqlstr = "DELETE FROM UserInfo WHERE UserName='" & Me.TextBox1.Text & "'"
UpdateData(Sqlstr)
FillDataView()
BindsField()
objCurrencyMannager.Position = intPosition
ShowCurrentPosition()
MsgBox("已经成功删除了该用户", MsgBoxStyle.OkOnly + _
MsgBoxStyle.Exclamation, "删除成功")
End Sub
Private Sub btnAdd_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles btnAdd.Click
chk1 = CInt(Me.CheckBox1.CheckState)
chk2 = CInt(Me.CheckBox2.CheckState)
chk3 = CInt(Me.CheckBox3.CheckState)
chk4 = CInt(Me.CheckBox4.CheckState)
Dim i As Integer = Me.ComboBox1.SelectedIndex
Dim intPosition As Integer = objCurrencyMannager.Position
Sqlstr = "INSERT INTO UserInfo " & _
"(UserName,Password,Pepdom,ModTable,ModClerk,ModPreOrder,ModExOrder) " & _
"VALUES ('" & Me.TextBox1.Text & "','" & Me.TextBox2.Text & "','" & i & "'," & _
"'" & chk1 & "','" & chk2 & "','" & chk3 & "','" & chk4 & "')"
UpdateData(Sqlstr)
FillDataView()
BindsField()
objCurrencyMannager.Position = intPosition
ShowCurrentPosition()
Me.TextBox1.ReadOnly = True
Me.btnAdd.Enabled = False
MsgBox("已经成功添加了该新用户", MsgBoxStyle.OkOnly + _
MsgBoxStyle.Exclamation, "添加成功")
End Sub
End Class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -