CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
+ Reply to Thread
Results 1 to 4 of 4
  1. #1
    Join Date
    Feb 2007
    Posts
    11

    Listview selected item

    I'm trying to select an item in a listview from code. However I'm having some problems. Both
    Code:
    ListView_SetItemState(ListViewHandle, 4, LVIS_SELECTED, LVIS_SELECTED)
    and
    Code:
    SelectListViewItemMessage.state = LVIS_SELECTED;
    SelectListViewItemMessage.stateMask = LVIS_SELECTED;
    SendMessage(ListViewHandle, LVM_SETITEMSTATE, (int) 4, (LPARAM) &SelectListViewItemMessage);
    don't work. However sendmessage does return TRUE.
    Mayb it is due to the style of the listview? It has the following style (there is always only 1 item selected never 0 never more than 1):
    Code:
    WS_CHILD              LVS_REPORT
    WS_VISIBLE            LVS_SINGLESEL
    WS_VSCROLL            LVS_SHOWSELALWAYS
    WS_TABSTOP            LVS_SHAREIMAGELISTS
                          LVS_OWNERDRAWFIXED
                          LVS_NOCOLUMHEADER
    ExStyle: 0x0

  2. #2
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,451

    Re: Listview selected item

    Quote Originally Posted by Henkie
    I'm trying to select an item in a listview from code. However I'm having some problems.
    First method should work.
    Second should work if you also set
    SelectListViewItemMessage.mask = LVIF_STATE;

    Stupid question - do you have at least 5 items in that list?

    <edited> Just noticed that you have an OWNERDRAW style. Then you had to draw items yourself. Do you check for the selected state?
    Last edited by VladimirF; February 23rd, 2007 at 06:17 PM.
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinViewer - an integrated GDI objects viewer for Visual C++ Debugger, and more...

  3. #3
    Join Date
    Feb 2007
    Posts
    11

    Re: Listview selected item

    Quote Originally Posted by VladimirF
    <edited> Just noticed that you have an OWNERDRAW style. Then you had to draw items yourself. Do you check for the selected state?
    No I don't, should I (and why)? What I'm trying to do is click on Items in a viewlist (from an application that I did not write). I can click on the item that has the focus with
    Code:
    SendMessage(ListViewHandle, WM_LBUTTONDBLCLK, MK_LBUTTON, 0);
    Now I need to be able to get the focus on the right item.

    After restarting Borland the first method Selects an Item (colours the box blue) but does not put the focus on it and using LVIS_FOCUSED | LVIS_SELECTED does not work either.

    Thanks in advance for the help.

  4. #4
    Join Date
    Feb 2007
    Posts
    11

    Re: Listview selected item

    I've temporarily solved it with a WM_KEYDOWN and VK_DOWN / VK_UP to scroll through the Items. This is a little bit dirty. If someone knows how to do it with ListView_SetItemState please let me know.

+ Reply to Thread

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



HTML5 Development Center

Click Here to Expand Forum to Full Width