3 Replies - 1514 Views - Last Post: 26 April 2012 - 06:27 AM Rate Topic: -----

#1 John11  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 49
  • Joined: 17-March 12

Do {Code} When Selecting Multiple Values From ListBox

Posted 26 April 2012 - 04:38 AM

Hello, I have 4 ListBoxes Named lstTickets], lstRegular, lstVip, lstBackstage. The lstTickets contains Numbers from 1 to 5000. I also have a button named btnSell which is selling the ticket, that means if the number is between 1 and 2700, I will add it to lstReuglar and remove it from lstTickets, if the number is between 2700 and 450, I will add it to lstVip and remove it from lstTickets and if the number is between 4500 and 5000, I will add it to lstBackstage and remove it from lstTickets.

I have this code :
Private Sub btnSell_Click(sender As System.Object, e As System.EventArgs) Handles btnSell.Click
        If CType(lstTickets.SelectedItem, String) = "" Then
            GoTo Textboxes
        End If
        If lstTickets.SelectedItem <= 2700 Then
            lstRegular.Items.Add(lstTickets.SelectedItem)
            lstTickets.Items.Remove(lstTickets.SelectedItem)
            txtTicketNumber.Focus()
            GoTo Calculations
        ElseIf lstTickets.SelectedItem > 2700 And lstTickets.SelectedItem <= 4500 Then
            lstVip.Items.Add(lstTickets.SelectedItem)
            lstTickets.Items.Remove(lstTickets.SelectedItem)
            txtTicketNumber.Focus()
            GoTo Calculations
        ElseIf lstTickets.SelectedItem > 4500 And lstTickets.SelectedItem <= 5000 Then
            lstBackstage.Items.Add(lstTickets.SelectedItem)
            lstTickets.Items.Remove(lstTickets.SelectedItem)
            txtTicketNumber.Focus()
            GoTo Calculations
        End If

Textboxes:
        If txtTicketNumber.Text = "" Then
            txtTicketNumber.Focus()
            Exit Sub
        End If
        If CType(txtTicketNumber.Text, Integer) < 1 Or CType(txtTicketNumber.Text, Integer) > 5000 Or txtTicketNumber.Text = "" Then
            MsgBox("Number Must Be Between 1 and 5000", MsgBoxStyle.Information)
            txtTicketNumber.Text = ""
            txtTicketNumber.Focus()
            Exit Sub
        End If
        If lstBanned.Items.Contains(CType(txtTicketNumber.Text, Integer)) Then
            MsgBox("'" & txtTicketNumber.Text & "' is already in the BANNED list", MsgBoxStyle.Critical)
            txtTicketNumber.Text = ""
            txtTicketNumber.Focus()
            Exit Sub
        End If
        If CType(txtTicketNumber.Text, Integer) <= 2700 Then
            If lstRegular.Items.Contains(CType(txtTicketNumber.Text, Integer)) Then
                MsgBox("'" & txtTicketNumber.Text & "' is already in the REGULAR list", MsgBoxStyle.Exclamation)
                txtTicketNumber.Text = ""
                txtTicketNumber.Focus()
                Exit Sub
            End If
            lstRegular.Items.Add(CType(txtTicketNumber.Text, Integer))
            lstTickets.Items.Remove(CType(txtTicketNumber.Text, Integer))
            txtTicketNumber.Focus()
        End If
        If CType(txtTicketNumber.Text, Integer) > 2700 And CType(txtTicketNumber.Text, Integer) <= 4500 Then
            If lstVip.Items.Contains(CType(txtTicketNumber.Text, Integer)) Then
                MsgBox("'" & txtTicketNumber.Text & "' is already in the VIP list", MsgBoxStyle.Exclamation)
                txtTicketNumber.Text = ""
                txtTicketNumber.Focus()
                Exit Sub
            End If
            lstVip.Items.Add(CType(txtTicketNumber.Text, Integer))
            lstTickets.Items.Remove(CType(txtTicketNumber.Text, Integer))
            txtTicketNumber.Focus()
        End If
        If CType(txtTicketNumber.Text, Integer) > 4500 And CType(txtTicketNumber.Text, Integer) <= 5000 Then
            If lstBackstage.Items.Contains(CType(txtTicketNumber.Text, Integer)) Then
                MsgBox("'" & txtTicketNumber.Text & "' is already in the BACKSTAGE list", MsgBoxStyle.Exclamation)
                txtTicketNumber.Text = ""
                txtTicketNumber.Focus()
                Exit Sub
            End If
            lstBackstage.Items.Add(CType(txtTicketNumber.Text, Integer))
            lstTickets.Items.Remove(CType(txtTicketNumber.Text, Integer))
            txtTicketNumber.Focus()
        End If

