Results 1 to 5 of 5

Thread: [RESOLVED] ListBox SelectedIndex in C#

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2010
    Posts
    1,103

    Resolved [RESOLVED] ListBox SelectedIndex in C#

    In .NET, ListBox has a very good feature to de-select all by .SelectedIndex =-1

    From what I know, VB6's Listbox ListIndex just set or return Focus (not Selected), it is totally differ to .Net SelectedIndex.

    Right now I coded .NET SelectedIndex = -1 in VB6 by two steps:
    lstGoods.ListIndex = -1 'Get rid of Focus at first
    Call SendMessage(lstGoods.hWnd, LB_SETSEL, 0, ByVal -1) 'De-select all

    Question 1:
    Is my above code for lstGoods.SelectedIndex = -1 right?

    Question 2:
    How to achieve the same thing for:
    C#:
    this.lstGoods().TopIndex = this.lstGoods().SelectedIndex
    Last edited by Jonney; Jan 13th, 2015 at 09:46 AM.

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

    Re: ListBox SelectedIndex in C#

    #1: Yes. Here's the documentation. You can always look up the window message on MSDN to answer the question yourself. LB_SETSEL is used for multi-select listboxes. A single-select would just need the ListIndex = -1
    Parameters
    wParam
    Specifies how to set the selection. If this parameter is TRUE, the item is selected and highlighted; if it is FALSE, the highlight is removed and the item is no longer selected.
    lParam
    Specifies the zero-based index of the item to set. If this parameter is –1, the selection is added to or removed from all items, depending on the value of wParam, and no scrolling occurs.
    #2. VB's ListBox also has a TopIindex property
    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}

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2010
    Posts
    1,103

    Re: ListBox SelectedIndex in C#

    Quote Originally Posted by LaVolpe View Post
    #1: Yes. Here's the documentation. You can always look up the window message on MSDN to answer the question yourself. LB_SETSEL is used for multi-select listboxes. A single-select would just need the ListIndex = -1


    #2. VB's ListBox also has a TopIindex property
    Thanks. I forgot to look for MSDN but VBForums.

    The problem is that SendMessage only works when no scrolling. If I didn't set lstGoods.ListIndex = -1 then SendMessage doesn't work.
    An annoying focus remained after deselect all which is not what I want. C# Listbox doesn't have focus rect but just selected highlight.

    One more question, ListBox/ComboBox have ugly plain selection highlight RECT. how to own-drawn Listbox selection with Themed selection like Win7 explorer? I don't know what parameter I should put for OpenThemeData or there's no such theme?

    Edited:
    If ListBox's MultiSelection is None,there's has focus. But If MultiSelection is Simple or Extend,there's no focus after deselect all.
    Attached Images Attached Images  
    Last edited by Jonney; Jan 13th, 2015 at 08:26 PM.

  4. #4
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: ListBox SelectedIndex in C#

    Which language are you using? I assume it is VB6 but the way the originally post is written it is a bit unclear.
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2010
    Posts
    1,103

    Re: ListBox SelectedIndex in C#

    Quote Originally Posted by Nightwalker83 View Post
    Which language are you using? I assume it is VB6 but the way the originally post is written it is a bit unclear.
    VB6.
    I wrote a small project in C#, but now I want to wrote same thing in VB6 with some enhancement. But look like VB6 is harder. I have to look for APIs.

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