Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27356

ListView selection

$
0
0
Another noob question. Learning arrays. Got an assignment
Quote:

Originally Posted by text book
Create an employee directory application that shows employee names in a list box on the main form. When the user selects a name from the list box, the application should display the employee's ID number, department name, and telephone number on a separate form.

The application should store all employee ID numbers, department names, and telephone numbers in separate arrays. The arrays and list lib should have a parallel relationship.

Instead of using listbox I choose to use ListView. My book doesn't cover ListView at all, and my understanding is a bit sketchy of them. The MSDN has information I don't understand (yet), so it's more trial and error from what I could find on Youtube and various Google Searches. That said, I do have a working application. It just seems like there may be a better way, but just couldn't really figure out the control or syntax.

Code:

Public Class frmMainForm
    'delcare variables
    Dim intMAX_SUBSCRIPT As Integer = 0
    Dim strLastName() As String = {"Downing", "Stuart", "Dodd", "Butler", "Tayler", "Hawkins", "Davis", "Mason", "Owens", "Walker"}
    Dim strFirstName() As String = {"Richard", "Nathan", "Vinette", "Wayne", "Derrick", "Demetrius", "Henry", "Dione", "Robert", "Morgan"}
    Dim strIDnumber() As String = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10"}
    Dim strDepartment() As String = {"Executive", "Automotive", "Sales", "Sales", "Deliveries", "Human Resources", "Field Sales", "Customer Service", "Network Administration", "Installations", "Customer Service"}
    Dim strTelephone() As String = {"707 555 1200", "510 550 1544", "415 380 7489", "301 525 3300", "420 525 1957", "707 718 8561", "510 657 7725", "415 352 1212", "510 854 7842", "707 747 4949"}

    [code removed]

    Private Sub frmMainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
       

        'determine subscript size of arrays based on elements in each array
        If intMAX_SUBSCRIPT < strLastName.Length Then
            intMAX_SUBSCRIPT = strLastName.Length
        ElseIf intMAX_SUBSCRIPT < strFirstName.Length Then
            intMAX_SUBSCRIPT = strFirstName.Length
        ElseIf intMAX_SUBSCRIPT < strIDnumber.Length Then
            intMAX_SUBSCRIPT = strIDnumber.Length
        ElseIf intMAX_SUBSCRIPT < strDepartment.Length Then
            intMAX_SUBSCRIPT = strDepartment.Length
        ElseIf intMAX_SUBSCRIPT < strTelephone.Length Then
            intMAX_SUBSCRIPT = strTelephone.Length
        End If

        'fill ListView with employees' name
        For intCounter = 0 To (intMAX_SUBSCRIPT - 1)
            lsvEmployees.Items.Add(strLastName(intCounter)).SubItems.Add(strFirstName(intCounter))
        Next

    End Sub

    Private Sub btnDisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplay.Click
        Dim frmDisplayForm As New frmDisplayForm
        Dim blnSelection As Boolean = True

        If lsvEmployees.Items(0).Selected = True Then
            frmDisplayForm.Text = lsvEmployees.Items(0).SubItems(1).Text & " " & lsvEmployees.SelectedItems(0).Text & " Information"
            frmDisplayForm.lblID.Text = strIDnumber(0).ToString
            frmDisplayForm.lblDepartment.Text = strDepartment(0)
            frmDisplayForm.lblTelelphone.Text = strTelephone(0)
        ElseIf lsvEmployees.Items(1).Selected = True Then
            frmDisplayForm.Text = lsvEmployees.Items(1).SubItems(1).Text & " " & lsvEmployees.SelectedItems(0).Text & " Information"
            frmDisplayForm.lblID.Text = strIDnumber(1).ToString
            frmDisplayForm.lblDepartment.Text = strDepartment(1)
            frmDisplayForm.lblTelelphone.Text = strTelephone(1)
        [code removed]
        ElseIf lsvEmployees.Items(9).Selected = True Then
            frmDisplayForm.Text = lsvEmployees.Items(9).SubItems(1).Text & " " & lsvEmployees.SelectedItems(0).Text & " Information"
            frmDisplayForm.lblID.Text = strIDnumber(9).ToString
            frmDisplayForm.lblDepartment.Text = strDepartment(9)
            frmDisplayForm.lblTelelphone.Text = strTelephone(9)

            'error message for no employee selected and sets boolean selct
        Else
            MessageBox.Show("Must select an employee", "Employee Selection Error")
            blnSelection = False
        End If

        If blnSelection = True Then
            frmDisplayForm.ShowDialog()
        End If

        'clears selected employees from memory for subsequent selections and gives error if no selection made
        lsvEmployees.SelectedItems.Clear()
    End Sub
[code removed]
    End Sub
End Class

I tried to make application kind of robust. I tried to create code to set the MAX_SUBSCRIPT to the largest array. I used that to fill and create the ListView with the employee names.

I just don't like the code I used for employee selection. My thinking was more along the lines
Code:

Dim intSelection As Integer 'gives which employee employee selected

intSelection = which employee selected based on order in ListView

frmDisplayForm.Text = lsvEmployees.Items(intSelection).SubItems(1).Text & " " & lsvEmployees.SelectedItems(0).Text & " Information"
            frmDisplayForm.lblID.Text = strIDnumber(intSelection).ToString
            frmDisplayForm.lblDepartment.Text = strDepartment(intSelection)
            frmDisplayForm.lblTelelphone.Text = strTelephone(intSelection)

I just couldn't figure out to create a way to give the Index selection number. I could have used SelectedItems for the Last Name, but didn't know if there was a similar code for a subitem. I still would have needed the selection number for the rest of the information for the display form. I know it may not be possible what I was thinking.

Thanks for any help.

Viewing all articles
Browse latest Browse all 27356

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>