Results 1 to 10 of 10

Thread: ListBox - Highlight 1st Item

  1. #1

    Thread Starter
    Frenzied Member aikidokid's Avatar
    Join Date
    Aug 2002
    Location
    Bristol, UK
    Posts
    1,968

    ListBox - Highlight 1st Item

    When I load a listbox I am trying to have the first item highlighted, but each time I do I seem to fire the click event some how.

    I have the following:
    vb Code:
    1. lstSearch.Selected (0) = true
    but this acts as if it has been clicked.

    I am just after getting it highlighted, but nothing else.
    If somebody helps you, take time to RATE the post. I do.

    "FAILURE IS NOT AN OPTION. It comes bundled with the software."

    Below are some of the threads that have helped me along the way:

    CodeBank submission:
    Listview Backcolor (without subclassing)

    Loading Treeview Nodes From A Database, Creating Registry Keys, Count Number of Lines in TextBox , Excellent RichTextBox Tricks & Tips
    Ideas & Screen Shots For A Code Library App
    How to do Data validation in Excel, Conditional Formating in Excel

  2. #2
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: ListBox - Highlight 1st Item

    set a boolean flag:
    Code:
    Private bNoClick As Boolean
    
    Private Sub Form_Load()
        bNoClick = True
        lstSearch.Selected(0) = True 
        bNoClick = False
    End Sub
    
    Private Sub lstSearch_Click()
        If bNoClick Then Exit Sub
        ' Code
    End Sub

  3. #3

    Thread Starter
    Frenzied Member aikidokid's Avatar
    Join Date
    Aug 2002
    Location
    Bristol, UK
    Posts
    1,968

    Re: ListBox - Highlight 1st Item

    Quote Originally Posted by bushmobile
    set a boolean flag:
    Code:
    Private bNoClick As Boolean
    
    Private Sub Form_Load()
        bNoClick = True
        lstSearch.Selected(0) = True 
        bNoClick = False
    End Sub
    
    Private Sub lstSearch_Click()
        If bNoClick Then Exit Sub
        ' Code
    End Sub
    Thanks bushmobile

    So decaring this in the form load would work only when the form was first loaded - yes?
    So, if the user does a second search, without closing and re-opening the form, would this work?
    If somebody helps you, take time to RATE the post. I do.

    "FAILURE IS NOT AN OPTION. It comes bundled with the software."

    Below are some of the threads that have helped me along the way:

    CodeBank submission:
    Listview Backcolor (without subclassing)

    Loading Treeview Nodes From A Database, Creating Registry Keys, Count Number of Lines in TextBox , Excellent RichTextBox Tricks & Tips
    Ideas & Screen Shots For A Code Library App
    How to do Data validation in Excel, Conditional Formating in Excel

  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: ListBox - Highlight 1st Item

    No...it will only work in the form load.

    What do you have in the click event that you don't want run?

    (Incidentially, bushmobile's method is what I've always used.....I laid back on this one to see if anyone else had a better solution.)

  5. #5
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: ListBox - Highlight 1st Item

    Quote Originally Posted by aikidokid
    So decaring this in the form load would work only when the form was first loaded - yes?
    So, if the user does a second search, without closing and re-opening the form, would this work?
    basically if you're changing which List item has focus and you don't want it to fire the _Click event then you set the boolean to True in the line before (you then set it to false the line after, so _Click events can fire again).

    I don't really understand what you're trying to say, but it's not a one off thing - you have manually decide when it ignores the _Click and when it doesn't.

  6. #6

    Thread Starter
    Frenzied Member aikidokid's Avatar
    Join Date
    Aug 2002
    Location
    Bristol, UK
    Posts
    1,968

    Re: ListBox - Highlight 1st Item

    Quote Originally Posted by Hack
    No...it will only work in the form load.
    That's what I thought, but I just wanted to make sure - been a long day so far

    Quote Originally Posted by Hack
    What do you have in the click event that you don't want run?
    What I would like to do is, when the listbox is first loaded, is have the first item highlighted. then if the user (me ) wants to navigate with the keyboard, it would highlight each selection to show where they were.
    It's not crucial, but I just liked the idea for clarity.

    In the Click event I have the code that loads the selected file from the DB to the RTB on the main form.

    Quote Originally Posted by Hack
    (Incidentially, bushmobile's method is what I've always used.....I laid back on this one to see if anyone else had a better solution.)
    Always worth looking about
    I know this, just from this site
    If somebody helps you, take time to RATE the post. I do.

    "FAILURE IS NOT AN OPTION. It comes bundled with the software."

    Below are some of the threads that have helped me along the way:

    CodeBank submission:
    Listview Backcolor (without subclassing)

    Loading Treeview Nodes From A Database, Creating Registry Keys, Count Number of Lines in TextBox , Excellent RichTextBox Tricks & Tips
    Ideas & Screen Shots For A Code Library App
    How to do Data validation in Excel, Conditional Formating in Excel

  7. #7
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: ListBox - Highlight 1st Item

    One thing about using "workarounds" is that you should document what you are doing and why. Not for anyone else looking at your code, but so that you, 6 months from now, won't scratch your head and ask yourself "what the heck did I do that for?"

  8. #8

    Thread Starter
    Frenzied Member aikidokid's Avatar
    Join Date
    Aug 2002
    Location
    Bristol, UK
    Posts
    1,968

    Re: ListBox - Highlight 1st Item

    Quote Originally Posted by Hack
    One thing about using "workarounds" is that you should document what you are doing and why. Not for anyone else looking at your code, but so that you, 6 months from now, won't scratch your head and ask yourself "what the heck did I do that for?"
    thanks for the advice.
    One thing that I am getting better at is copious amounts of commenting.
    I don't have to wait 6 months to wonder what I was doing.

    I though this would be some property setting that I had missed, but obviously it's not that straight forward.

    Like bushmobile says, sort out when you want to fire the click event and do it manually.
    If somebody helps you, take time to RATE the post. I do.

    "FAILURE IS NOT AN OPTION. It comes bundled with the software."

    Below are some of the threads that have helped me along the way:

    CodeBank submission:
    Listview Backcolor (without subclassing)

    Loading Treeview Nodes From A Database, Creating Registry Keys, Count Number of Lines in TextBox , Excellent RichTextBox Tricks & Tips
    Ideas & Screen Shots For A Code Library App
    How to do Data validation in Excel, Conditional Formating in Excel

  9. #9
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: ListBox - Highlight 1st Item

    The more experienced you get, the more you will find your code documentation diminish. But, these little "juryrigs" need to always be commented.

    And, you have discovered what will continue to be an "feature" of the listbox.

  10. #10

    Thread Starter
    Frenzied Member aikidokid's Avatar
    Join Date
    Aug 2002
    Location
    Bristol, UK
    Posts
    1,968

    Re: ListBox - Highlight 1st Item

    Quote Originally Posted by Hack
    And, you have discovered what will continue to be an "feature" of the listbox.
    Ah well, something else to take a look at, along with writing th rest of the app.
    If somebody helps you, take time to RATE the post. I do.

    "FAILURE IS NOT AN OPTION. It comes bundled with the software."

    Below are some of the threads that have helped me along the way:

    CodeBank submission:
    Listview Backcolor (without subclassing)

    Loading Treeview Nodes From A Database, Creating Registry Keys, Count Number of Lines in TextBox , Excellent RichTextBox Tricks & Tips
    Ideas & Screen Shots For A Code Library App
    How to do Data validation in Excel, Conditional Formating in Excel

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