Edit item value in an unbound list box

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Mihail
    Contributor
    • Apr 2011
    • 759

    Edit item value in an unbound list box

    Hello !
    Please, be patient with me :) but I spend 2 hours to find a way to do that with no success.

    So:
    I have an unbound list box. Only one column.
    I fill this list using .AddItem command.
    Now I wish to modify the value for item(n).

    Oh, of course I can remove this item then insert other one in the same position (index) in the list with the new value.

    But... is no solution to edit that item ?

    Thank you !
  • NeoPa
    Recognized Expert Moderator MVP
    • Oct 2006
    • 32645

    #2
    Very interesting question.

    Unfortunately, I believe the answer is "No". .Itemdata(), which might otherwise have been a viable approach, is a ReadOnly property, and I can find no other approach that might work (other than the obvious one of removing then re-adding the entry of course).

    Comment

    • Mihail
      Contributor
      • Apr 2011
      • 759

      #3
      Because I can't find a solution to directly edit that damned item, I have developed a piece of code to do that:
      Code:
      Private Function ModifyItemInList(LstBx As ListBox, NewValue As String, R As Long, Optional C As Long = 0) As Boolean
      'C > 0 is used for multicolumns list boxes
      On Error GoTo Ex
          ModifyItemInList = False
      Dim SplitedRowSource
          SplitedRowSource = Split(LstBx.RowSource, ";")
          'Modify item
          SplitedRowSource(R * LstBx.ColumnCount + C) = NewValue
      'Create the new RowSource
      Dim NewRowSource As String, i As Long
          For i = 0 To UBound(SplitedRowSource)
              NewRowSource = NewRowSource & SplitedRowSource(i) & ";"
          Next i
          'Remove last ";"
          NewRowSource = Left(NewRowSource, Len(NewRowSource) - 1)
          'Update
          LstBx.RowSource = NewRowSource
          
          ModifyItemInList = True
      Exit Function
      
      Ex:
          ModifyItemInList = False
      End Function
      But more questions has been raised in this time.
      So please take a look to this thread:

      Comment

      • NeoPa
        Recognized Expert Moderator MVP
        • Oct 2006
        • 32645

        #4
        I suggest you use the simpler method of romoving and adding the item using .RemoveItem() and .AddItem() now it's clear how that can work (See linked thread).

        Comment

        Working...