Results 1 to 16 of 16

Thread: Check Listbox - Move Cursor to Selected Item

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jul 2001
    Location
    Tucson, AZ
    Posts
    2,166

    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

  2. #2
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: Check Listbox - Move Cursor to Selected Item

    Is objLB MultiSelect? If yes, use objLB.Selected(idx) = True



  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Jul 2001
    Location
    Tucson, AZ
    Posts
    2,166

    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.

  4. #4
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    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.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  5. #5
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    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.



  6. #6
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Check Listbox - Move Cursor to Selected Item

    Quote Originally Posted by 4x2y View Post
    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
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  7. #7

    Thread Starter
    PowerPoster
    Join Date
    Jul 2001
    Location
    Tucson, AZ
    Posts
    2,166

    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)

  8. #8
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  9. #9

    Thread Starter
    PowerPoster
    Join Date
    Jul 2001
    Location
    Tucson, AZ
    Posts
    2,166

    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.

  10. #10
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    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.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  11. #11

    Thread Starter
    PowerPoster
    Join Date
    Jul 2001
    Location
    Tucson, AZ
    Posts
    2,166

    Re: Check Listbox - Move Cursor to Selected Item

    VB5?
    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.

  12. #12
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    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"
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  13. #13

    Thread Starter
    PowerPoster
    Join Date
    Jul 2001
    Location
    Tucson, AZ
    Posts
    2,166

    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.

  14. #14
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    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!



  15. #15

    Thread Starter
    PowerPoster
    Join Date
    Jul 2001
    Location
    Tucson, AZ
    Posts
    2,166

    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.

  16. #16
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: Check Listbox - Move Cursor to Selected Item

    Quote Originally Posted by dw85745 View Post
    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
  •  



Click Here to Expand Forum to Full Width