ListBox MultiSelection Problem

J

janKowalskiPL

Hello,

I am building a form which will play with all selected lines in
ListBox. I have ListBox filled like that:

Option1
Option2
Option3

and I select by mouse Option1 and Option2. The problem is as follows:
How to get these selected elements (I mean strings like 'Option1',
'Option3', not indexes).

My code:

For i = 0 To MyList.ListCount - 1
If MyList.Selected(i) Then
MyString = MyString & MyList.??? // what to put
here??? cause ItemData return index, not string value
End If
Next

Regards, Jan
 
B

Bob Quintal

MyString = MyString & MyList.column(N,i)
where N is the zero-based count from left to right.
(1st column is 0, 3rd column is 2)


(e-mail address removed) wrote in
Hello,

I am building a form which will play with all selected lines
in ListBox. I have ListBox filled like that:

Option1
Option2
Option3

and I select by mouse Option1 and Option2. The problem is as
follows: How to get these selected elements (I mean strings
like 'Option1', 'Option3', not indexes).

My code:

For i = 0 To MyList.ListCount - 1
If MyList.Selected(i) Then
MyString = MyString & MyList.??? // what to
put
here??? cause ItemData return index, not string value
End If
Next

Regards, Jan
 
D

Dale Fye

Jan,

You could shorten the run time of the code a little if instead of looping
through each item in the list, you only loop through the ItemsSelected
collection.

Dim MyString as variant
Dim varItem as variant
For each varItem in MyList.ItemsSelected
MyString = (MyString + ",") & chr$(34) & MyList.column(N, varItem) &
chr$(34)
Next

I've made MyString a variant so that I can concatenate the comma as a list
separater using the plus sign rather than the ampersand. This technique
results in
dropping the comma the first time through the loop, since MyString will be
NULL during the first pass.

See Bob's post regarding the Column reference to N.

HTH
Dale
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top