Results 1 to 29 of 29

Thread: Howto add an item to a listbox

  1. #1

    Thread Starter
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359

    Howto add an item to a listbox

    Lets say I have a listbox.
    How would I add an item to the listbox ?
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  2. #2
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808
    ListBox.Additem


    I might be too asleep... but is there any help for VB? Press F1, I can assure there are example for that matter.
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  3. #3
    Bouncy Member darre1's Avatar
    Join Date
    May 2001
    Location
    Peterborough, UK
    Posts
    3,828
    ur kidding right...


    hmm, i think plender's pulling legs.
    Confucious say, "Man standing naked in biscuit barrel not necessarily ****ing crackers."

    Don't forget to format your code in your posts

  4. #4

    Thread Starter
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Originally posted by darre1
    ur kidding right...


    hmm, i think plender's pulling legs.
    heh too right
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  5. #5
    Bouncy Member darre1's Avatar
    Join Date
    May 2001
    Location
    Peterborough, UK
    Posts
    3,828
    ok smartypants...

    why not just create and populate the listbox through a long list of API calls.

    Confucious say, "Man standing naked in biscuit barrel not necessarily ****ing crackers."

    Don't forget to format your code in your posts

  6. #6

    Thread Starter
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Originally posted by darre1
    ok smartypants...

    why not just create and populate the listbox through a long list of API calls.

    or howsabout we don't reinvent the wheel ?
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  7. #7
    Bouncy Member darre1's Avatar
    Join Date
    May 2001
    Location
    Peterborough, UK
    Posts
    3,828
    yeh well, you asked a stupid question (considering) so i felt oblidged to give you a stupid answer.


    of course, i can't be bothered either
    Confucious say, "Man standing naked in biscuit barrel not necessarily ****ing crackers."

    Don't forget to format your code in your posts

  8. #8

    Thread Starter
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    From time to time I tend to ask stupid questions just to see how people would react
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  9. #9
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,539
    from time to time?
    hmmmm
    maybe its just me but i have though of all or your questions the same as i did with this one

  10. #10

    Thread Starter
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Originally posted by kovan
    from time to time?
    hmmmm
    maybe its just me but i have though of all or your questions the same as i did with this one
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  11. #11
    Hyperactive Member billwagnon's Avatar
    Join Date
    Jul 1999
    Location
    St. Louis, Missouri, Mississippi Valley
    Posts
    290
    Fun puzzle: How do you make a listbox that has only the last two selections selected?

    I didn't solve it.

  12. #12

    Thread Starter
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    The last two items selected you mean ?
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  13. #13
    Hyperactive Member billwagnon's Avatar
    Join Date
    Jul 1999
    Location
    St. Louis, Missouri, Mississippi Valley
    Posts
    290
    yes - so you always keep the two (or 'n') most recent items selected

  14. #14
    Serge's Avatar
    Join Date
    Feb 1999
    Location
    Scottsdale, Arizona, USA
    Posts
    2,744
    VB Code:
    1. List1.Selected(List1.ListCount - 1) = True
    2. List1.Selected(List1.ListCount - 2) = True

  15. #15
    Hyperactive Member billwagnon's Avatar
    Join Date
    Jul 1999
    Location
    St. Louis, Missouri, Mississippi Valley
    Posts
    290
    I guess that's what I said...but it's not what I meant

  16. #16

    Thread Starter
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    And make sure MultiSelect is turned on
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  17. #17
    Bouncy Member darre1's Avatar
    Join Date
    May 2001
    Location
    Peterborough, UK
    Posts
    3,828
    ha haa,

    plenderj's thread just turned into something useful

    Confucious say, "Man standing naked in biscuit barrel not necessarily ****ing crackers."

    Don't forget to format your code in your posts

  18. #18

    Thread Starter
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Originally posted by darre1
    ha haa,

    plenderj's thread just turned into something useful

    goddamn I hate it when that happens
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  19. #19
    Hyperactive Member billwagnon's Avatar
    Join Date
    Jul 1999
    Location
    St. Louis, Missouri, Mississippi Valley
    Posts
    290
    It hasn't been useful yet!

    --In a listbox, how do you keep the two most recent (or n most recent) selections selected?

    So if listbox items are

    A
    B
    C
    D
    E
    F
    G

    and I click on E, then D, then A,

    A and D are selected but all others are deselected.

  20. #20
    Bouncy Member darre1's Avatar
    Join Date
    May 2001
    Location
    Peterborough, UK
    Posts
    3,828
    VB Code:
    1. Private Sub List1_Click()
    2.     Dim lngCount As Long
    3.     Dim i As Long
    4.     Dim x As Long
    5.    
    6.     If List1.SelCount < 2 Then Exit Sub 'no point
    7.    
    8.     'find the second list item that is selected
    9.     For i = 0 To List1.ListCount - 1
    10.         If List1.Selected(i) Then lngCount = lngCount + 1
    11.         If lngCount >= 2 Then Exit For
    12.     Next i
    13.    
    14.     If x >= List1.ListCount - 1 Then Exit Sub 'last item so exit
    15.    
    16.     'deselect remaining items
    17.     For x = i + 1 To List1.ListCount - 1
    18.         List1.Selected(x) = False
    19.     Next x
    20. End Sub

    Confucious say, "Man standing naked in biscuit barrel not necessarily ****ing crackers."

    Don't forget to format your code in your posts

  21. #21
    Hyperactive Member billwagnon's Avatar
    Join Date
    Jul 1999
    Location
    St. Louis, Missouri, Mississippi Valley
    Posts
    290
    I think it needs 'memory' of a sort.

    Try your code -

    click on A, then C, then D. D is deselected, but A should be deselected because it is the oldest.

  22. #22
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    Talking You're nicked my old matey!

    I think the answer to your problem regarding the listbox is:
    VB Code:
    1. Private Sub Command1_Click()
    2. Dim adoRecordset   As Recordset
    3.    Set adoRecordset = New Recordset
    4.    adoRecordset.AddItem "Woof and a packet of crisps please"
    5.    adoRecordset.AddItem "2nd Item in listbox"
    6.    adoRecordset.AddItem "3rd and final item"
    7.    Set adoRecordset = Nothing
    8. End Sub
    Hope this helps...Oh sod! I think I've made a mistake, think the code above maybe for a listview and not a listbox...Hmmmmm, can't remember now?

    I can define a string using:
    VB Code:
    1. Dim strString   As String
    Anyone know how do you define an Integer?

  23. #23
    Serge's Avatar
    Join Date
    Feb 1999
    Location
    Scottsdale, Arizona, USA
    Posts
    2,744
    Here you go:
    VB Code:
    1. Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    2. Private Const LB_SETSEL = &H185&
    3.  
    4. Private Sub List1_Click()
    5.     Static intPos1 As Integer
    6.     Static intPos2 As Integer
    7.    
    8.     If List1.SelCount > 2 Then
    9.         'the reason I use API here is because I don't want the Click event
    10.         'to get fired when you say [b]List1.Selected(intPos1) = False[/b]
    11.         Call SendMessage(List1.hwnd, LB_SETSEL, False, ByVal intPos1)
    12.         intPos1 = intPos2
    13.     End If
    14.    
    15.     Select Case List1.SelCount
    16.         Case 1
    17.             intPos1 = List1.ListIndex
    18.         Case 2
    19.             intPos2 = List1.ListIndex
    20.         Case Else
    21.             intPos1 = intPos2
    22.             intPos2 = List1.ListIndex
    23.     End Select
    24. End Sub

  24. #24

    Thread Starter
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    heh Serge, did you move my soccer thread into chit-chat ?
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  25. #25
    Hyperactive Member billwagnon's Avatar
    Join Date
    Jul 1999
    Location
    St. Louis, Missouri, Mississippi Valley
    Posts
    290
    Serge, that works perfectly.

    Can you explain SendMessage? I don't understand what it is doing.

  26. #26
    Serge's Avatar
    Join Date
    Feb 1999
    Location
    Scottsdale, Arizona, USA
    Posts
    2,744
    Originally posted by plenderj
    heh Serge, did you move my soccer thread into chit-chat ?
    Yup, I thought it belongs in the Chit-Chat area

  27. #27
    Serge's Avatar
    Join Date
    Feb 1999
    Location
    Scottsdale, Arizona, USA
    Posts
    2,744
    Originally posted by billwagnon
    Serge, that works perfectly.

    Can you explain SendMessage? I don't understand what it is doing.
    SendMessage API will do exactly what Selected property (it's actually being called when you use Selected property by VB). The only thing is that when you use Selected property, the Click event fires, so you might get "out out stack" error.

  28. #28

    Thread Starter
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Originally posted by Serge

    Yup, I thought it belongs in the Chit-Chat area
    Well I'd like to lodge a formal complaint.
    Jesus Christ and Soccer have everything to do with VB.

    ... and if you don't see that ... well I just don't know what to do at all at all ...

    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  29. #29
    Serge's Avatar
    Join Date
    Feb 1999
    Location
    Scottsdale, Arizona, USA
    Posts
    2,744
    lol

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