Results 1 to 4 of 4

Thread: Adding items from one listbox to another depending on what the listbox item contains

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2013
    Posts
    5

    Adding items from one listbox to another depending on what the listbox item contains

    hi, i have 3 listboxes. one contains all of the items and i want to add specific items to another listbox depending on what the item contains

    so for example
    in listbox one it contains all of the seats for a concert
    i want the seats ranging from A to C to be added to listbox two and so on

    im new to vb, please help

    thanks

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,376

    Re: Adding items from one listbox to another depending on what the listbox item conta

    It would look something like this:
    Code:
    Option Strict On
    Option Explicit On
    Public Class Form1
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            'Load items in listbox1
            ListBox1.Items.AddRange({"A", "B", "C", "D", "E", "F", "G", "H"})
    
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim wantedseats() As String = {"A", "B", "C"}
            For Each itm As String In ListBox1.Items
                If wantedseats.Contains(itm) Then
                    ListBox2.Items.Add(itm)
                End If
            Next
        End Sub
    
    End Class
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  3. #3

    Thread Starter
    New Member
    Join Date
    Mar 2013
    Posts
    5

    Re: Adding items from one listbox to another depending on what the listbox item conta

    Sorry i wasnt specific enough. the listbox1 contains items that have been selected from another form and saved to a text file. then the listbox1 retreives the seatnumbers ranging from A1 to A15 all the way down to Z1 to Z15 and they have different prices so i want to put each price range in a different listbox to find out the total income made by each seat. so for listbox2 i want all seat the contain A B or C in the item in the listbox to move to listbox2. so if A12 is in listbox1 how do it get it to go to Listbox 2

    sorry for not being specific enough and thanks for the quick reply

  4. #4
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,376

    Re: Adding items from one listbox to another depending on what the listbox item conta

    You could create a Dictionary(Of String, Double), that away you can hold the seat name and have a price associated with it:
    Code:
    Option Strict On
    Option Explicit On
    Public Class Form1
        Private dic As New Dictionary(Of String, Double)
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            'Adding A1-A3 and B1-B3 in a dictionary
            dic.Add("A1", 10.5)
            dic.Add("A2", 10.5)
            dic.Add("A3", 10.5)
            dic.Add("B1", 12.5)
            dic.Add("B2", 12.5)
            dic.Add("B3", 12.5)
    
            'Loading in the seat names into listbox1
            For Each val As String In dic.Keys
                ListBox1.Items.Add(val)
            Next
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            'Each seat that's selected, bring the price into listbox2
            'Using the .ToString(currency)
            For Each Str As String In ListBox1.SelectedItems
                If dic.ContainsKey(Str) Then
                    ListBox2.Items.Add(dic(Str).ToString("c"))
                End If
            Next
        End Sub
    End Class
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

Tags for this Thread

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