WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

EXAMPLES FROM USERS

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

How to Query Checkbox in ListView Control


 keywords: Checkbox ListView LVGetItemState 
I needed to toggle the state of Networking Tab items. the dialog contains a ListView control with checkboxes. You can apply this same idea to any standard ListView control with checkboxes though.

OK.. so you've used RoboScripter or something to get a handle on a listview window that has CheckBoxes - like the "Local Area Connection Properties" in XP for your network card. You already know that you can pass a keypress (SpaceBar) to the currently selected item to toggle the state of the CheckBox, but RoboScripter doesn't actually have any way to let you query what state it is currently in! ... So how do you know if you should send a spacebar keypress or not?

Well, by using one of my old API UDFs for ListView controls, you can see the state of the item in question, including it's checkbox image. This is tested and works fine in XP (SP2) and should work in Vista 32bit bit I think but I haven't got it so don't know for sure

Developer: IFICantBYTE

;Open your "Local Area Connection Properties" window and then run the following example

GoSub DEFINEUDF ;Define the ListView UDF at the bottom of the script - see comments down there to make code smaller if you like.
Message("OK - are you ready?","For this example, have your Local Area Connection Properties window open and try ticking on or off a couple of things (don't OK it though!)")

:START

;This first bit was made by RoboScripter so it's probably something similar to the script that you already have...

;RoboScripter
; Made with
;   RoboScripter ver: 45
;   CtlMgr ver: 44037
AddExtender("wwctl44i.dll")


;Title: <untitled>
;ID: 16012
;Class: SysListView32
;Level: 3

; Default cWndByWndSpec seems OK here
window1=cWndByWndSpec("#32770","explorer",6,0,1,2,12321,9,12320)
window2=cWndbyid(window1,0)
ControlHandle=cWndbyid(window2,16012)
;ControlHandle contains control handle       ; TODO - Add code


;Title: <untitled>
;ID: 16012
;Class: SysListView32
;Level: 3

; Default cWndByWndSpec seems OK here
window1=cWndByWndSpec("#32770","explorer",6,0,1,2,12321,9,12320)
window2=cWndbyid(window1,0)
ControlHandle=cWndbyid(window2,16012)
result=cGetLvText(ControlHandle)     ;Get first column of SysLsitView32 text
;Message("LV Text",StrReplace(result,@tab,@crlf))     ;TODO - Remove Message.  Add code

;========================================================

; OK - that's the RoboScript code done... now we have a handle on the ListView control that we want to look at and a text list of the items in it
; Lets use the ListView UDF to query the checkbox state and change the list to show our results...


For ItemX = 1 To ItemCount(result,@TAB) ; loop through the items found
ItemXText = ItemExtract(ItemX,result,@TAB) ; get the text

ItemXState = LVGetItemState(ControlHandle,ItemX-1) ; This is the line that uses the ListView UDF to look at the item's State value

;Message(ItemXState,ItemXState|4096)

If ItemXState == (ItemXState|4096) ;4096 = 1000Hex ....seems to be the value for the checkbox state (It needs the LVIS_STATEIMAGEMASK to be set when querying)
   ;No it's not checked ... so do something about that here....
   result = ItemReplace(StrCat(ItemXText,"   ","Returned state value = ",ItemXState,"  So CheckBox is CLEAR"),ItemX,result,@TAB) ; we will just change our returned list to show the status
Else
   ;Yes it's checked
   result = ItemReplace(StrCat(ItemXText,"   ","Returned state value = ",ItemXState,"  So CheckBox is TICKED"),ItemX,result,@TAB)
EndIf

Next ItemX


;OK - lets display or text with the results...

Message("LV Text and Status results",StrReplace(result,@TAB,@CRLF))     ;TODO - Remove Message.  Add code

Answer = AskYesNo("Try again?","Do you want to change a few CheckBoxes and look again?%@CRLF%Remember to CANCEL your changes in the window when closing it!")
If Answer == @YES Then Goto START

