CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Sep 2001
    Location
    MA
    Posts
    11

    type mismatch error

    Ok, I know why I'm getting this error but can't figure out how to fix it.

    I'm trying to create a string of index values based on what is selected in a listbox. The listbox is populated with two values: The itemdata and an index value tied to the item data. For example, the user sees itemdata such as 'Harry' in the listbox which is tied to an index value corresponding to let's say Harry's customer id. I want to be able to create a string of these customer id values hidden within the listbox's index. I also need to create a string of the itemdata value's as well.Here's a short sample of my code that tries to pull out a string of itemdata and index data:


    Dim index as string
    Dim itemdata as string
    Dim i as Integer
    Dim first as Boolean
    first = true
    for i = 0 to (list1.ListCount - 1)
    If (list1.Selected(i)) then
    If (first) then
    itemdata = "(" & list1.ItemData(i)
    index = "(" & list1.ItemData(list1.ListIndex)(i)
    first = false
    else
    itemdata = itemdata & "," & list1.Itemdata(i)
    index = index & "," & list1.ItemData(list1.ListIndex)(i)
    End If
    End If
    next i
    itemdata = itemdata & ")"
    index = index & ")"




    This code is tied to a command button. If item values 'Harry', 'Mary', 'Bob' are selected the output should be a string of customerids like (1,4,6) for example. But everytime I run it I get a 'type mismatch' error due to trying to create the string of index values. I've never tried creating a string of values based on a listbox's index, only it's itemdata. Any help would be great! Thanks.



  2. #2
    Join Date
    Jan 2000
    Location
    Saskatchewan, Canada
    Posts
    595

    Re: type mismatch error

    index = "(" & list1.ItemData(list1.ListIndex)(i)

    Take out the (i)

    David Paulson


  3. #3
    Join Date
    Feb 2000
    Location
    OH - USA
    Posts
    1,892

    Re: type mismatch error

    Ok Im not exactly sure what you are looking for but I think that replacing the lines from this:


    List1.itemdata(List1.ListIndex)(i)

    to this

    List1.itemdata(i)




    will solve your Type-Mismatch problem.

    Good Luck,
    Craig - CRG IT Solutions - Microsoft Gold Partner

    -My posts after 08/2015 = .NET 4.x and Visual Studio 2015
    -My posts after 11/2011 = .NET 4.x and Visual Studio 2012
    -My posts after 02/2010 = .NET 4.0 and Visual Studio 2010
    -My posts after 12/2007 = .NET 3.5 and Visual Studio 2008
    -My posts after 04/2007 = .NET 3.0 and Visual Studio 2005
    -My posts before 04/2007 = .NET 1.1/2.0

    *I do not follow all threads, so if you have a secondary question, message me.

  4. #4
    Join Date
    Sep 2001
    Location
    MA
    Posts
    11

    Re: type mismatch error

    thanks for the suggestion but it won't fix it.
    changing
    List1.itemdata(List1.ListIndex)(i)to List1.itemdata(i)
    will not solve the problem. that solution will give what's selected in the listbox, not the index associated with it.

    taking the (i) off the end of the list1.itemdata(list1.listindex) statement did get rid of the type mismatch error. the only problem is that the string created is composed of the last index value selected in the listbox (ie. 6,6,6). still need something in there that will keep track of the index values. any suggestions?


  5. #5
    Join Date
    Jun 2001
    Location
    MO, USA
    Posts
    2,868

    Re: type mismatch error

    Isn't the ListIndex equal to your loop counter, i? just use it's value....


  6. #6
    Join Date
    Sep 2001
    Location
    MA
    Posts
    11

    Re: type mismatch error

    i can't use the loop counter because the index value is not the same. i populated the listbox with data from a database. when i did this i created a newindex equal to a unique field value from the database. I want to be able to pull this index value out. in other words the index of the listbox is not ordered from 0. its ordered/indexed by the database field value instead.


  7. #7
    Join Date
    Jan 2000
    Location
    Saskatchewan, Canada
    Posts
    595

    Re: type mismatch error

    Hi
    Try this. I think this is what your after.


    Dim index As String
    Dim itemdata As String
    Dim i As Integer
    Dim first As Boolean
    first = True
    For i = 0 To (List1.ListCount - 1)
    If (List1.Selected(i)) Then
    If (first) Then
    itemdata = "(" & List1.List(i)
    index = "(" & List1.itemdata(i)
    first = False
    Else
    itemdata = itemdata & "," & List1.List(i)
    index = index & "," & List1.itemdata(i)
    End If
    End If
    Next i
    itemdata = itemdata & ")"
    index = index & ")"




    David Paulson


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

Featured