369 lines
12 KiB
VB.net
369 lines
12 KiB
VB.net
Imports System
|
|
Imports System.Data
|
|
Imports System.Data.SqlClient
|
|
Imports System.IO
|
|
Imports System.Text
|
|
Public Class userForm
|
|
|
|
Private cnstr = mainForm.cnstr
|
|
Private conn As New SqlConnection(cnstr)
|
|
Private myQuery As String = String.Empty
|
|
Private myCommand As SqlCommand
|
|
Private adapter As SqlDataAdapter = New SqlDataAdapter()
|
|
Private ds As New DataSet("Users")
|
|
Private i, limit As Integer
|
|
Private oldUserName As String = String.Empty
|
|
Private addMode, editMode As Integer
|
|
|
|
Private Sub users_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
|
|
Me.MdiParent = mainForm
|
|
Control.CheckForIllegalCrossThreadCalls = False
|
|
Me.Top = 0
|
|
Me.Left = 0
|
|
|
|
fillDataSet()
|
|
getplants()
|
|
getAccounts()
|
|
disableControls()
|
|
moveFirst()
|
|
addMode = 0
|
|
editMode = 0
|
|
btn_find.Visible = False
|
|
End Sub
|
|
|
|
Private Sub enableControls()
|
|
txtUsername.Enabled = True
|
|
txtPassword.Enabled = True
|
|
txtPassword2.Enabled = True
|
|
cmbPlant.Enabled = True
|
|
cmbAccount.Enabled = True
|
|
cmb_userType.Enabled = True
|
|
cbActive.Enabled = True
|
|
End Sub
|
|
|
|
Private Sub disableControls()
|
|
txtUsername.Enabled = False
|
|
txtPassword.Enabled = False
|
|
txtPassword2.Enabled = False
|
|
cmbPlant.Enabled = False
|
|
cmbAccount.Enabled = False
|
|
cmb_userType.Enabled = False
|
|
cbActive.Enabled = False
|
|
End Sub
|
|
|
|
Private Sub clear()
|
|
txtUsername.Text = String.Empty
|
|
txtPassword.Text = String.Empty
|
|
txtPassword2.Text = String.Empty
|
|
cmb_userType.SelectedIndex = 0
|
|
cbActive.Checked = False
|
|
End Sub
|
|
|
|
Public Sub toNormalMode()
|
|
ts_new.Enabled = True
|
|
ts_edit.Enabled = True
|
|
ts_delete.Enabled = True
|
|
ts_find.Enabled = True
|
|
ts_revert.Enabled = False
|
|
ts_save.Enabled = False
|
|
disableControls()
|
|
End Sub
|
|
|
|
Public Sub toEditMode()
|
|
ts_new.Enabled = False
|
|
ts_edit.Enabled = False
|
|
ts_delete.Enabled = False
|
|
ts_find.Enabled = False
|
|
ts_revert.Enabled = True
|
|
ts_save.Enabled = True
|
|
enableControls()
|
|
End Sub
|
|
|
|
Public Sub toAddMode()
|
|
ts_new.Enabled = False
|
|
ts_edit.Enabled = False
|
|
ts_delete.Enabled = False
|
|
ts_find.Enabled = False
|
|
ts_revert.Enabled = True
|
|
ts_save.Enabled = True
|
|
enableControls()
|
|
End Sub
|
|
|
|
Public Sub toSearchMode()
|
|
ts_new.Enabled = False
|
|
ts_edit.Enabled = False
|
|
ts_delete.Enabled = False
|
|
ts_find.Enabled = True
|
|
ts_revert.Enabled = False
|
|
ts_save.Enabled = False
|
|
End Sub
|
|
|
|
Private Sub fillDataSet()
|
|
Dim conn As New SqlConnection(cnstr)
|
|
myQuery = "SELECT * FROM Users ORDER BY username"
|
|
|
|
myCommand = New SqlCommand(myQuery, conn)
|
|
|
|
Try
|
|
conn.Open()
|
|
adapter.SelectCommand = myCommand
|
|
ds.Clear()
|
|
adapter.Fill(ds)
|
|
i = 0
|
|
limit = ds.Tables(0).Rows.Count - 1
|
|
Catch ex As Exception
|
|
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
|
|
Finally
|
|
conn.Close()
|
|
conn.Dispose()
|
|
End Try
|
|
End Sub
|
|
|
|
Private Sub trace(ByVal username As String)
|
|
Dim j As Integer = 0
|
|
|
|
For j = 0 To ds.Tables(0).Rows.Count - 1
|
|
If ds.Tables(0).Rows(j).Item("username") = username Then
|
|
txtUsername.Text = ds.Tables(0).Rows(j).Item("username")
|
|
txtPassword.Text = ds.Tables(0).Rows(j).Item("password")
|
|
txtPassword2.Text = ds.Tables(0).Rows(j).Item("password")
|
|
cmbPlant.SelectedItem = ds.Tables(0).Rows(j).Item("plant")
|
|
cmbAccount.SelectedItem = ds.Tables(0).Rows(j).Item("account")
|
|
cmb_userType.SelectedIndex = cmb_userType.Items.IndexOf(ds.Tables(0).Rows(j).Item("userType"))
|
|
If ds.Tables(0).Rows(j).Item("active") = 1 Then
|
|
cbActive.Checked = True
|
|
Else
|
|
cbActive.Checked = False
|
|
End If
|
|
i = j
|
|
Exit For
|
|
End If
|
|
Next
|
|
End Sub
|
|
|
|
Public Sub moveFirst()
|
|
If ds.Tables(0).Rows.Count <> 0 And limit > -1 Then
|
|
i = 0
|
|
txtUsername.Text = ds.Tables(0).Rows(i).Item("username")
|
|
oldUserName = ds.Tables(0).Rows(i).Item("username")
|
|
txtPassword.Text = ds.Tables(0).Rows(i).Item("password")
|
|
txtPassword2.Text = ds.Tables(0).Rows(i).Item("password")
|
|
cmbPlant.SelectedItem = ds.Tables(0).Rows(i).Item("plant")
|
|
cmbAccount.SelectedItem = ds.Tables(0).Rows(i).Item("account")
|
|
cmb_userType.SelectedItem = ds.Tables(0).Rows(i).Item("userType")
|
|
If ds.Tables(0).Rows(i).Item("active") = 1 Then
|
|
cbActive.Checked = True
|
|
Else
|
|
cbActive.Checked = False
|
|
End If
|
|
Else
|
|
clear()
|
|
End If
|
|
End Sub
|
|
|
|
Public Sub moveNext()
|
|
If i < limit And limit > -1 Then
|
|
i = i + 1
|
|
txtUsername.Text = ds.Tables(0).Rows(i).Item("username")
|
|
oldUserName = ds.Tables(0).Rows(i).Item("username")
|
|
txtPassword.Text = ds.Tables(0).Rows(i).Item("password")
|
|
txtPassword2.Text = ds.Tables(0).Rows(i).Item("password")
|
|
cmbPlant.SelectedItem = ds.Tables(0).Rows(i).Item("plant")
|
|
cmbAccount.SelectedItem = ds.Tables(0).Rows(i).Item("account")
|
|
cmb_userType.SelectedItem = ds.Tables(0).Rows(i).Item("userType")
|
|
If ds.Tables(0).Rows(i).Item("active") = 1 Then
|
|
cbActive.Checked = True
|
|
Else
|
|
cbActive.Checked = False
|
|
End If
|
|
End If
|
|
End Sub
|
|
|
|
Public Sub movePrevious()
|
|
If i <> 0 And limit > -1 Then
|
|
i = i - 1
|
|
txtUsername.Text = ds.Tables(0).Rows(i).Item("username")
|
|
oldUserName = ds.Tables(0).Rows(i).Item("username")
|
|
txtPassword.Text = ds.Tables(0).Rows(i).Item("password")
|
|
txtPassword2.Text = ds.Tables(0).Rows(i).Item("password")
|
|
cmbPlant.SelectedItem = ds.Tables(0).Rows(i).Item("plant")
|
|
cmbAccount.SelectedItem = ds.Tables(0).Rows(i).Item("account")
|
|
cmb_userType.SelectedItem = ds.Tables(0).Rows(i).Item("userType")
|
|
If ds.Tables(0).Rows(i).Item("active") = 1 Then
|
|
cbActive.Checked = True
|
|
Else
|
|
cbActive.Checked = False
|
|
End If
|
|
End If
|
|
End Sub
|
|
|
|
Public Sub moveLast()
|
|
If limit > -1 Then
|
|
i = limit
|
|
txtUsername.Text = ds.Tables(0).Rows(i).Item("username")
|
|
oldUserName = ds.Tables(0).Rows(i).Item("username")
|
|
txtPassword.Text = ds.Tables(0).Rows(i).Item("password")
|
|
txtPassword2.Text = ds.Tables(0).Rows(i).Item("password")
|
|
cmbPlant.SelectedItem = ds.Tables(0).Rows(i).Item("plant")
|
|
cmbAccount.SelectedItem = ds.Tables(0).Rows(i).Item("account")
|
|
cmb_userType.SelectedItem = ds.Tables(0).Rows(i).Item("userType")
|
|
If ds.Tables(0).Rows(i).Item("active") = 1 Then
|
|
cbActive.Checked = True
|
|
Else
|
|
cbActive.Checked = False
|
|
End If
|
|
Else
|
|
clear()
|
|
End If
|
|
End Sub
|
|
|
|
Public Sub add()
|
|
clear()
|
|
addMode = 1
|
|
editMode = 0
|
|
toAddMode()
|
|
End Sub
|
|
|
|
Public Sub edit()
|
|
oldUserName = txtUserName.Text
|
|
|
|
editMode = 1
|
|
addMode = 0
|
|
|
|
toEditMode()
|
|
End Sub
|
|
|
|
Public Sub save()
|
|
Dim user As New User(txtUsername.Text)
|
|
If addMode Or editMode Then
|
|
If txtPassword.Text = txtPassword2.Text Then
|
|
If addMode Then
|
|
user.add(txtUsername.Text, txtPassword.Text, cmbPlant.SelectedItem, cmbAccount.SelectedItem, cmb_userType.SelectedItem, cbActive.Checked)
|
|
ElseIf editMode Then
|
|
user.edit(txtUsername.Text, txtPassword.Text, cmbPlant.SelectedItem, cmbAccount.SelectedItem, cmb_userType.SelectedItem, cbActive.Checked)
|
|
End If
|
|
fillDataSet()
|
|
If ds.Tables(0).Rows.Count <> 0 Then
|
|
oldUserName = txtUsername.Text
|
|
trace(oldUserName)
|
|
End If
|
|
disableControls()
|
|
addMode = 0
|
|
editMode = 0
|
|
toNormalMode()
|
|
Else
|
|
MessageBox.Show("Passwords do not match", "User", MessageBoxButtons.OK, MessageBoxIcon.Warning)
|
|
End If
|
|
End If
|
|
End Sub
|
|
|
|
Public Sub delete()
|
|
If txtUserName.Text <> "" Then
|
|
If MessageBox.Show("Are you sure you want to delete?", "System User", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes And txtUserName.Text <> "" Then
|
|
Dim user As New User(txtUserName.Text)
|
|
user.delete(txtUserName.Text)
|
|
fillDataSet()
|
|
moveFirst()
|
|
End If
|
|
End If
|
|
End Sub
|
|
|
|
Public Sub revert()
|
|
If (editMode Or addMode) Then
|
|
disableControls()
|
|
clear()
|
|
If oldUserName <> "" Then
|
|
retrieve(oldUserName)
|
|
End If
|
|
|
|
toNormalMode()
|
|
End If
|
|
End Sub
|
|
|
|
Public Sub find()
|
|
txtUsername.Enabled = True
|
|
txtUserName.Text = String.Empty
|
|
txtPassword.Text = String.Empty
|
|
cmb_userType.SelectedIndex = 0
|
|
cbActive.Visible = False
|
|
btn_find.Visible = True
|
|
|
|
toSearchMode()
|
|
End Sub
|
|
|
|
Private Sub getplants()
|
|
Dim user As New User
|
|
Dim plantlist As New List(Of String)
|
|
Dim i As Integer = 0
|
|
|
|
cmbPlant.Items.Clear()
|
|
plantlist = user.getPlants
|
|
While i < plantlist.Count
|
|
cmbPlant.Items.Add(plantlist(i))
|
|
i = i + 1
|
|
End While
|
|
End Sub
|
|
|
|
Private Sub getAccounts()
|
|
Dim user As New User
|
|
Dim accountList As New List(Of String)
|
|
Dim i As Integer = 0
|
|
|
|
cmbAccount.Items.Clear()
|
|
accountList = user.getAccounts
|
|
While i < accountList.Count
|
|
cmbAccount.Items.Add(accountList(i))
|
|
i = i + 1
|
|
End While
|
|
End Sub
|
|
|
|
Private Sub retrieve(ByVal username As String)
|
|
fillDataSet()
|
|
trace(username)
|
|
txtUsername.Enabled = False
|
|
cbActive.Visible = True
|
|
btn_find.Visible = False
|
|
|
|
toNormalMode()
|
|
End Sub
|
|
|
|
Private Sub ts_new_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ts_new.Click
|
|
add()
|
|
End Sub
|
|
|
|
Private Sub ts_edit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ts_edit.Click
|
|
edit()
|
|
End Sub
|
|
|
|
Private Sub ts_delete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ts_delete.Click
|
|
delete()
|
|
End Sub
|
|
|
|
Private Sub ts_find_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ts_find.Click
|
|
find()
|
|
End Sub
|
|
|
|
Private Sub ts_save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ts_save.Click
|
|
save()
|
|
End Sub
|
|
|
|
Private Sub ts_revert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ts_revert.Click
|
|
revert()
|
|
End Sub
|
|
|
|
Private Sub btn_find_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_find.Click
|
|
oldUserName = txtUsername.Text
|
|
retrieve(oldUserName)
|
|
End Sub
|
|
|
|
Private Sub txtUsername_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtUsername.KeyUp
|
|
If e.KeyCode = Keys.Enter Then
|
|
oldUserName = txtUsername.Text
|
|
retrieve(oldUserName)
|
|
End If
|
|
End Sub
|
|
|
|
Private Sub ts_close_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
|
|
Me.Close()
|
|
End Sub
|
|
End Class |