dcsimg
Results 1 to 5 of 5

Thread: [RESOLVED] ListBox AddItem mystery

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2013
    Posts
    55

    Resolved [RESOLVED] ListBox AddItem mystery

    I'm almost embarrassed to ask about this as I thought I'd been using similar code for more years than I care to remember.

    For the purposes of describing the problem I'm having, I have a form with two listboxes on it: List1 and List2. I've filled List1 with place names (using the AddItem method) and the idea is that when I click on any of those names. I want the current day's date to appear in List2 directly opposite the name I've clicked, which can mean anywhere in the list.

    Here is the relevant code I've used:

    Private Sub List1_Click()

    Dim strDate As String

    strDate = Format(Now, "yyyymmdd")
    List2.AddItem strDate, List1.ListIndex

    End Sub

    That's it. However I am always ending up with an 'Invalid procedure call or argument' error (Code 5) for the AddItem operation UNLESS I click on the LIst1 list in order from top to bottom - which is obviously defeating the object of the exercise. Looking at the help for the AddItem method suggests that the above code should work fine and do what I want (in this example).

    Am I missing something, or is this not possible?

    Thanks for any suggestions.

    John Marchington

  2. #2
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    17,601

    Re: ListBox AddItem mystery

    If I understand you correctly, you are going to need to fill List2, up to your List1.ListIndex-1 as needed, with blank entries. Then add the date. The error is likely that List1.ListIndex exceeds List2.ListCount
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  3. #3

    Thread Starter
    Member
    Join Date
    Nov 2013
    Posts
    55

    Re: ListBox AddItem mystery

    Quote Originally Posted by LaVolpe View Post
    If I understand you correctly, you are going to need to fill List2, up to your List1.ListIndex-1 as needed, with blank entries. Then add the date. The error is likely that List1.ListIndex exceeds List2.ListCount
    Thanks, LaVolpe. I guess I should have considered that possibility myself. I've subsequently added the same number of zero-length strings to List2 as there are items in List1 and thought that had solved the problem as I clicked on a place name in List1 and the date appeared correctly positioned in the other box. However, when I clicked on another name positioned higher in List1, and the date appeared correctly, the lower-positioned date moved down one row and that continued to occur when other places were clicked. I've also modified the 'real' program I'm writing and have noticed the same anomaly there. I'll keep working on it and may come up with a solution.

  4. #4
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    17,601

    Re: ListBox AddItem mystery

    If you already have enough items in List2, then you can't add another one, you need to change the existing one.

    In that case, instead of .AddItem, something like List2.List(List1.ListIndex) = strDate
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  5. #5

    Thread Starter
    Member
    Join Date
    Nov 2013
    Posts
    55

    Re: ListBox AddItem mystery

    Quote Originally Posted by LaVolpe View Post
    If you already have enough items in List2, then you can't add another one, you need to change the existing one.

    In that case, instead of .AddItem, something like List2.List(List1.ListIndex) = strDate
    It's working beautifully now, so thank you once again. The next step is to figure out how to remove a date when I click on the same place a second time. (You're possibly wondering what all this is about but it's definitely required in my program). It may work with the RemoveItem method but it may need the List2.List() method with a "" string. Anyway, I'll try and solve that issue myself.

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width