Creating a previous list item, Next list item, Display the current record and get an button enable
-
Monday, October 23, 2006 2:29 AM
Hello,
im new to visual basic 2005. im having problems Creating a previous list item, Next list item, Display the current record and get an button enable.
can anyone out there help
Here is the code
' Chapter.08 - Solution to Hands-on Project 1
'
' Modified from:
'
' Programming With Visual Basic .NET
' An Object-Oriented Approach - Second Edition
Option
Explicit OnOption
Strict OnImports
System.ConvertPublic
Class frmMain ' Create the generic EmployeeList Private EmployeeList As New List(Of Employee) ' Initalize the index to indicate that there are no employees ' in the list. Dim CurrentIndex As Integer = -1 ' Keep track of max employee ID Dim MaxEmployeeID As Integer = 0 ' String to store messages Dim MsgStr As String ' Display the record count when the form is initialized. Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load End Sub ' Save a new employee to the list. Private Sub btnSaveNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSaveNew.Click ' Instantiate employee object Dim EmpCurrent As New Employee
' If all are valid save else display error If IsAllValid() Then ' Increment the Max Employee indicating the latest number added.MaxEmployeeID = MaxEmployeeID + 1
' Assign an EmployeeID automatically.EmpCurrent.EmployeeID = MaxEmployeeID
' Populate the remaining Employee object data.EmpCurrent.EmployeeType = txtEmployeeType.Text
EmpCurrent.EmployeeName = txtEmployeeName.Text
EmpCurrent.EmployeeAddress = txtEmployeeAddress.Text
EmpCurrent.EmployeeSSN = txtEmployeeSSN.Text
' Add the Employee to the generic list.EmployeeList.Add(EmpCurrent)
' Display the employee ID to text box.txtEmployeeID.Text = EmpCurrent.EmployeeID.ToString
' Add an employee to the listbox on formlisEmployees.Items.Add(EmpCurrent.EmployeeID.ToString & EmpCurrent.EmployeeName.ToString)
' Clear the contents of the text boxes.ClearFields()
txtEmployeeID.Text =
""txtEmployeeType.Text =
""txtEmployeeName.Text =
""txtEmployeeAddress.Text =
""txtEmployeeSSN.Text =
"" ElseMsgBox(MsgStr)
End If ' Write the next available Employee ID to the ID text boxtxtEmployeeID.Text = (MaxEmployeeID + 1).ToString
' Display the number of employees in the list to the count text box.txtRecordCount.Text = (MaxEmployeeID + CurrentIndex).ToString
End Sub Private Function IsAllValid() As Boolean ' Assume all valid input Dim AllValid As Boolean = TrueMsgStr =
"Please enter valid: " & ControlChars.CrLf ' Is Employee Type valid If Not (txtEmployeeType.Text = "Manager" Or txtEmployeeType.Text = "Staff") ThenAllValid =
FalseMsgStr = MsgStr &
"Employee type ('Manager' OR 'Staff')" & ControlChars.CrLf End If ' Is Employee Name valid If txtEmployeeName.Text = "" ThenAllValid =
FalseMsgStr = MsgStr &
"Employee Name can't be empty" & ControlChars.CrLf End If ' Is Employee Address valid If txtEmployeeAddress.Text = "" ThenAllValid =
FalseMsgStr = MsgStr &
"Employee Address can't be empty" & ControlChars.CrLf End If ' Is Employee SSN valid If Not IsValidSSN(txtEmployeeSSN.Text) ThenAllValid =
FalseMsgStr = MsgStr &
"Employee SSN ('###-##-####')" & ControlChars.CrLf End If ' Return result Return AllValid End Function
' Save the changes contained in the text boxes to the list. Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click Dim EmpCurrent As New Employee ' If all are valid save else display error If IsAllValid() Then ' Increment the Max Employee indicating the latest number added.MaxEmployeeID = MaxEmployeeID + 1
' Assign an EmployeeID automatically.EmpCurrent.EmployeeID = MaxEmployeeID
' Populate the remaining Employee object data.EmpCurrent.EmployeeType = txtEmployeeType.Text
EmpCurrent.EmployeeName = txtEmployeeName.Text
EmpCurrent.EmployeeAddress = txtEmployeeAddress.Text
EmpCurrent.EmployeeSSN = txtEmployeeSSN.Text
' Add the Employee to the generic list.EmployeeList.Add(EmpCurrent)
ElseMsgBox(MsgStr)
End If End Sub ' Delete the current list item. Private Sub btnDeleteCurrent_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDeleteCurrent.Click Dim EmpCurrent As Employee If CurrentIndex > -1 Then ' Remove Employee from list TrylisEmployees.Items.Remove(EmpCurrent.EmployeeID.ToString & EmpCurrent.EmployeeName.ToString)
Catch ex As ArgumentOutOfRangeExceptionMessageBox.Show(
"Employee Not Found") End Try ' Write Employee count to text boxClearFields()
txtEmployeeID.Text =
""txtEmployeeType.Text =
""txtEmployeeName.Text =
""txtEmployeeAddress.Text =
""txtEmployeeSSN.Text =
"" ElseMsgBox(
"There are no records to delete.") End If End Sub ' Display the previous list item. Private Sub btnPrevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrevious.Click End Sub ' Display the next list item. Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click End Sub ' Exit the application. Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click Me.Close() End Sub Private Sub btnClearForm_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClearForm.Click ' Clear the contents of the text boxes.ClearFields()
txtEmployeeID.Text =
""txtEmployeeType.Text =
""txtEmployeeName.Text =
""txtEmployeeAddress.Text =
""txtEmployeeSSN.Text =
"" ' Write the next available Employee ID to the ID text boxtxtEmployeeID.Text = (MaxEmployeeID + 1).ToString
End Sub ' Display the current record. Private Sub DisplayCurrentRecord() End Sub Private Sub ClearFields() ' Clear the contents of the text boxes.txtEmployeeID.Text =
""txtEmployeeType.Text =
""txtEmployeeName.Text =
""txtEmployeeAddress.Text =
""txtEmployeeSSN.Text =
"" End Sub Private Sub btnAddNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddNew.ClickgrpAdd.Enabled =
TruegrpEmpData.Enabled =
TruebtnAddNew.Enabled =
FalsebtnViewExisting.Enabled =
False ' Clear the contents of the text boxes.ClearFields()
txtEmployeeID.Text =
""txtEmployeeType.Text =
""txtEmployeeName.Text =
""txtEmployeeAddress.Text =
""txtEmployeeSSN.Text =
"" ' Write the next available Employee ID to the ID text boxtxtEmployeeID.Text = (MaxEmployeeID + 1).ToString
End Sub Private Sub btnViewExisting_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnViewExisting.ClickgrpEditView.Enabled =
TruebtnAddNew.Enabled =
FalsebtnViewExisting.Enabled =
FalsebtnGetEmployee.Enabled =
False ' Clear the contents of the text boxes.ClearFields()
txtEmployeeID.Text =
""txtEmployeeType.Text =
""txtEmployeeName.Text =
""txtEmployeeAddress.Text =
""txtEmployeeSSN.Text =
"" End Sub Private Sub btnExitAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExitAdd.ClickgrpAdd.Enabled =
FalsegrpEmpData.Enabled =
FalsebtnAddNew.Enabled =
TruebtnViewExisting.Enabled =
True ' Clear the contents of the text boxes.ClearFields()
txtEmployeeID.Text =
""txtEmployeeType.Text =
""txtEmployeeName.Text =
""txtEmployeeAddress.Text =
""txtEmployeeSSN.Text =
"" End Sub Private Sub btnExitView_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExitView.ClickgrpEditView.Enabled =
FalsegrpEmpData.Enabled =
FalsebtnAddNew.Enabled =
TruebtnViewExisting.Enabled =
True ' Clear the contents of the text boxes.ClearFields()
txtEmployeeID.Text =
""txtEmployeeType.Text =
""txtEmployeeName.Text =
""txtEmployeeAddress.Text =
""txtEmployeeSSN.Text =
"" End Sub Private Sub btnGetEmployee_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetEmployee.Click Dim EmpCurrent As New Employee Try ' Get employee selected in list boxCurrentIndex = lisEmployees.SelectedIndex
If CurrentIndex > -1 Then TrylisEmployees.Items.Add(EmpCurrent.EmployeeID)
Catch ex As ExceptionMessageBox.Show(
"Employee is Found") End Try Else End IfbtnPrevious.Enabled =
TruebtnNext.Enabled =
TruebtnUpdate.Enabled =
TruebtnDeleteCurrent.Enabled =
TruegrpEmpData.Enabled =
True Catch ex As ExceptionMsgBox(
"No Employees exist") End Try End Sub ' Function to check for a valid SSN Private Function IsValidSSN(ByVal arg As String) As Boolean Dim Count As Integer Dim CurrentChar As Integer If arg.Length = 11 Then ' Check all eleven chars in string For Count = 0 To 10CurrentChar = Asc(arg.Substring(Count, 1))
Select Case Count ' Check for numeric ascii values Case 0, 1, 2, 4, 5, 7, 8, 9, 10 If Not (CurrentChar >= 48 And CurrentChar <= 57) Then Return False End If ' Check for a dash ascii value Case 3, 6 If Not (CurrentChar = 45) Then Return False End If End Select Next Else Return False End If Return True End Function Private Sub lisEmployees_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lisEmployees.Click ' Check to see if btnGetEmployee should be enabled End SubEnd
Class
All Replies
-
Monday, October 30, 2006 9:11 PMModerator
Hi,
Would you please list the problems / exception messages you're experiencing? It would be great if you can share your whole project since the form code above is not complete to easily reproducing your problems.
Best regards,

