Brain cramp! setting value of multiselect list

D

Dale Fye

I've got a listbox that I'm using for several purposes, based on several of
the other controls on a form. I had thought about using multiple lists (some
multi-select, others single-select), but after working through the number of
combinations of functionality, decided it would take about 6 different lists,
and decided to try using one list instead.

Depending on the selections in a couple of combo boxes, I wanted to display
different fields (just change the column widths - this works fine).

In other instances, I need to switch between multi-select and single-select
(obviously this is a problem). I've worked out a way to ensure that only a
single item is selected when I want the single select functionality.
However, in one instance I have to requery the list, and after the requery, I
want to highlight the single row that was highlighted prior to the requery.
Since I cannot set the listindex, I'm using:

intListIndex = lst.listindex
lst.requery
lst.Selected(intListIndex) = true
debug.print lst.listindex

which works to highlight the previously highlighted row. However, after the
requery and the next line of code, the lists listindex property shows -1.
Is there any way to get the lists listindex property to reflect the previous
value?

I'm about ready to go back to the drawing board and resort to multiple
lists, but thought I'd check with you guyst first.

Thanks.
 
D

Dirk Goldgar

Dale Fye said:
I've got a listbox that I'm using for several purposes, based on several
of
the other controls on a form. I had thought about using multiple lists
(some
multi-select, others single-select), but after working through the number
of
combinations of functionality, decided it would take about 6 different
lists,
and decided to try using one list instead.

Depending on the selections in a couple of combo boxes, I wanted to
display
different fields (just change the column widths - this works fine).

In other instances, I need to switch between multi-select and
single-select
(obviously this is a problem). I've worked out a way to ensure that only
a
single item is selected when I want the single select functionality.
However, in one instance I have to requery the list, and after the
requery, I
want to highlight the single row that was highlighted prior to the
requery.
Since I cannot set the listindex, I'm using:

intListIndex = lst.listindex
lst.requery
lst.Selected(intListIndex) = true
debug.print lst.listindex

which works to highlight the previously highlighted row. However, after
the
requery and the next line of code, the lists listindex property shows -1.
Is there any way to get the lists listindex property to reflect the
previous
value?

1. What mode is the list box in at this point, single- or multi-select?

2. Why do you care what the ListIndex is?

3. Does the list box have the focus when this code is running, or not?
 
D

Dale Fye

Dirk,

1. It's set as multi-select.
2. I need to highlight the previously selected row after requerying the
list. But since a multi-select list doesn't really have a value, I figured
using the listindex and the Selected property would be the best way to do
this. But the next time I come into this code segment, the listindex is set
to -1.
3. This particular segment of code is in a routine that fires in the click
event of a command button. Basically, what it does is allows me to
resequence the items in the list by changing their sort order. I click on an
item, then click an up or down button. The problem comes in when I want to
click the up or down button several times, without reselecting the item in
the list. The code works great in a single select list, but is not working in
a multi-select environment.

If I cannot come up with a solution to this problem by tomorrow AM, I'll
probably fall back to my original idea and use several different lists.

--
HTH
Dale

email address is invalid
Please reply to newsgroup only.
 
D

Dirk Goldgar

Dale Fye said:
Dirk,

1. It's set as multi-select.
2. I need to highlight the previously selected row after requerying the
list. But since a multi-select list doesn't really have a value, I
figured
using the listindex and the Selected property would be the best way to do
this. But the next time I come into this code segment, the listindex is
set
to -1.
3. This particular segment of code is in a routine that fires in the click
event of a command button. Basically, what it does is allows me to
resequence the items in the list by changing their sort order. I click on
an
item, then click an up or down button. The problem comes in when I want
to
click the up or down button several times, without reselecting the item in
the list. The code works great in a single select list, but is not working
in
a multi-select environment.

You could set the focus to the listbox, and then set the ListIndex:

With lst
intListIndex = .ListIndex
.Requery
.Selected(intListIndex) = True
.SetFocus
.ListIndex = intListIndex
End With

Is this what you want? If your requery changed the order of the items,
wouldn't the originally selected item be at a different ListIndex? Maybe
you need to get the value of that item before requerying:

With lst
varItemValue = .ItemData(.ListIndex)

.Requery

And then locate it again by looping through the ItemData collection:

For I = Abs(.ColumnHeads) To .ListCount - 1
If .ItemData(I) = varItemValue Then
.Selected(I) = True
Exit For
End If
Next I

End With
 
P

Peter Hibbs

Dale,

Have you considered using a Flex Grid control. This would, with a bit
of code, give you complete flexibility over the display.

Peter Hibbs.
 
D

Dale Fye

Dirk,

Thanks for the ideas I just needed someone to kick me in the head to get rid
of the brain cramp.

Dirk wrote: If your requery changed the order of the items,
wouldn't the originally selected item be at a different ListIndex?

Yes, I was actually using the ListIndex +/- 1 when I reselect the item. I
did not realize that you could set the listindex property, if the list has
the focus (or using With). I'll try that, as that would accomplish the goal.

I did something similar to your last For loop last night, and that worked as
well, for getting the index of the currently selected item, without using the
ListIndex property.

Dim intIndex as integer
Dim varItem as variant

For each varItem in lst.ItemsSelected
intIndex = cint(varItem)
Next varItem


--
HTH
Dale

email address is invalid
Please reply to newsgroup only.
 
D

Dirk Goldgar

Dale Fye said:
Dirk,

Thanks for the ideas I just needed someone to kick me in the head to get
rid
of the brain cramp.

Dirk wrote: If your requery changed the order of the items,
wouldn't the originally selected item be at a different ListIndex?

Yes, I was actually using the ListIndex +/- 1 when I reselect the item. I
did not realize that you could set the listindex property, if the list has
the focus (or using With). I'll try that, as that would accomplish the
goal.


Yes, you can set the ListIndex property if the list box has the focus. But
be aware that setting the ListIndex of a multi-select list box doesn't
select the item at that index.
 
D

Dale Fye

Thanks, Dirk.

That did the trick!
--
Dale

email address is invalid
Please reply to newsgroup only.
 

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