Results 1 to 6 of 6
  1. #1
    3 Star Lounger
    Join Date
    Dec 2000
    Location
    NJ, USA
    Posts
    239
    Thanks
    2
    Thanked 1 Time in 1 Post

    ListBox.Selected (2000, SP-3)

    I have several multi-select ListBoxes that require similar processing. Consequently I'd like to create a common subroutine that can be called. However, it doesn't seem to allow me to pass a reference to the ListBox itself, nor will it accept ListBox.Selected - which should just be a boolean array. The only way I can figure is to populate another boolean array, abln() say, from ListBox.Selected and pass that. And the additional array CAN'T be populated by a statement like "abln = ListBox.Selected"; it seems that the values have to be populated individually in a "For i = 0 to..." loop.
    Any thoughts anybody? TIA

  2. #2
    Platinum Lounger
    Join Date
    Nov 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    5,016
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: ListBox.Selected (2000, SP-3)

    Hi Colin

    Without seeing it, this is just a stab at the kind of thing you might be after. In the module for the UserForm, General section:

    Dim box As Object

    Public Sub showListBoxInfo()
    MsgBox "Listbox Name is " & box.Name
    End Sub

    Somewhere you need to call your sub:

    Set box = ListBox1
    showListBoxInfo
    Set box = ListBox2
    showListBoxInfo
    Set box = Nothing

    This seems to work for the simple example I created.

    Alan

  3. #3
    3 Star Lounger
    Join Date
    Dec 2000
    Location
    NJ, USA
    Posts
    239
    Thanks
    2
    Thanked 1 Time in 1 Post

    Re: ListBox.Selected (2000, SP-3)

    Excellent. Thanks, Alan.
    I was trying to pass the information via parameters in the Call, and hadn't thought of using a global variable/reference and accessing it that way. I noticed that it only works when it's dimensioned as a generic object, as you did. If you define it as a ListBox, it doesn't like it. However, the object still has the attributes of a ListBox; in particular the boolean array .Selected can be accessed.
    Thanks again.

  4. #4
    Platinum Lounger
    Join Date
    Nov 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    5,016
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: ListBox.Selected (2000, SP-3)

    Hmmm... <img src=/S/hmmn.gif border=0 alt=hmmn width=15 height=15>. Unfortunately I don't use VB enough to recall all of the intricacies. But if you're going to reference an existing object, the "alias" must be declared as type Object. I think Variant (or typeless) will work*, but I seem to recall a gotcha if you try to set it to a non-object value. Declaring as ListBox would refer to an actual ListBox control, rather than a reference to it - that kind of declaration is not possible.

    Regards passing as a parameter, I can't see off hand why that wouldn't be possible; certainly for the object reference, if not the object itself. Maybe someone closer to the nuts & bolts could answer that one. Still, glad it's at least a workable solution.

    Alan

    *Edited - Correction: Variant does NOT include object references, but not specifying a data type will work.

  5. #5
    WS Lounge VIP sdckapr's Avatar
    Join Date
    Jul 2002
    Location
    Pittsburgh, Pennsylvania, USA
    Posts
    11,225
    Thanks
    14
    Thanked 342 Times in 335 Posts

    Re: ListBox.Selected (2000, SP-3)

    If you want a paramater pass you could use:

    <pre>Public Sub showListBoxInfo(lb As Object)
    MsgBox "Listbox Name is " & lb.Name
    End Sub</pre>


    And in code call it with:
    <pre> showListBoxInfo me.listbox1
    showListBoxInfo me.listbox2</pre>


    Steve

  6. #6
    Platinum Lounger
    Join Date
    Nov 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    5,016
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: ListBox.Selected (2000, SP-3)

    Of course - the Me. qualifier. It shows how often I take too long a break from VB! I guess, for Colin's case, UserForm1.ListBox1 etc. would do the job too, although Me is decidedly "nicer".

    Alan

Posting Permissions

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