Quantcast
Viewing all articles
Browse latest Browse all 1449

Creating Persistent/Default Selections in List Boxes

I've received a lot of great help here in the last few weeks and want to add a little.

Unless I've missed something there is no built-in way to maintain the currently selected items in a listbox or similar when the user starts moving/clicking.

My situation was a program scan of a list of MIDI commands containing text messages searching for potential sources of composer/artist information and other things useful when organizing tens of thousands of files. You can find anything in a MIDI file and there has never been the least sort of standard for tagging. I needed these potential items to remain selected as the user moves and clicks through the list, copies the actual text from the line to the clipboard, etc.

Other than the refresh flash when the list is cleared and then replaced the following code snippet works very well. It uses the .tag property to store a CSV list of items to remain selected. Put the same code in the KeyUp event. The list must of course have the multiselect property set to "extended (2)"

edit: the misplaced parenthesis in the original are now corrected and numbers found in the .tag list will be properly highlighted. Sorry. I thought it worked fully the first time multiple values were encountered but I was wrong.

Code:

Dim i&, j&
   
    Dim strLine As String
    Dim strDummy As String

    'read the tag--when CSV numeric values are found use them to set "default" items in the list
    If Trim(lstMidText.Tag) > "" Then
    'save the current index number to the selected item
        i& = lstMidText.ListIndex
        strLine = lstMidText.Tag
        Do Until InStr(strLine, ",") = 0 'read and strip numeric values until no commas remain
            j& = Val(Left(strLine, InStr(strLine, ",") - 1))
            lstMidText.Selected(j&) = True
            strLine = Mid(strLine, InStr(strLine, ",") + 1)
        Loop
        If Val(strLine) > 0 Then 'ensure that sole and final entries are included
            lstMidText.Selected(Val(strLine)) = True
        End If
    'restore saved index number
        lstMidText.ListIndex = i&
    End If

Here's the simple If..Then to build the .tag where i& = value of list item to be selected

Code:

If Trim(lstMidText.Tag) = "" Then
      lstMidText.Tag = Trim(Str(i&))
Else
      lstMidText.Tag = lstMidText.Tag + "," + Trim(Str(i&))
End If


Viewing all articles
Browse latest Browse all 1449

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>