Results 1 to 5 of 5

Thread: Listbox: Set background color when item selected by program

  1. #1
    .NET Framework
    .NET 4.0
    Join Date
    May 2010
    Location
    Auckland, New Zealand
    Posts
    108
    Reputation
    106

    Listbox: Set background color when item selected by program

    I have a listbox called lbxDBType. When a value is clicked it is selected and the background color is set blue to highlight it.

    If my program selects a value by executing
    lbxDBType.SetSelected(IX, True)
    this doesn't happen: all values remain plain.

    Is there a property that I can set that makes it behave the same way as a click? Or a statement that I should add?

    Thank you, Robert.

  2. #2
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    13,995
    Reputation
    1727
    Firstly, what technology are you using? As you mention a SetSelected method, I'm assuming that it's Windows Forms. In that case, this thread should have been created in the Windows Forms forum. Please don't be lazy and post everything in the VB.NET General Discussion forum. Post in the most appropriate forum. That way, we don't have to waste our time and yours with questions like what technology are you using. Thread moved.

  3. #3
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    13,995
    Reputation
    1727
    As for the question, I'm guessing that it is answered by the documentation:
    You can use this property to set the selection of items in a multiple-selection ListBox. To select an item in a single-selection ListBox, use the SelectedIndex property.
    So, what is theSelectionMode of your ListBox set to? If it's One then that's your answer and this demonstrates why you should ALWAYS read the documentation. It takes seconds to find it via the Help menu so there's really no reason not to.

    If the SelectionMode is set to MultiExtended or MultiSimple then there's something else at play but I'd need confirmation of that to look into it further.

  4. #4
    .NET Framework
    .NET 4.0
    Join Date
    May 2010
    Location
    Auckland, New Zealand
    Posts
    108
    Reputation
    106
    I had in fact read this help page, but by the time I'd got to the end of the example (which used SetSelected, so of course this is what I used in my code) I read " items in a multiple-selection ListBox" as simply a list of items in a listbox from which you selected one, the help didn't mention SelectionMode and I didn't make the connection. So please don't assume that I'm too lazy to read documentation, nor that the MS documentation is always crystal clear. I had in fact spent a couple of hours reading this page and others looking for the solution before I posted a question on the forum.

    But thanks for solving the problem for me.

  5. #5
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    13,995
    Reputation
    1727
    Quote Originally Posted by robertb_NZ View Post
    I read " items in a multiple-selection ListBox" as simply a list of items in a listbox from which you selected one
    I'm not sure why you would think that given that that would mean that a single-selection ListBox would be one with a single item in it. Why would that be a thing? There's no logic to that at all. Also, the ListBox documentation says this:
    A ListBox control can provide single or multiple selections using the SelectionMode property.
    It's good that you do/did read the documentation but you did a rather poor job of processing what you read on this occasion.

Similar Threads

  1. Replies: 10
    Last Post: 10-13-2016, 7:57 AM
  2. Change highlight color of selected Listbox item
    By rockola1454 in forum Listviews / Treeviews
    Replies: 2
    Last Post: 02-16-2008, 3:53 PM
  3. how can i check if an item is selected or not in a listbox?
    By ethicalhacker in forum VB.NET General Discussion
    Replies: 5
    Last Post: 12-08-2007, 8:42 PM
  4. Replies: 1
    Last Post: 12-31-2006, 11:28 AM
  5. Deselecting an already selected item in Listbox???
    By sunnypalsingh in forum VB.NET General Discussion
    Replies: 2
    Last Post: 07-27-2005, 11:03 AM

Bookmarks

Posting Permissions

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