Calculations:
        Dim TotalRegular, TotalVip, TotalBackstage As Integer

        TotalRegular = lstRegular.Items.Count
        TotalVip = lstVip.Items.Count
        TotalBackstage = lstBackstage.Items.Count

        txtTicketNumber.Text = ""
        lblTicket.Text = lstTickets.Items.Count
        lblRegular.Text = lstRegular.Items.Count
        lblVip.Text = lstVip.Items.Count
        lblBackstage.Text = lstBackstage.Items.Count

        lblTotalRegular.Text = (TotalRegular * 52500)
        lblTotalRegular.Text = FormatCurrency(lblTotalRegular.Text)
        lblTotalVip.Text = (TotalVip * 90000)
        lblTotalVip.Text = FormatCurrency(lblTotalVip.Text)
        lblTotalBackstage.Text = (TotalBackstage * 150000)
        lblTotalBackstage.Text = FormatCurrency(lblTotalBackstage.Text)
        lblTotalAllTickets.Text = (TotalRegular * 52500) + (TotalVip * 90000) + (TotalBackstage * 150000)
        lblTotalAllTickets.Text = FormatCurrency(lblTotalAllTickets.Text)
        txtTicketNumber.Focus()
    End Sub



This code shows that I'm selecting a number from a textbox txtTicketNumber or from the lstTickets and then I'm selling it. But the problem is how do I modify this code to work if I want to select multiple values from the listbox lstTickets and sell them so that it won't take much time to sell the tickets.

Thank you.

Is This A Good Question/Topic? 0
  • +

Replies To: Do {Code} When Selecting Multiple Values From ListBox

#2 m_wylie85  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 96
  • View blog
  • Posts: 899
  • Joined: 15-October 10

Re: Do {Code} When Selecting Multiple Values From ListBox

Posted 26 April 2012 - 05:42 AM

Well if i understand you correctly you want to select mutable item from lstTickets then run your various checks about wither there vip and so on. So when you have selected the items you would need to run a loop on EACH of the items that have be selected. A bit of code to help you get started.

     For Each Item In lstTickets.SelectedItems
            'Your checks for vip and so on
        Next


Was This Post Helpful? 0
  • +
  • -

#3 John11  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 49
  • Joined: 17-March 12

Re: Do {Code} When Selecting Multiple Values From ListBox

Posted 26 April 2012 - 06:02 AM

I came up with a code but I'm getting an error:
Index was outside the bounds of the array.

The code is:
Dim myArray(lstTickets.SelectedItems.Count) As Integer

        For i = 0 To lstTickets.SelectedItems.Count - 1
            myArray(i) = CType(lstTickets.SelectedItems(i), Integer)
            If myArray(i) <= 2700 Then
                lstRegular.Items.Add(myArray(i))
                lstTickets.Items.Remove(myArray(i))
                txtTicketNumber.Focus()
            ElseIf myArray(i) > 2700 And myArray(i) <= 4500 Then
                lstVip.Items.Add(myArray(i))
                lstTickets.Items.Remove(myArray(i))
                txtTicketNumber.Focus()
            ElseIf myArray(i) > 4500 And myArray(i) <= 5000 Then
                lstBackstage.Items.Add(myArray(i))
                lstTickets.Items.Remove(myArray(i))
                txtTicketNumber.Focus()
            End If
        Next



What am I doing wrong here?
Was This Post Helpful? 0
  • +
  • -

#4 John11  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 49
  • Joined: 17-March 12

Re: Do {Code} When Selecting Multiple Values From ListBox

Posted 26 April 2012 - 06:27 AM

I solved the problem by just modifying this code:
Dim myArray(lstTickets.SelectedItems.Count) As Integer

        [b]For i = lstTickets.SelectedItems.Count - 1 To 0 Step -1[/b]
            myArray(i) = CType(lstTickets.SelectedItems(i), Integer)
            If myArray(i) <= 2700 Then
                lstRegular.Items.Add(myArray(i))
                lstTickets.Items.Remove(myArray(i))
                txtTicketNumber.Focus()
            ElseIf myArray(i) > 2700 And myArray(i) <= 4500 Then
                lstVip.Items.Add(myArray(i))
                lstTickets.Items.Remove(myArray(i))
                txtTicketNumber.Focus()
            ElseIf myArray(i) > 4500 And myArray(i) <= 5000 Then
                lstBackstage.Items.Add(myArray(i))
                lstTickets.Items.Remove(myArray(i))
                txtTicketNumber.Focus()
            End If
        Next


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1