Exit
;=================================
;===== MAIN SCRIPT ENDS HERE =====
;=================================




:DEFINEUDF ; jumped to from GoSub at top of script

; You can cut the size of this down massivly if you like to just include the DllCall line with the relevant values you need,
; - it doesn't even need to be a UDF - it could be a single line in the rest of your code - I just kept it intact as it was
; when I originally wrote it so that it is a bit easier to understand and use for other purposes.


;-----------------------------------------------------------------------------------------------------------;
;LVGetItemState : Retrieves the state of a ListView item                                                    ;
;v1.1 by IFICantBYTE (26 July 2003)                                                                         ;
;-----------------------------------------------------------------------------------------------------------;
;hListView      : ListView handle                                                                           ;
;iItem          : Index of the Item                                                                         ;
;-----------------------------------------------------------------------------------------------------------;
;Returns        : BitMask value of the Item's state. (see LVIS Windows API values)                          ;
;                 eg: it will return 1 if the item has the FOCUS                                            ;
;                     it will return 2 if the item is SELECTED                                              ;
;                     it will return 3 if the item has the FOCUS AND it is SELECTED ( 1 | 2 = 3)            ;
;                     Note: LVIS_OVERLAYMASK and LVIS_STATEIMAGEMASK actually return the                    ;
;                           One based item's overlay or state image index in the associated imagelist.      ;
;                           (Bits 12 through 15 specify the one-based index of an image in the control's    ;
;                            state image list. The state image is displayed next to an item's icon to       ;
;                            indicate an application-defined state. If these bits are zero, the item has    ;
;                            no state image)                                                                ;
; - Note: you could set the MASK to be any single or combination of the LVIS values so it wouldn't return   ;
;         the values of any other states, and would return just a 0 if the one(s) in question were not set. ;
;         (except for OVERLAYMASK and STATEIMAGEMASK - they return different info - see above)              ;
;         However, it is probably more useful to get everything in one go and then just use a logical OR    ;
;         on the result to determine the status of a state....                                              ;
;   - eg: the following would see if an item was selected or not:                                           ;
;         StateOfItem = LVGetItemState(hListView,iItem)                                                      ;
;         If StateOfItem == (StateOfItem|2)                                                                  ;
;            Message(StateOfItem,"Item IS Selected")                                                         ;
;         Else                                                                                               ;
;            Message(StateOfItem,"Item is NOT selected")                                                     ;
;         EndIf                                                                                              ;
;-----------------------------------------------------------------------------------------------------------;
#DefineFunction LVGetItemState(hListView,iItem)
user32=StrCat(DirWindows(1),"user32.dll")
LVM_FIRST = 4096
LVM_GETITEMSTATE = LVM_FIRST + 44
LVIS_CUT = 4
LVIS_DROPHILITED = 8
LVIS_FOCUSED = 1
LVIS_SELECTED = 2
LVIS_OVERLAYMASK = 3840 ;returns imagelist index bits
LVIS_STATEIMAGEMASK = 61440 ;returns imagelist index bits - seems to be used by checkboxes ... 4096 = checked on (for standard tick - haven't tested on non-standard like grey 3 state kind yet)
MASK = LVIS_CUT | LVIS_DROPHILITED | LVIS_FOCUSED | LVIS_SELECTED | LVIS_OVERLAYMASK | LVIS_STATEIMAGEMASK  ; tell it to return any of these values if they are set on the item in question
Return DllCall(user32,long:"SendMessageA",long:hListView,long:LVM_GETITEMSTATE,long:iItem,long:MASK)
#EndFunction
;##########################################################################################################################################


Return ; Return to the calling GoSub at the top of script

Article ID:   W17565
Filename:   How to Query Checkbox in ListView Control.txt
File Created: 2008:11:25:13:00:04
Last Updated: 2008:11:25:13:00:04