Results 1 to 6 of 6

Thread: [RESOLVED] listbox - selectionmode = none issues

  1. #1

    Thread Starter
    PowerPoster BruceG's Avatar
    Join Date
    May 2000
    Location
    New Jersey (USA)
    Posts
    2,657

    Resolved [RESOLVED] listbox - selectionmode = none issues

    Hello,
    I would like to have a listbox where I can pre-select an item in code, but prevent the user from selecting a different item (i.e. a "read-only" listbox). I don't want to disable the listbox, because then the user can't scroll it. If I set selectionmode = none, then I can't select an item in code. Any ideas on how I can achieve the results I am looking for here?
    Thanks,
    Bruce
    Last edited by BruceG; Apr 5th, 2009 at 01:21 PM. Reason: resolved
    "It's cold gin time again ..."

    Check out my website here.

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: listbox - selectionmode = none issues

    try this. pre-select your item , then set selectionmode in code

  3. #3

    Thread Starter
    PowerPoster BruceG's Avatar
    Join Date
    May 2000
    Location
    New Jersey (USA)
    Posts
    2,657

    Re: listbox - selectionmode = none issues

    Thanks, .paul., but unfortunately that does not work. What happens is, when I set selectionmode = none in code, the item that I previously selected in code is de-selected.
    "It's cold gin time again ..."

    Check out my website here.

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: listbox - selectionmode = none issues

    ok. this works. its an extended listbox that can only have its selectedindex changed in code:

    vb Code:
    1. Public Class listboxEx
    2.     Inherits ListBox
    3.  
    4.     Private Const WM_LBUTTONDOWN As Integer = &H201
    5.     Private Const WM_LBUTTONUP As Integer = &H202
    6.     Private Const WM_KEYDOWN As Integer = &H100
    7.     Private Const WM_KEYUP As Integer = &H101
    8.  
    9.     Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
    10.         If m.Msg = WM_LBUTTONDOWN OrElse m.Msg = WM_LBUTTONUP OrElse m.Msg = WM_KEYDOWN OrElse m.Msg = WM_KEYUP Then Return
    11.         MyBase.WndProc(m)
    12.     End Sub
    13.  
    14. End Class
    Last edited by .paul.; Apr 5th, 2009 at 12:10 PM.

  5. #5
    Fanatic Member Vectris's Avatar
    Join Date
    Dec 2008
    Location
    USA
    Posts
    941

    Re: listbox - selectionmode = none issues

    You could just add a selection code on the index changed event

    Code:
    ListBox1.SelectedIndex = 1
    So every time they change it, it instantly changes back.
    If your problem is solved, click the Thread Tools button at the top and mark your topic as Resolved!

    If someone helped you out, click the button on their post and leave them a comment to let them know they did a good job

    __________________
    My Vb.Net CodeBank Submissions:
    Microsoft Calculator Clone
    Custom TextBox Restrictions
    Get the Text inbetween HTML Tags (or two words)

  6. #6

    Thread Starter
    PowerPoster BruceG's Avatar
    Join Date
    May 2000
    Location
    New Jersey (USA)
    Posts
    2,657

    Re: listbox - selectionmode = none issues

    .paul.,
    Very nice, I must say. I tried it out and it works great. I see you added it to your bag of tricks in your signature, as well you should. I think this little gem fills a need. Thanks very much!

    Vectris - thanks for your input. I did the kind of thing you are talking about in VB6, but was hoping for a more elegant solution in .NET. As we can see, .NET still does not offer the desired functionality "out of the box", but .paul.'s extension gets the job done nicely.
    "It's cold gin time again ..."

    Check out my website here.

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