Selected... or not ??

K

kirkm

What's the group consider the definition of Selected ?


Here's Excel help...

--
object.Selected( index ) [= Boolean]

The Selected property syntax has these parts:

Part Description
object Required. A valid object.
index Required. An integer with a range from 0 to one less than the
number of items in the list.
Boolean Optional. Whether an item is selected.


Settings

The settings for Boolean are:

Value Description
True The item is selected.
False The item is not selected.
--

So, this command

frmTest.List1.Selected(0) = True

Should select line1 in a listbox?

Well I supoose (as it does) to put a marqee around the line does
'Select' it but
changing the command to = False additionally highlights the line.

Which surely means, THEN it's selected ? Or is my Excel working in
reverse ? :)
 
D

Dave Peterson

I bet you have some other code that's running when you change that .selected(0)
to true. Look for the list_change event.

In fact, if you can step through the code, you may see it walk to a procedure
that you didn't realize was firing.

If I'm correct, you can do this kind of thing:

Option Explicit
Dim BlkProc As Boolean
Private Sub CommandButton1_Click()
BlkProc = True
Me.ListBox1.Selected(0) = True
BlkProc = False
End Sub
Private Sub ListBox1_Change()
If BlkProc = True Then Exit Sub
'your code here
MsgBox "hi from _change"
End Sub
Private Sub UserForm_Initialize()
With Me.ListBox1
.AddItem "A"
.AddItem "B"
.AddItem "C"
End With
End Sub

The BlkProc variable (and the check if it's true) serves the same kind of
purpose as "Application.enableevents = false" in a worksheet event.

Try uncommenting the "if blkproc = true" statement and you'll see that the
_change event will fire if the user changes the listbox--or if your code changes
it.

What's the group consider the definition of Selected ?

Here's Excel help...

--
object.Selected( index ) [= Boolean]

The Selected property syntax has these parts:

Part Description
object Required. A valid object.
index Required. An integer with a range from 0 to one less than the
number of items in the list.
Boolean Optional. Whether an item is selected.

Settings

The settings for Boolean are:

Value Description
True The item is selected.
False The item is not selected.
--

So, this command

frmTest.List1.Selected(0) = True

Should select line1 in a listbox?

Well I supoose (as it does) to put a marqee around the line does
'Select' it but
changing the command to = False additionally highlights the line.

Which surely means, THEN it's selected ? Or is my Excel working in
reverse ? :)
 
K

kirkm

On Wed, 03 Jun 2009 21:08:13 -0500, Dave Peterson
Hi Dave,

Thanks for that suggestion. I spent several hours
playing around... it showed the Change event was fired
more than once, and that my List Enter and Userform
Initialise code wasn'tt running every time. I can't fathom why
things work differently with a temp STOP command, than without one.

Anyway, I moved the form Initialise and List enter stuff into a
module, so I know it's runs once, reliably.

This produced a marqee-only every time, but the following
Selects it -

Private Sub List1_Enter()

Me.List1.Selected(0)) = False
Me.List1.Selected(0) = True

Exit Sub

Doesn't make sense to me... but this seems to do the trick

Thanks - Kirk
 
P

Patrick Molloy

any time you click an item in a listbox control, the change event fires.
when an item is selected using the SELECTED property, it also fires the
change event. The change event will fire even if the item state doesn't
change

so
listbox1.selected(5)=True
listbox1.selected(5)=True
will fire twice even though the 2nd line doesn't change the property value.
imagine it like the worksheet change event. it will fire if you type A into
a cell. it will fire again if you type A into the same cell


for a multiselect you test if an item is selected

with listbox1
for i = 0 to .listcount-1 'zero based
if .selected(i) then
'item i is selected
end if
next
end with

an item is selected by using this code
listbox1.selected(index)=True
and deselected by
listbox1.selected(index)=False


in message news:[email protected]...
 
D

Dave Peterson

Or you could have tried using a boolean variable to exit the event you don't
want as soon as it starts.
 
K

kirkm

On Thu, 4 Jun 2009 09:46:11 +0100, "Patrick Molloy"

Thanks for the rundown. Learning more all the time.. ceratinly all
this multiple firing and what's called and in what sequencecan be hard
to follow. By the waycould you explain what 'Control Source' is in
relation to a listbox, please?
Not the Excel help, which I can't fathom, but in normal English? :)

Thanks... there was another thread but it went cold, and there
was no answer.
Cheers - Kirk
 

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