Creating a previous list item, Next list item, Display the current record and get an button enable

Locked 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 On

    Option Strict On

    Imports System.Convert

    Public 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 form

    lisEmployees.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 = ""

    Else

    MsgBox(MsgStr)

    End If

    ' Write the next available Employee ID to the ID text box

    txtEmployeeID.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 = True

    MsgStr = "Please enter valid: " & ControlChars.CrLf

    ' Is Employee Type valid

    If Not (txtEmployeeType.Text = "Manager" Or txtEmployeeType.Text = "Staff") Then

    AllValid = False

    MsgStr = MsgStr & "Employee type ('Manager' OR 'Staff')" & ControlChars.CrLf

    End If

    ' Is Employee Name valid

    If txtEmployeeName.Text = "" Then

    AllValid = False

    MsgStr = MsgStr & "Employee Name can't be empty" & ControlChars.CrLf

    End If

    ' Is Employee Address valid

    If txtEmployeeAddress.Text = "" Then

    AllValid = False

    MsgStr = MsgStr & "Employee Address can't be empty" & ControlChars.CrLf

    End If

    ' Is Employee SSN valid

    If Not IsValidSSN(txtEmployeeSSN.Text) Then

    AllValid = False

    MsgStr = 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)

     

    Else

    MsgBox(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

    Try

    lisEmployees.Items.Remove(EmpCurrent.EmployeeID.ToString & EmpCurrent.EmployeeName.ToString)

    Catch ex As ArgumentOutOfRangeException

    MessageBox.Show("Employee Not Found")

    End Try

    ' Write Employee count to text box

    ClearFields()

    txtEmployeeID.Text = ""

    txtEmployeeType.Text = ""

    txtEmployeeName.Text = ""

    txtEmployeeAddress.Text = ""

    txtEmployeeSSN.Text = ""

    Else

    MsgBox("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 box

    txtEmployeeID.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.Click

    grpAdd.Enabled = True

    grpEmpData.Enabled = True

    btnAddNew.Enabled = False

    btnViewExisting.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 box

    txtEmployeeID.Text = (MaxEmployeeID + 1).ToString

    End Sub

    Private Sub btnViewExisting_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnViewExisting.Click

    grpEditView.Enabled = True

    btnAddNew.Enabled = False

    btnViewExisting.Enabled = False

    btnGetEmployee.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.Click

    grpAdd.Enabled = False

    grpEmpData.Enabled = False

    btnAddNew.Enabled = True

    btnViewExisting.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.Click

    grpEditView.Enabled = False

    grpEmpData.Enabled = False

    btnAddNew.Enabled = True

    btnViewExisting.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 box

    CurrentIndex = lisEmployees.SelectedIndex

    If CurrentIndex > -1 Then

    Try

    lisEmployees.Items.Add(EmpCurrent.EmployeeID)

    Catch ex As Exception

    MessageBox.Show("Employee is Found")

    End Try

    Else

    End If

    btnPrevious.Enabled = True

    btnNext.Enabled = True

    btnUpdate.Enabled = True

    btnDeleteCurrent.Enabled = True

    grpEmpData.Enabled = True

    Catch ex As Exception

    MsgBox("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 10

    CurrentChar = 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 Sub

    End Class

All Replies

  • Monday, October 30, 2006 9:11 PM
    Moderator
     
     

    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,