AllAPI Network - The KPD-Team

 
Allapi Network
 API-Guide
 ApiViewer

 API List

 
API Resources
 Tips & Tricks
 VB Tutorials
 Error Lookup
 
Misc Stuff
 VB examples
 VB Tools
 VB Links
 Top Downloads
 
This Site
 Search Engine
 Contact Form
 

Donate to AllAPI.net

How to place checkboxes in a Listview?

This tip demonstrates how to add check boxes into the ListView control.

Declarations

Add the following code to the declarations section of a module.

Option Explicit
 
Public Declare Function SendMessageLong _
Lib "user32" _
Alias "SendMessageA" _
(ByVal hwnd As Long, _
ByVal Msg As Long, _
ByVal wParam As Long, _
ByVal lParam As Long) As Long
 
Public Declare Function SendMessageAny _
Lib "user32" _
Alias "SendMessageA" _
(ByVal hwnd As Long, _
ByVal Msg As Long, _
ByVal wParam As Long, _
lParam As Any) As Long
 
Public Const MAX_PATH = 260
Public Const LVM_FIRST As Long = &H1000
Public Const LVM_SETEXTENDEDLISTVIEWSTYLE _
As Long = (LVM_FIRST + 54)
Public Const LVM_GETEXTENDEDLISTVIEWSTYLE _
As Long = (LVM_FIRST + 55)
Public Const LVS_EX_FULLROWSELECT As Long = &H20
Public Const LVS_EX_GRIDLINES As Long = &H1
Public Const LVS_EX_CHECKBOXES As Long = &H4
Public Const LVM_GETITEMSTATE As Long = (LVM_FIRST + 44)
Public Const LVM_GETITEMTEXT As Long = (LVM_FIRST + 45)
Public Const LVIS_STATEIMAGEMASK As Long = &HF000
 
Public Type LV_ITEM
    mask As Long
    iItem As Long
    iSubItem As Long
    state As Long
    stateMask As Long
    pszText As String
    cchTextMax As Long
    iImage As Long
    lParam As Long
    iIndent As Long
End Type

  • Place a Listview1, Combo1 and Commandbutton1 on Form1.
  • Add one Column Header to the Listview with the text "Fonts" and set ListView1.View = lvwReport.
  • Set the Combo1 style to 2 - Dropdown List.
Form Code

Add the following code to Form1

Private Sub Form_Load()
'puts all fonts in listview1
For Ifor& = 0 To Screen.FontCount - 1
    Set itmX = ListView1.ListItems.Add(, , CStr(Screen.Fonts(Ifor&)))
Next Ifor&
'addes the checkboxes to listview1
rStyle = rStyle Or LVS_EX_CHECKBOXES
r& = SendMessageLong(ListView1.hwnd, _
LVM_SETEXTENDEDLISTVIEWSTYLE, 0&, rStyle)
End Sub
 
Private Sub Command1_Click()
Dim LI As LV_ITEM
Combo1.Clear
'looks for checked items
For i& = 0 To ListView1.ListItems.Count - 1
   r& = SendMessageLong(ListView1.hwnd, _
   LVM_GETITEMSTATE, i&, LVIS_STATEIMAGEMASK)
   'if r returns 8192 means that the checkbox is checked
    If r& = 8192 Then
      LI.cchTextMax = MAX_PATH
      LI.pszText = Space$(MAX_PATH)
      Call SendMessageAny(ListView1.hwnd, _
      LVM_GETITEMTEXT, i&, LI)
      'puts the text of the checked listitem in the combo
      Combo1.AddItem LI.pszText
   End If
Next i&
'shows the first item of the combo
If Combo1.ListCount > 0 Then Combo1.Text = _
Combo1.List(0)
End Sub

Run the app. (F5) youŽll see all fonts in the Listview with checkboxes, check some items and click on Commandbutton1, then take a look at combo1.

 

 


Copyright © 1998-2007, The Mentalis.org Team - Privacy statement
Did you find a bug on this page? Tell us!
This site is located at http://allapi.mentalis.org/