-
Jun 28th, 2015, 05:50 PM
#1
Thread Starter
PowerPoster
Check Listbox - Move Cursor to Selected Item
The following code should work (??) to move the cursor to the selected item
but the cursor always remains on item 0.
Suggestions Please!
Code:
'LOOP works but item 0 always is highlighted
'Loop Listbox for checked Items and
'Show the First Selected
For idx = 0 To objLB.ListCount - 1
If objLB.Selected(idx) Then
'Highlight the first selected item
' objLB.ListIndex = idx
Call DBRecord_ShowSelected(objLB.ItemData(idx))
objLB.ListIndex = idx
Exit For
End If
Next
-
Jun 28th, 2015, 06:44 PM
#2
Re: Check Listbox - Move Cursor to Selected Item
Is objLB MultiSelect? If yes, use objLB.Selected(idx) = True
-
Jun 28th, 2015, 09:18 PM
#3
Thread Starter
PowerPoster
Re: Check Listbox - Move Cursor to Selected Item
4x2y: Thanks for responding.
FWIW:
Code:
If objLB.Selected(idx) Then
is the same as
Code:
objLB.Selected(idx) = True
Also: This line of code is not needed, just put in for testing.
Code:
objLB.ListIndex = idx
But had previously tried:
Code:
objLB.Selected(idx) = True
in its place after the call per your suggestion.
Neither s/b needed to get the caret to move to the ListIndex line per code.
-
Jun 28th, 2015, 10:09 PM
#4
Re: Check Listbox - Move Cursor to Selected Item
What exactly do you mean by caret? Carets are in textboxes, not listboxes. Setting a listbox ListIndex property should show a dashed rectangle around the item when the lisbox has focus (but not in themed listboxes it seems). Is that happening? If it is, can you better describe what you are expecting? Maybe a snapshot of your listbox before & and what it should look like after your loop?
Anything special in your listbox properties?
Do you have any code in the listbox's Click event?
For a checked listbox, setting the listindex does move the highlight to the item I told it to go to
Edited: Just to be sure... Is idx declared in that routine? Or is it being changed during your DBRecord_ShowSelected() routine?
Last edited by LaVolpe; Jun 28th, 2015 at 10:24 PM.
-
Jun 28th, 2015, 11:08 PM
#5
Re: Check Listbox - Move Cursor to Selected Item
You didn't answer my question is objLB multiselect?
When the ListBox is multiselect, setting the ListIndex property don't heaghlight the item, so you have to use objLB.Selected(idx) = True
Last edited by 4x2y; Jun 28th, 2015 at 11:46 PM.
-
Jun 29th, 2015, 07:38 AM
#6
Re: Check Listbox - Move Cursor to Selected Item
Originally Posted by 4x2y
You didn't answer my question is objLB multiselect?
Believe the style is checkbox (per title of this thread) & being checkbox, MultiSelect must be zero
-
Jun 29th, 2015, 08:08 AM
#7
Thread Starter
PowerPoster
Re: Check Listbox - Move Cursor to Selected Item
4x2y: MultiSelect must be zero for a list checkbox; see below.
Per LaVolpe:
Believe the style is checkbox (per title of this thread) & being checkbox, MultiSelect must be zero
Based on response, obviously I didn't detail (descibe) things well enough.
===================================
The loop as posted checks the correct list item checkbox.
However, the listbox item that is highlighted is "always" the first item in the list.
What I'm trying to do is have BOTH the correct checkbox checked AS WELL AS have that same item highlighted.
(Lavolpe: Used caret as was not sure how MS refers to listbox highlighted items -- caret, cursor, focus rectangle -- sorry for the confusion)
-
Jun 29th, 2015, 08:32 AM
#8
Re: Check Listbox - Move Cursor to Selected Item
Check the properties... there's one for lsit boxes that hides the highlight when it doesn't have focus. It's set by default... you need to "unset" it or is it the other way around? I forget... but the name of the property is obvious. And it has to be set at design time, I don't think it's affected at runtime.
-tg
-
Jun 29th, 2015, 11:38 AM
#9
Thread Starter
PowerPoster
Re: Check Listbox - Move Cursor to Selected Item
techgnome:
Don't see anything in property list that will have any effect on the problem -- at least for the VB5 listbox -- which I'm using in this case.
-
Jun 29th, 2015, 11:53 AM
#10
Re: Check Listbox - Move Cursor to Selected Item
VB5?
Add a test button to your project and in the click event add this statement: objLB.ListIndex = 2
If you have at least 3 items, did the highlight move to the 3rd row? If so, we'll need to know more about that routine you showed us a portion of.
-
Jun 29th, 2015, 04:23 PM
#11
Thread Starter
PowerPoster
Re: Check Listbox - Move Cursor to Selected Item
Yes - still using it.
Add a test button to your project and in the click event add this statement: objLB.ListIndex = 2
Excellent suggestion -- Should of thought of it myself. -- THANKS{Forest and tree issue is my excuse.}
==========================
Results:
Everything works as designed from the Command Button.
Above post is last part of the routine except for
Code:
obLB.Visible = True
Exit Sub
So far tried:
1) using the original listbox name instead of the object reference (objLB) but still the same.
2) Moving objLB.ListIndex outside of routine
3) Put in a objLB.Refresh
So will have to dig deeper since know it should work.
Out loud >> Only thing I can think of is "objLB.Index = idx" is actually working and then somewhere
along the line is being reset to zero prior to the Listbox actually showing.
Will keep thread open for now.
-
Jun 29th, 2015, 04:32 PM
#12
Re: Check Listbox - Move Cursor to Selected Item
Don't know if the variable idx is declared in that routine or outside of it. If outside of it, is it possible that idx is being reset to zero before DBRecord_ShowSelected().
Wouldn't hurt Debug.Print just before/after setting the ListIndex to see what is happening, i.e.,
Code:
Debug.Print objLB.ListIndex, idx, "before"
... change the listindex
Debug.Print objLB.ListIndex, idx, "after"
-
Jun 29th, 2015, 04:36 PM
#13
Thread Starter
PowerPoster
Re: Check Listbox - Move Cursor to Selected Item
LaVolpe:
Thanks for the assist.
What was happening was per my last post.
Routine was being called from Form_Load and had some code after the call that was causing the index to be reset to zero.
Dumb Mistake, but guess all mistakes are dumb.
Thanks again.
-
Jun 29th, 2015, 04:42 PM
#14
Re: Check Listbox - Move Cursor to Selected Item
How the param of DBRecord_ShowSelected declared? ByRef or ByVal?
Note if ByRef or ByVal is not exist, it is ByRef by default!
-
Jun 29th, 2015, 10:20 PM
#15
Thread Starter
PowerPoster
Re: Check Listbox - Move Cursor to Selected Item
4x2y:
Not exactly sure what your post relates to as THREAD CLOSED and PROBLEM SOLVED.
Re:
Note if ByRef or ByVal is not exist, it is ByRef by default!
FWIW: VB has always been this way. I default to ByVal for all my Params when I create my procedure.
Thanks for your input.
-
Jun 29th, 2015, 10:47 PM
#16
Re: Check Listbox - Move Cursor to Selected Item
Originally Posted by dw85745
4x2y:
Not exactly sure what your post relates to as THREAD CLOSED and PROBLEM SOLVED.
I didn't see your reply #13 before posting.
Mark this thread Resolved
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|