Selecting a ListItem via API


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: Selecting a ListItem via API

  1. #1
    Damit Senanayake Guest

    Selecting a ListItem via API

    Env: Win2k SP1, VB5 SP3, IE 5.5 (VS.NET b1 also installed, but I think this
    is not really relevant)

    Does anyone know how to do this (selecting an item) properly? I have tried
    (in a UserControl):

    Public Property Let Selected(ByVal NewVal As Boolean)
    m_ListItem.State = m_ListItem.State Xor LVIS_SELECTED
    m_ListItem.stateMask = LVIS_SELECTED
    rc = SendMessage(PtrToCtl(m_LV).ListViewHandle, LVM_SETITEMSTATE, m_Index,
    m_ListItem)
    End Property

    but this is not working (the item is not selected)

    However, getting the selection state works:

    Public Property Get Selected() As Boolean
    Selected = SendMessage(PtrToCtl(m_LV).ListViewHandle, LVM_GETITEMSTATE,
    m_Index, ByVal LVIS_SELECTED)
    End Property

    and checking/unchecking items works:

    Public Property Get Checked() As Boolean
    Checked = RightShift(SendMessage(PtrToCtl(m_LV).ListViewHandle,
    LVM_GETITEMSTATE, m_Index, _
    ByVal LVIS_STATEIMAGEMASK), 12) - 1
    End Property

    Public Property Let Checked(ByVal NewVal As Boolean)
    m_ListItem.State = m_ListItem.State Xor INDEXTOSTATEIMAGELIST(IIf(NewVal =
    True, 2, 1))
    m_ListItem.stateMask = LVIS_STATEIMAGEMASK
    SendMessage PtrToCtl(m_LV).ListViewHandle, LVM_SETITEMSTATE, m_Index,
    m_ListItem
    End Property

    As I (obviously) can't distribute a ListView which doesn't select items, any
    help would be appreciated...
    --
    Damit Senanayake | damit@mvps.org | http://vulcan.spaceports.com/~damit/
    Please reply to newsgroups, not by e-mail. | ICQ: 6930718



  2. #2
    Mattias Sjögren Guest

    Re: Selecting a ListItem via API

    Hi Damit,

    >Does anyone know how to do this (selecting an item) properly? I have tried
    >(in a UserControl):


    Here's how I'd do it:

    Public Property Let Selected(ByVal NewVal As Boolean)
    If NewVal Then
    m_ListItem.State = LVIS_SELECTED
    Else
    m_ListItem.State = 0
    End If
    m_ListItem.stateMask = LVIS_SELECTED
    rc = SendMessage(PtrToCtl(m_LV).ListViewHandle, LVM_SETITEMSTATE,
    m_Index, m_ListItem)
    End Property

    or, if you don't want to lose the other bits in m_ListItem.State, like
    this:

    If NewVal Then
    m_ListItem.State = m_ListItem.State Or LVIS_SELECTED
    Else
    m_ListItem.State = m_ListItem.State And Not LVIS_SELECTED
    End If


    Matt

    ============================================
    Mattias Sjögren - mattiass @ hem.passagen.se

    CodeHound - The Software Developer's Search Engine
    http://www.codehound.com

  3. #3
    Mattias Sjögren Guest

    Re: Selecting a ListItem via API

    Hi Damit,

    >Does anyone know how to do this (selecting an item) properly? I have tried
    >(in a UserControl):


    Here's how I'd do it:

    Public Property Let Selected(ByVal NewVal As Boolean)
    If NewVal Then
    m_ListItem.State = LVIS_SELECTED
    Else
    m_ListItem.State = 0
    End If
    m_ListItem.stateMask = LVIS_SELECTED
    rc = SendMessage(PtrToCtl(m_LV).ListViewHandle, LVM_SETITEMSTATE,
    m_Index, m_ListItem)
    End Property

    or, if you don't want to lose the other bits in m_ListItem.State, like
    this:

    If NewVal Then
    m_ListItem.State = m_ListItem.State Or LVIS_SELECTED
    Else
    m_ListItem.State = m_ListItem.State And Not LVIS_SELECTED
    End If


    Matt

    ============================================
    Mattias Sjögren - mattiass @ hem.passagen.se

    CodeHound - The Software Developer's Search Engine
    http://www.codehound.com

  4. #4
    Damit Senanayake Guest

    Re: Selecting a ListItem via API

    Hi Mattias,

    : Public Property Let Selected(ByVal NewVal As Boolean)
    : Dim rc As Long
    : If NewVal Then
    : m_ListItem.State = m_ListItem.State Or LVIS_SELECTED
    : Else
    : m_ListItem.State = m_ListItem.State And Not LVIS_SELECTED
    : End If
    : m_ListItem.stateMask = LVIS_SELECTED
    : rc = SendMessage(PtrToCtl(m_LV).ListViewHandle, LVM_SETITEMSTATE,
    : m_Index, m_ListItem)
    : End Property

    Thanks for the code, I had forgotten about this approach even though I used
    it for the window styles (sheepish grin), but...

    I looked through the docs [again] and (finally) came up with this paragraph,
    which explained everything.... BTW this was in the ListView_SetItemState
    (the macro form of LVM_SETITEMSTATE) docs:

    LVIS_SELECTED
    The item is selected. The appearance of a selected item depends on
    whether it has the focus and also on the system colors used for selection.
    Items will only show as selected if the list view control has focus or the
    LVS_SHOWSELALWAYS style is used.


    Interestingly, does LVIS_ACTIVATING do anything at all?
    --
    Damit Senanayake | damit@mvps.org | http://vulcan.spaceports.com/~damit/
    Please reply to newsgroups, not by e-mail. | ICQ: 6930718



  5. #5
    Damit Senanayake Guest

    Re: Selecting a ListItem via API

    Hi Mattias,

    : Public Property Let Selected(ByVal NewVal As Boolean)
    : Dim rc As Long
    : If NewVal Then
    : m_ListItem.State = m_ListItem.State Or LVIS_SELECTED
    : Else
    : m_ListItem.State = m_ListItem.State And Not LVIS_SELECTED
    : End If
    : m_ListItem.stateMask = LVIS_SELECTED
    : rc = SendMessage(PtrToCtl(m_LV).ListViewHandle, LVM_SETITEMSTATE,
    : m_Index, m_ListItem)
    : End Property

    Thanks for the code, I had forgotten about this approach even though I used
    it for the window styles (sheepish grin), but...

    I looked through the docs [again] and (finally) came up with this paragraph,
    which explained everything.... BTW this was in the ListView_SetItemState
    (the macro form of LVM_SETITEMSTATE) docs:

    LVIS_SELECTED
    The item is selected. The appearance of a selected item depends on
    whether it has the focus and also on the system colors used for selection.
    Items will only show as selected if the list view control has focus or the
    LVS_SHOWSELALWAYS style is used.


    Interestingly, does LVIS_ACTIVATING do anything at all?
    --
    Damit Senanayake | damit@mvps.org | http://vulcan.spaceports.com/~damit/
    Please reply to newsgroups, not by e-mail. | ICQ: 6930718



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
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center