Results 1 to 9 of 9
  1. #1
    sgerber
    Guest

    Selecting first item in a listbox

    I hope I'm not make too big a pest of myself...

    I'm trying to figure out how to bring up a listbox with the first item selected (highlighted). WordBasic did that by default. VBA doesn't seem to.

  2. #2
    3 Star Lounger
    Join Date
    Feb 2001
    Location
    Portland, Maine, USA
    Posts
    296
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Selecting first item in a listbox

    I couldn't find a way to do this either. Instead, I made the first item in my list box blank. Then a user can just start typing to select from the list.

  3. #3
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Selecting first item in a listbox

    CAUTION: I'm doing this by hand, not pasting a chunk of working code.

    You highlight the fourth item in a list by:
    lb.ListIndex = 3

    This fails if there aren't four items in a list, so you test:
    If lb.ListCount >=3 Then ....


    To highlight the first (adjacent to the top border) item in a listbox you'd need something like:
    If lb.ListCount >=0 Then
    lb.ListIndex = 0
    Else
    MsgBox "There are no items in the list!"
    Endif

    If I haven't already done so, I should post as "Code:" my function lngIndexToListBox.

  4. #4
    Uranium Lounger
    Join Date
    Jan 2001
    Location
    South Carolina, USA
    Posts
    7,295
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Selecting first item in a listbox

    Something like this should do it:
    <pre> frmAccount.cbAccountNumber.ListIndex = 0
    frmAccount.cbAccountNumber.SelStart = 0
    frmAccount.cbAccountNumber.SelLength = Len(frmAccount.cbAccountNumber.list(0,0))
    </pre>

    Legare Coleman

  5. #5
    sgerber
    Guest

    Re: Selecting first item in a listbox

    Chris,

    The ListIndex property did the trick. Thanks very much.

    (The list I'm dealing with always contains at least five items, so I don't need to test for length, but it was useful to see how to do that, too.)

  6. #6
    sgerber
    Guest

    Re: Selecting first item in a listbox

    Legare,

    Thanks very much for the suggestion.

    As it turned out in my particular case, the ListIndex property did the job all by itself.

    Just out of curiosity (and only if you've got the time to explain), what do those other two lines of code do?

  7. #7
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Selecting first item in a listbox

    Here's another little snippet. Today I wanted to advance the highlighted item in the list by one (down the list) each time the "load" button was clicked on the GUI. If I am already at the bottom of my list, I highlight the top item in the list




    <pre> If Me.lbTables.ListIndex >= Me.lbTables.ListCount - 1 Then
    Me.lbTables.ListIndex = 0
    Else
    Me.lbTables.ListIndex = Me.lbTables.ListIndex + 1
    End If
    </pre>


  8. #8
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Selecting first item in a listbox

    You can use an approach like that to select the text in a textbox:

    'select text in textbox:
    With txtBmkName
    .SelStart = 0
    .SelLength = Len(.Text)
    End With

    (I think I learned that one from Rory, here...)

  9. #9
    sgerber
    Guest

    Re: Selecting first item in a listbox

    Gary,

    I needed that one, too. Thanks.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •