CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Apr 2014
    Posts
    1

    MultiListbox selection

    Hello, I am having a little trouble with my assignment at the moment, at the remove button its suppose to delete the item I have selected and that works fine however I have 4 list boxes each one named First Name, Last Name, Age, and User Id. So as you can see they are all linked together so much question is how do I highlight all the items that are parallel to each other and then deleted?

    Code:
     
        Private Sub BtnAdd_Click(sender As Object, e As EventArgs) Handles BtnAdd.Click
            'Validate First Name for integer and decimal
            If IsNumeric(TxtFirstName.Text) Then
                MessageBox.Show("Please Enter First Name in Text Form")
                TxtFirstName.Text = String.Empty
                TxtFirstName.Focus()
                Return
            End If
            'Validate Last Name for integer and decimal
            If IsNumeric(TxtLastName.Text) Then
                MessageBox.Show("Please Enter Last Name in Text Form")
                TxtLastName.Text = String.Empty
                TxtLastName.Focus()
                Return
            End If
    
            'No input
            If TxtFirstName.Text = String.Empty Then
                MessageBox.Show("Please Enter First Name")
                TxtFirstName.Focus()
                Return
            End If
            If TxtLastName.Text = String.Empty Then
                MessageBox.Show("Please Enter Last Name")
                TxtLastName.Focus()
                Return
            End If
            If TxtAge.Text = String.Empty Then
                MessageBox.Show("Please Enter Age")
                TxtAge.Focus()
                Return
            End If
    
            'Validate Age for non integer and decimal
            Dim AgeInteger As Integer
            If Not Integer.TryParse(TxtAge.Text, AgeInteger) Then
                MessageBox.Show("Please enter Age in Integer form")
                TxtAge.Text = String.Empty
                TxtAge.Focus()
                Return
            End If
            'Validate Age
            If TxtAge.Text < 15 Or TxtAge.Text > 90 Then
                MessageBox.Show("Please enter Valid ages of 15 through 90")
                TxtAge.Text = String.Empty
                TxtAge.Focus()
                Return
            End If
    
            ' Add inputs to list boxes
            FirstNameListBox.Items.Add(TxtFirstName.Text)
            LastNameListBox.Items.Add(TxtLastName.Text)
            AgeListBox.Items.Add(TxtAge.Text)
    
            'Calculate User ID and add lowercase the UserID
            Dim counter As Integer = 1
            Do
                If UserIdListBox.FindStringExact(String.Format("{0}{1}{2:000}", TxtFirstName.Text, TxtLastName.Text.First, counter)) <> -1 Then
                    counter += 1
                Else
                    Exit Do
                End If
            Loop
            UserIdListBox.Items.Add(String.Format("{0}{1}{2:000}", TxtFirstName.Text.ToLower(), TxtLastName.Text.First.ToString().ToLower(), counter))
    
            'Erase Input
            TxtFirstName.Text = String.Empty
            TxtLastName.Text = String.Empty
            TxtAge.Text = String.Empty
    
            'Set Focus to First Name
            TxtFirstName.Focus()
    
            'Enable Clear Button
            BtnClear.Enabled = True
    
            'Enable Remove Button
            BtnRemove.Enabled = True
    
        End Sub
    
    
        Private Sub BtnRemove_Click(sender As Object, e As EventArgs) Handles BtnRemove.Click
            'Remove Selected User
           
    
            'Send Focus to First Name
            TxtFirstName.Focus()
        End Sub
    
    
        Private Sub BtnClear_Click(sender As Object, e As EventArgs) Handles BtnClear.Click
            'Erage All Listboxes
            FirstNameListBox.Items.Clear()
            LastNameListBox.Items.Clear()
            AgeListBox.Items.Clear()
            UserIdListBox.Items.Clear()
    
            'Disable Remove Button
            BtnRemove.Enabled = False
            'Disable Clear Button
            BtnClear.Enabled = False
            'Send Focus to First Name
            TxtFirstName.Focus()
        End Sub
    
    
        Private Sub BtnExit_Click(sender As Object, e As EventArgs) Handles BtnExit.Click
            'Display Message Box of Average Age of Users
            Dim dblTotal As Double = 0
            For Each str As String In AgeListBox.Items
                dblTotal += Double.Parse(str)
            Next
            dblTotal = dblTotal / AgeListBox.Items.Count
            MessageBox.Show("User Average Age:  " & dblTotal)
    
            'Terminate Program
            Close()
        End Sub
    
    End Class

  2. #2
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: MultiListbox selection

    You would need to add some code in the list click event to select/unselect the items in the other lists based on the list index of the item clicked on in the active list. The the code to remove the item would need to be ran against all 4 lists
    Always use [code][/code] tags when posting code.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured