Results 1 to 6 of 6
  1. #1
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    Serbia and Montenegro (Yugoslavia)
    Posts
    342
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Checking IsSelected in MS ListView (VB6)

    I was wondering if anyone else has figured this out. I'm using Microsoft's ListView control and need to test to see if an item (any item) is selected. If nothing is selected I get an out of range error.

    I'm writing a function that catches that error and returns a boolean returning True if anything in the listview is selected and False if there is an error. This is sloppy to say the least, but I don't think I should have to write a function for something so fundamental anyway.

  2. #2
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Checking IsSelected in MS ListView (VB6)

    ListView.Selected returns an error if nothing is selected? Can you trap the error number?

    On Error Resume Next
    myVar = ListView.Selected
    If err.Number = TBD Then
    'error handling
    err.Clear
    Else
    'you're cool
    End If
    On Error Goto 0

    If the function approach works better for your application, browsing through the object tree, it appears you could do this:

    Dim itm as ListItem
    For Each itm in ListView.ListItems
    If itm.Selected Then
    FunctionName = True
    Exit Function
    End If
    Next
    FunctionName = False

    But I didn't actually test it. Hope this helps.

  3. #3
    WS Lounge VIP rory's Avatar
    Join Date
    Dec 2000
    Location
    Burwash, East Sussex, United Kingdom
    Posts
    6,280
    Thanks
    3
    Thanked 191 Times in 177 Posts

    Re: Checking IsSelected in MS ListView (VB6)

    Hi Mike,
    This is shamelessly nicked from the MSDN library - hope it helps:
    Dim item As ListItem
    Set item = lvwFiles.SelectedItem
    If item Is Nothing Then
    Regards,
    Rory

    Microsoft MVP - Excel

  4. #4
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    Serbia and Montenegro (Yugoslavia)
    Posts
    342
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Checking IsSelected in MS ListView (VB6)

    Bruce,

    I wanted to thank you for all of this. This was very clear and it did exactly what I needed. Someone should write a book like "API calls made simple" or API for Visual Basic Programmers" or something. Oh, I did have to change SendMessageLong to just SendMessage, but then it worked.

    I don't know if you've had any expreience with this, but I'm trying to enable/disable certain buttons when items are selected/unselected in the lvw. The OnClick works fine with your code, but on the LostFocus event, it seems to default to the first item, though it does not show selected. I put the code in the _Validate event, which works, but seems like it is sloppy.

    Mike

  5. #5
    Star Lounger
    Join Date
    Aug 2001
    Location
    GA
    Posts
    52
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Checking IsSelected in MS ListView (VB6)

    <....I don't think I should have to write a function for something so fundamental anyway. >
    Ahhhh, the innocence of those seeking to use a Microsoft control in VB/VBA.....

    Here's my suggestions based on my experience of using the MS Listview control (and TreeView control)...
    I suggest you use a call to the API SendMessage to get this information. Once you have set up some constants and declarations, a single line of code will let you know not only if there are any selected items but also the number of items selected. Also the API is blazing fast compared to using the OCX control itself.

    Here's how it goes:
    Put the following in the delcarations section of your form.
    '================================================= ===
    Private Const LVM_FIRST As Long = &H1000
    Private Const LVM_GETITEMCOUNT As Long = (LVM_FIRST + 4)
    Private Const LVM_GETSELECTEDCOUNT As Long = (LVM_FIRST + 50)

    Private Declare Function SendMessage _
    Lib "User32" Alias "SendMessageA" _
    (ByVal hwnd As Long, _
    ByVal wMsg As Long, _
    ByVal wParam As Long, _
    lParam As Any) As Long
    '================================================= ===

    Next create a function that calls the API SendMessage to your ListView Control. Maybe something like this added to the form. ( the example uses Xlvw as the name of the listview control):

    Private Function GetLvwSelectedCnt() As Long
    Dim lngSelectedCount As Long
    ' Don't forget to change the lvw control name (Xlvw) in
    ' the SendMessage call to your lvw control's name.
    ' Get the total number of items selected in the listview by passing the listview's window handle.
    lngSelectedCount = SendMessage(Xlvw.hwnd, LVM_GETSELECTEDCOUNT, 0, 0)
    GetLvwSelectedCnt = lngSelectedCount
    End Function

    Now you can use this function somewhere in your code.

    Dim lngSelCount as Long
    lngSelCount = GetLvwSelectedCnt

    IF lngSelCount > 0 then
    'We have at least one item selected. do stuff here with the selection.
    ' if you need the count of the selected you now have that answer too in the lngSelCount variable.
    ELSE
    ' There are no items selected. do stuff here for that case. Like
    ' maybe enable/disable command buttons, menu items, or what ever....
    END IF

    HERE IS AN ADDED BONUS FOR HAVING SET UP THE ABOVE API CALL.
    Its all down hill from here to get other info from the Listview control.
    You may need to know the number of items in the listview control. (maybe to see if user selected ALL items in the listview?)

    By changing the constant passed to the API you can get the number of items in the listview control. Add this function to your form, like this;

    Private Function GetLvwItemCnt() As Long
    Dim lngItemCount As Long
    'Don't forget to change the lvw control name (Xlvw) in the
    ' SendMessage call to your lvw control's name.
    'Get the total number of items in the listview control by passing the LVM_GETITEMCOUNT constant
    lngItemCount = SendMessage(Xlvw.hwnd, LVM_GETITEMCOUNT, 0, 0)
    GetLvwItemCnt = lngItemCount
    End Function

    This is only the begining of what you can do with the listview control making API calls.
    Other things like adding an bmp next to the text of a column header.(to show which column is sorted).
    Highlighting the entire row of a selected item in reports view.
    Need to allow a user to sort a column that contains date values??? hmmm...
    Want to control Icon "Run away" when user drags an item near the edge of the listview contol when in Icon view??

    To have full control of a ListView control, the API is the way to go. If you look at MSDN you will find the most info about the Listview control and its constants. <A target="_blank" HREF=http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/CommCtls/ListView/ListView.asp>http://msdn.microsoft.com/library/default....ew/ListView.asp</A>

    Also check MSDN's C++ area. I know nothing about C++ but was able to figure out what i needed to make things work for me in my Access database. I wanted the robust features of the ListView (and TreeView) control that you see Microsoft use in their applications. I've waded through tens of web sites. Everyone has the basic fill a listview control example but finding solid examples (like drag/drop items, column sorts, drag/drop columns) is tuff.

    Later,
    Bruce

  6. #6
    Star Lounger
    Join Date
    Aug 2001
    Location
    GA
    Posts
    52
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Checking IsSelected in MS ListView (VB6)

    A small 'oopps' on the SendMessageLong. I went back and updated my original post with the correction to SendMessage.

    Any ways, about toggling button. In my Application I created a sub procedure that does the dirty work for toggling the Menu state base on user's selection and the control's current view style (these could just as well be command buttons on a form that need to be enabled/disabled). I have items on the menu and submenu toggled as enabled/disabled, visible/not visible, button icon or text, depending on what the user selects in the listview control and/or the current ListViews style (ie List, Detail view).

    I then call this sub procedure in three places.
    1. ListView_Click event (this may be what you referred to as the _OnClick in your post)
    User clicks in the control but not on an item.
    2. ListView_ItemClick event
    User clicks on an item or is dragging a marquee(window) around several items.
    3. Form_Open event
    Initialize your command buttons. Depends on your application.
    What ever happens on Form_Open, at the bottom, make a call to the
    sub that enables/disables your command buttons.

    This is all you need. I can't think of any reason to be using the LostFocus event to toggle command buttons. One of the above ListView events will have occurred before the LostFocus so no need to do stuff on LostFocus related to enabling command buttons.

    Here is an Example: This builds on my previous post in this thread about using the API to get the listview item count and listview selected count.

    Lets say you have a command button called cmdDelete. It has code that will delete the selected items in the Listview control when clicked.
    And another command button called cmdSelectALL. It has code that does something silly like selecting ALL items in the listview control.
    And as an added bonus, a textbox called txtListMsg just under your Listview control and set its width to the same as your listview control. Set the textbox as locked. We'll use this to let the user know how many items are in the listview control and the number of items they selected. Kinda' like Windows Explorer does when you select files from a folder. Very helpful if the items count/selection is high. Makes user feel warm and fuzzy and doesn't cost us anything.

    Create a sub like this in your form's code. Only 5 line of code, the rest is comments.
    Private Sub FormCmdButtonInit()
    ' Toggles the command buttons on form based on Listview item selection.
    DIM lngSelectedCount as long ' Count of selected items in the listview control
    DIM lngTotItemCount as long ' Count of ALL items in the listview control

    lngSelectedCount = GetLvwSelectedCnt
    lngTotItemCount = GetLvwItemCnt

    ' For the Delete button ==========================
    ' If at least one item is selected, enable the Delete Button so user can choose to delete them,
    ' otherwise disable the button (or stupid user will try and click it even though they haven't selected
    ' any items to delete).
    cmdDelete.Enabled = (lngSelectedCount > 0)

    ' For the Select ALL button ==========================
    ' If the listview does not contain any items the Select ALL button will be disabled.
    ' If the user has already selected all items in the listview control disable this button
    ' so the user is not confused in thinking there may be more items than are in view.
    ' If anything less the all items selected, enable the
    ' button so user can choose to click it to select all items in the listview control.
    ' (this is only an example. In a real app this would be a stupid button
    ' to have with a list view control. As a menu or submenu item, it would be acceptable)
    cmdSelectALL.Enabled = (lngSelectedCount <> lngTotItemCount) AND (lngTotItemCount >0)

    ' Make the user feel warm and fuzzy.
    ' Give the user some feed back about what they did or didn't select. At this point its free information.
    Me.txtListMsg.Value = " Items Selected: " & lngSelectedCount & " of " & lngTotItemCount

    ' You can also manipulate the form's menu bar or sub-menu items here.
    ' Whatever else you need to do based on the users selection/no selection.
    ' Listviews current style or sort order.....

    END SUB

    Now all you have to do is call this function in the 3 events mentioned above.
    Your form open would be something like this
    Private Sub Form_Open()
    ' Your code goes here, do this and that....
    ' Do stuff here to fill listview control and initialize other controls and stuff.
    ' Maybe make sure nothing is selected by default in the listview, or maybe ensure
    ' at least the first item is selected in the control when form first opens. Whatever...works for your app.

    ' Last thing to do is toggle your command buttons.
    Call FormCmdButtonInit

    END SUB

    Then, in your ListView_Click event and the Listview_ItemClick event add a call to the FormCmdButtonInit at the bottom of any other code you may have for these events similar to the Form_Open event.

    Now, as the user selects one or more items, or unselects them all, your command buttons will toggle and help keep the user out of trouble..


    Hope this helps...
    Later
    Bruce

Posting Permissions

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