Results 1 to 16 of 16

Thread: Pulling information from listbox

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2014
    Posts
    10

    Pulling information from listbox

    Can I get some generic code on how to pull information from a multiline listbox and display it in another listbox as one entry. (example) I choose 3 options from the first listbox and they display as so "Example0, Example1, Example2, Example3" in the other listbox

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,350

    Re: Pulling information from listbox

    Like any problem, break this one down into smaller parts. Getting multiple items from a ListBox is the same regardless of what you intend to do with them. Adding one item to a ListBox is the same regardless of where it comes from. The only thing that should be a mystery at all is the actual combination of three Strings into one, although that should really be all that mysterious either. What I would recommend is using the String.Join method, which will join multiple Strings into one with a specific delimiter between each pair.

  3. #3

    Thread Starter
    New Member
    Join Date
    Dec 2014
    Posts
    10

    Re: Pulling information from listbox

    I'm very new to vb so let me give u an example of my problem.

    Code:
    Private Sub btnAdd_Click(sender As System.Object, e As System.EventArgs) Handles btnAdd.Click
            lbOrder.Items.Add(lbToppings.selecteditems)
    Currently this only displays "(collection)" in the Order listbox

    I'm not 100% sure lbtoppings.selecteditems should even be used

    I feel like this is very basic knowledge and I somehow missed it in class and now I'm struggling hard.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,350

    Re: Pulling information from listbox

    You didn't bother to digest what I already posted. I said that you should break it down into three parts:

    1. Get the items from the first ListBox.
    2. Combine the items.
    3. Add the new item to the second ListBox.

    You've already got parts 1 and 3 in that code you posted but you haven't bothered to even try part 2. I specifically told you what method to use to achieve part 2 and you made no effort to use it. You are new to VB and it's quite reasonable that you wouldn't know about the String.Join method. I told you about it so you now know about it. You're not new to using a computer or the internet so there's no reason that you can't now look for information on using the String.Join method that I just told you about. You should assume that there will be explanations and examples available on the web and make an effort to find them. If you can't find what you need or can't understand what you find then it's quite reasonable to ask for further guidance but there's no good reason not to bother trying.

  5. #5

    Thread Starter
    New Member
    Join Date
    Dec 2014
    Posts
    10

    Re: Pulling information from listbox

    I have guidelines I have to follow, this program is for a final and we didn't learn anything about String.Join so that isn't within the guidelines. I did look for it and I didn't understand what I came across so I moved on. However I almost have it figured out but this does separate lines but I'm looking to join it all as one separated by comma's.
    Code:
    Dim i As Integer
    
            For i = 0 To lbToppings.SelectedItems.Count - 1
                lbOrder.Items.Add(lbToppings.Items.Item(i))
            Next i
    
        End Sub
    Sorry I just get a little frustrated and I have a hard time explaining exactly what my problem is.
    Last edited by JoshTon; Dec 14th, 2014 at 11:48 PM.

  6. #6

    Thread Starter
    New Member
    Join Date
    Dec 2014
    Posts
    10

    Re: Pulling information from listbox

    Still stuck on how to pull and concatenate listbox entries into another listbox any help from anybody?

    listbox1.items.add(listbox2.selecteditems) it just displays (collection) for me and I can't find anything on google (or i just can't word it right)

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,350

    Re: Pulling information from listbox

    Quote Originally Posted by JoshTon View Post
    Still stuck on how to pull and concatenate listbox entries into another listbox any help from anybody?

    listbox1.items.add(listbox2.selecteditems) it just displays (collection) for me and I can't find anything on google (or i just can't word it right)
    That's because, again, you have ignored what I have already told you. The issue has nothing to do with ListBoxes. You already know how to pull data out of a ListBox and how to put data into a ListBox. Your one and only issue is how to combine multiple Strings into one String. The fact that those Strings come from one ListBox and the result is going to another ListBox is irrelevant to how they are combined. Have you looked for information on how to combine multiple Strings? I'll wager that you actually already know how to combine two Strings as that's one of the most elementary actions you can perform. Just as a long walk consists of taking lots of single steps, so combining multiple Strings can be done simply by combining two Strings multiple times.

  8. #8

    Thread Starter
    New Member
    Join Date
    Dec 2014
    Posts
    10

    Re: Pulling information from listbox

    Quote Originally Posted by jmcilhinney View Post
    That's because, again, you have ignored what I have already told you. The issue has nothing to do with ListBoxes. You already know how to pull data out of a ListBox and how to put data into a ListBox. Your one and only issue is how to combine multiple Strings into one String. The fact that those Strings come from one ListBox and the result is going to another ListBox is irrelevant to how they are combined. Have you looked for information on how to combine multiple Strings? I'll wager that you actually already know how to combine two Strings as that's one of the most elementary actions you can perform. Just as a long walk consists of taking lots of single steps, so combining multiple Strings can be done simply by combining two Strings multiple times.
    Okay I will research combining multiple strings into a single and thank you for your patience and help.

    So I figured out how to add them to the other listbox but they are different entries now I can't seem to figure out how to concatenate them into a single entry
    Last edited by JoshTon; Dec 15th, 2014 at 01:55 AM.

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,350

    Re: Pulling information from listbox

    Quote Originally Posted by JoshTon View Post
    Okay I will research combining multiple strings into a single and thank you for your patience and help.

    So I figured out how to add them to the other listbox but they are different entries now I can't seem to figure out how to concatenate them into a single entry
    You've never seen or done something like this?
    Code:
    str3 = str1 & str2

  10. #10

    Thread Starter
    New Member
    Join Date
    Dec 2014
    Posts
    10

    Re: Pulling information from listbox

    Not really, only thing I've really concatenated are variables that are totally different and declared at the start. I want to highlight like 3 entries and move them over to a single one in the other listbox and I'm a total loss the more frustrated I get the less I can focus and I've been at this for like 6 hours today.

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,350

    Re: Pulling information from listbox

    Quote Originally Posted by JoshTon View Post
    Not really, only thing I've really concatenated are variables that are totally different and declared at the start. I want to highlight like 3 entries and move them over to a single one in the other listbox and I'm a total loss the more frustrated I get the less I can focus and I've been at this for like 6 hours today.
    Yes really. Of course you've seen something like that before. Think about arithmetic. How do you add two numbers? Does the way you add two numbers change depending on where those two numbers come from? Of source not. You put one numeric expression before the addition operator and one after and you get a result. This is the same. You put one String expression before the concatenation operator (&) and one after and you get a String result. Where the two Strings come from is completely irrelevant to the operation of concatenating them, as I've said numerous times now. If you want to concatenate the first selected item from the ListBox with a literal delimiter, e.g. a comma, then you put one of those Strings before the operator and one after and you get the result. If you want to then concatenate that result with the second selected item then you put one before the concatenation operator and one after and you get the result. You can keep doing that for as many Strings as you want and, at the end, you will have a result that combines all of them, just as if you keep adding a number to a running total you will end up with a complete sum at the end. It is no different to adding multiple numbers so stop trying to treat it differently.

  12. #12

    Thread Starter
    New Member
    Join Date
    Dec 2014
    Posts
    10

    Re: Pulling information from listbox

    Ok so I think I have made a small breakthrough,
    Code:
    Dim x As Integer
            For x = 0 To 7
                If lbToppings.GetSelected(x) = True Then
                    lbOrder.Items.Add(lbToppings.Items.Item(x))
                End If
            Next
    I still can't make it a single entry because I don't know how to reference an individual option so I can concatenate, right now it just lists them as separate entries.
    Last edited by JoshTon; Dec 15th, 2014 at 03:28 AM.

  13. #13
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,350

    Re: Pulling information from listbox

    Quote Originally Posted by JoshTon View Post
    Ok so I think I have made a small breakthrough,
    Code:
    Dim x As Integer
            For x = 0 To 7
                If lbToppings.GetSelected(x) = True Then
                    lbOrder.Items.Add(lbToppings.Items.Item(x))
                End If
            Next
    I still can't make it a single entry because I don't know how to reference an individual option so I can concatenate, right now it just lists them as separate entries.
    This is what comes from trying to write code without a clear idea of what the code is supposed to do. Let's look back at post #4:
    break it down into three parts:

    1. Get the items from the first ListBox.
    2. Combine the items.
    3. Add the new item to the second ListBox.
    Adding the result to the second ListBox is the LAST thing you do. You know that you only want to add one item so it should be obvious that you only want to add an item once. If you want to do something once then does it make sense to do it inside a loop, the specific purpose of which is to do something multiple times? What is the thing that you want to do multiple times? I'll give you a hint: it's in post #11. Use the lop to combine the multiple values into one and then add that one value to the second ListBox.

  14. #14

    Thread Starter
    New Member
    Join Date
    Dec 2014
    Posts
    10

    Re: Pulling information from listbox

    I dont know anymore 8 hours into this I just can not figure it out I just don't know. Thanks for the help anyway I guess.

  15. #15
    Hyperactive Member
    Join Date
    Sep 2014
    Posts
    404

    Re: Pulling information from listbox

    i think you should give up personally, as you've already been given all the information and even had all the necessary procedures written for you.

    however, here is how you would do what you are trying to achieve.
    Code:
    Sub  TransferSelectedItems()
    
    Dim SelectedItems As String
    
            For i As Integer = 0 To lbToppings.SelectedItems.Count - 1
            SelectedItems += lbToppings.Items.Item(i) 7 ","
    
                lbOrder.Items.Add(lbToppings.Items.Item(i))
            Next i
    
    
    End Sub
    This will give you what you need your comma separated list in contained within the SelectedItems variable, it will have a trailing comma as i cannot think off the top of my head how to remove this but I believe you would use the mid function

  16. #16
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,543

    Re: Pulling information from listbox

    Quote Originally Posted by JoshTon View Post
    I dont know anymore 8 hours into this I just can not figure it out I just don't know. Thanks for the help anyway I guess.
    Then you're not paying attention.
    First you have to understand what SelectedItems is ... it's a COLLECTION of the selected items <- plural... see that? ItemS ... that means it's like an array. It has elements that you can loop through...
    which... you did... back in post #5
    Code:
     For i = 0 To lbToppings.SelectedItems.Count - 1
    But then rather going through THAT collection, you went back to the original:
    Code:
    lbOrder.Items.Add(lbToppings.Items.Item(i))
    If you have 10 items in the list and every other one is selected, your selectedItems would include those 5 ... indexed from 0-4 ... but your items you're accessing are the original 10, indexed from 0 to 9.... so of course you're going to get the wrong elements.

    what you should have done was gone through the SelectedItems array:
    Code:
    lbOrder.Items.Add(lbToppings.SelectedItems(i))
    You were making it more difficult than it needed to be simply by not paying attention. You focused too much on the fact that it wasn't working with out taking a moment to understand why. And in the process you weren't able to articulate the why.

    Going back to the 10 items in a list, let's say it was
    A
    B
    C
    D
    E
    F
    G
    H
    I
    J

    And I selected E, G, J...
    using your original code, it would have looped 0-2 (correctly) but then returned A,B,C because those are the .Items(0), .Items(1), and .Items(2) items... but what you want is .SelectedITems(0) which is E, .SelectedITems(1) which is G, and .SelectedITems(2) which is J.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

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