Bay Six Software Forum Index Bay Six Software
Beyond the Basics
 
 FAQFAQ   SearchSearch   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

ListView with CheckBoxes (Select&Unselect Control)

 
Post new topic   Reply to topic    Bay Six Software Forum Index -> General Discussions
View previous topic :: View next topic  
Author Message
Petrus
Junior Member


Joined: 12 Jul 2014
Posts: 13
Location: Barcelona - Spain

PostPosted: Aug 29th, 2014, 11:44am    Post subject: ListView with CheckBoxes (Select&Unselect Control) Reply with quote

Hi there,

I want to create a sub to select all checkboxes of the listview at once, and another for unselect at once too, but I have not succeeded yet.

Can someone help me please ?

The source program is as follows:


'ListView Report Example
'by Brent D. Thorn, 7/2002
NoMainWin
Call InitCommonControls
Open "ListView Report Example" For Window As #1
#1 "TrapClose [Quit]"
'set style = LVS_REPORT + LVS_SINGLESEL +
' LVS_SHOWSELALWAYS+ LVS_NOSORTHEADER
dwStyle = _WS_CHILD + _WS_VISIBLE + 1 + 4 + 8 + 32768
hLV = CreateListView(_WS_EX_CLIENTEDGE, _
dwStyle, 0, 0, 175, 150, hWnd(#1), 0)
Call InsertColumn hLV, 0, "English", 75
Call InsertColumn hLV, 1, "Español", 75
Restore [Data]
Read E$, S$: I = 0
While E$ <> "EOD"
Call InsertItem hLV, I, E$
Call SetItem hLV, I, 1, S$
Read E$, S$: I = I + 1
Wend
Wait
[Quit]
Close #1
End
[Data]
Data "One", "Uno", "Two", "Dos", "Three", "Tres"
Data "Four", "Cuatro", "Five", "Cinco"
Data "Six", "Seis", "Seven", "Siete", "Eight", "Ocho"
Data "Nine", "Nueve", "Ten", "Diez"
Data "EOD", "EOD"
Sub InitCommonControls
struct LVCOLUMN, _
mask As ulong, _
fmt As long, _
cx As long, _
pszText As ptr, _
cchTextMax As long, _
iSubItem As long, _
iImage As long, _
iOrder As long
struct LVITEM, _
mask As ulong, _
iItem As long, _
iSubItem As long, _
state As ulong, _
stateMask As ulong, _
pszText As ptr, _
cchTextMax As long, _
iImage As long, _
lParam As long, _
iIndent As long
End Sub
'The next line wraps and will need to be put back together
Function CreateListView(dwExStyle, dwStyle, x, y, nWidth, nHeight,hWndParent, nID)
CallDLL #user32, "GetWindowLongA", _
hWndParent As long, _
_GWL_HINSTANCE As long, _
hInst As long
CallDLL #user32, "CreateWindowExA", _
dwExStyle As long, _
"SysListView32" As ptr, _
0 As long, _
dwStyle As long, _
x As long, _
y As long, _
nWidth As long, _
nHeight As long, _
hWndParent As long, _
nID As long, _
hInst As long, _
0 As long, _
CreateListView As long
End Function
Sub InsertColumn hLV, iCol, Text$, cx
LVCOLUMN.mask.struct = 2 + 4 'LVCF_WIDTH + LVCF_TEXT
LVCOLUMN.cx.struct = cx
LVCOLUMN.pszText.struct = Text$
CallDLL #user32, "SendMessageA", _
hLV As long, _
4123 As long, _ 'LVM_INSERTCOLUMN
iCol As long, _
LVCOLUMN As struct, _
r As long
End Sub
Sub InsertItem hLV, iItem, Text$
LVITEM.mask.struct = 1 'LVIF_TEXT
LVITEM.iItem.struct = iItem
LVITEM.iSubItem.struct = 0
LVITEM.pszText.struct = Text$
CallDLL #user32, "SendMessageA", _
hLV As long, _
4103 As long, _ 'LVM_INSERTITEM
0 As long, _
LVITEM As struct, _
r As long
End Sub
Sub SetItem hLV, iItem, iSubItem, Text$
LVITEM.mask.struct = 1 'LVIF_TEXT
LVITEM.iItem.struct = iItem
LVITEM.iSubItem.struct = iSubItem
LVITEM.pszText.struct = Text$
CallDLL #user32, "SendMessageA", _
hLV As long, _
4102 As long, _ 'LVM_SETITEM
0 As long, _
LVITEM As struct, _
r As long
End Sub
Back to top
View user's profile Send private message Send e-mail
Brent
Site Admin


Joined: 01 Jul 2005
Posts: 797

PostPosted: Aug 29th, 2014, 5:50pm    Post subject: Re: ListView with CheckBoxes (Select&Unselect Control) Reply with quote

Hello Pedro,

I am a little puzzled by your question. Apparently others feel the same because of the lack of responses on the LB Conforums where you cross-posted this question. Your subject and the demo code you posted seem to have no relation to one another because the demo shows no check boxes.

The following demo is my guess as to what you want. Please let me know if I am wrong.
Code:
  NoMainWin
  Call InitCommonControls
  Button #1.btnChecked, "Check All", [CheckAll], UL, 185, 10
  Open "ListView Check Demo" For Window As #1
  #1 "TrapClose [Quit]"
  dwStyle = _WS_CHILD + _WS_VISIBLE + _
        1 + 4 + 8 + 32768 'LVS_REPORT + LVS_SINGLESEL + LVS_SHOWSELALWAYS + LVS_NOSORTHEADER
  hLV = CreateListView(_WS_EX_CLIENTEDGE, dwStyle, 0, 0, 175, 150, hWnd(#1), 0)
  dwStyle = 4 + _ 'LVS_EX_CHECKBOXES
            32    'LVS_EX_FULLROWSELECT
  Call SetExtendedStyle hLV, dwStyle
  Call InsertColumn hLV, 0, "Numbers", 150
  Restore [Data]
  Read N$: I = 0
  While N$ <> "EOD"
      Call InsertItem hLV, I, N$
      Read N$: I = I + 1
  Wend
  Wait
  [Quit]
  Close #1
  End
  [Data]
  Data "One", "Two", "Three", "Four", "Five"
  Data "Six", "Seven", "Eight", "Nine", "Ten"
  Data "EOD"
[CheckAll]
    Call SetChecked hLV, -1, 1
    Wait
  Sub InitCommonControls
      struct LVCOLUMN, _
          mask As ulong, _
          fmt As long, _
          cx As long, _
          pszText As ptr, _
          cchTextMax As long, _
          iSubItem As long, _
          iImage As long, _
          iOrder As long
      struct LVITEM, _
          mask As ulong, _
          iItem As long, _
          iSubItem As long, _
          state As ulong, _
          stateMask As ulong, _
          pszText As ptr, _
          cchTextMax As long, _
          iImage As long, _
          lParam As long, _
          iIndent As long
  End Sub
  Function CreateListView( dwExStyle, dwStyle, x, y, nWidth, nHeight, hWndParent, nID )
      CallDLL #user32, "GetWindowLongA", _
          hWndParent As long, _
          _GWL_HINSTANCE As long, _
          hInst As long
      CallDLL #user32, "CreateWindowExA", _
          dwExStyle As long, _
          "SysListView32" As ptr, _
          0 As long, _
          dwStyle As long, _
          x As long, _
          y As long, _
          nWidth As long, _
          nHeight As long, _
          hWndParent As long, _
          nID As long, _
          hInst As long, _
          0 As long, _
          CreateListView As long
  End Function
  Sub InsertColumn hLV, iCol, Text$, cx
      LVCOLUMN.mask.struct = 2 + 4 'LVCF_WIDTH + LVCF_TEXT
      LVCOLUMN.cx.struct = cx
      LVCOLUMN.pszText.struct = Text$
      CallDLL #user32, "SendMessageA", _
          hLV As long, _
          4123 As long, _ 'LVM_INSERTCOLUMN
          iCol As long, _
          LVCOLUMN As struct, _
          r As long
  End Sub
  Sub InsertItem hLV, iItem, Text$
      LVITEM.mask.struct = 1 'LVIF_TEXT
      LVITEM.iItem.struct = iItem
      LVITEM.iSubItem.struct = 0
      LVITEM.pszText.struct = Text$
      CallDLL #user32, "SendMessageA", _
          hLV As long, _
          4103 As long, _ 'LVM_INSERTITEM
          0 As long, _
          LVITEM As struct, _
          r As long
  End Sub
  Sub SetItem hLV, iItem, iSubItem, Text$
      LVITEM.mask.struct = 1 'LVIF_TEXT
      LVITEM.iItem.struct = iItem
      LVITEM.iSubItem.struct = iSubItem
      LVITEM.pszText.struct = Text$
      CallDLL #user32, "SendMessageA", _
          hLV As long, _
          4102 As long, _ 'LVM_SETITEM
          0 As long, _
          LVITEM As struct, _
          r As long
  End Sub
  Sub SetExtendedStyle hLV, dwStyle
      CallDLL #user32, "SendMessageA", _
          hLV As ULong, _
          4150 As ULong, _ 'LVM_SETEXTENDEDLISTVIEWSTYLE
          dwStyle As ULong, _
          dwStyle As ULong, _
          ret As Long
  End Sub
  Function GetChecked$( hLV )
      CallDLL #user32, "SendMessageA", _
          hLV As ULong, _
          4100 As ULong, _ 'LVM_GETITEMCOUNT
          0 As Long, _
          0 As Long, _
          cnt As Long
      For i = 0 To cnt - 1
          CallDLL #user32, "SendMessageA", _
              hLV As ULong, _
              4140 As ULong, _ 'LVM_GETITEMSTATE
              i As Long, _
              61440 As ULong, _ 'LVIS_STATEIMAGEMASK
              state As ULong
          If (state And 8192) Then
              GetChecked$ = GetChecked$ + " " + Str$(i)
          End If
      Next
      GetChecked$ = Trim$(GetChecked$)
  End Function

Sub SetChecked hLV, item, checked
    LVITEM.stateMask.struct = 61440
    If checked Then checked = 8192 Else checked = 4096
    LVITEM.state.struct = checked
    CallDLL #user32, "SendMessageA", _
        hLV As ULong, _
        4139 As Long, _ ' LVM_SETITEMSTATE
        item As Long, _
        LVITEM As Struct, _
        r As Long
 End Sub

_________________
Brent
Back to top
View user's profile Send private message Send e-mail
Petrus
Junior Member


Joined: 12 Jul 2014
Posts: 13
Location: Barcelona - Spain

PostPosted: Aug 29th, 2014, 6:03pm    Post subject: Re: ListView with CheckBoxes (Select&Unselect Control) Reply with quote

Yes. Sorry.

This is the code that i had wanted to show.
Have you any apicall to select and unselect all checkboxes also at a time. Thank you very much and sorry for the error code posted.
Back to top
View user's profile Send private message Send e-mail
Brent
Site Admin


Joined: 01 Jul 2005
Posts: 797

PostPosted: Aug 29th, 2014, 7:24pm    Post subject: Re: ListView with CheckBoxes (Select&Unselect Control) Reply with quote

The code I posted today is not the same code posted on the Conforums. Please notice the addition of a Sub SetChecked, which takes the following parameters: hLV, the handle of a ListView control; item, the index of the item you want checked or unchecked, or -1 for all items; and checked, a flag to indicate whether the item should be checked (1) or unchecked (0).
_________________
Brent
Back to top
View user's profile Send private message Send e-mail
Petrus
Junior Member


Joined: 12 Jul 2014
Posts: 13
Location: Barcelona - Spain

PostPosted: Aug 30th, 2014, 2:24pm    Post subject: Re: ListView with CheckBoxes (Select&Unselect Control) Reply with quote

ok.

Thank you very much.

Regards,


Pere
Back to top
View user's profile Send private message Send e-mail
Petrus
Junior Member


Joined: 12 Jul 2014
Posts: 13
Location: Barcelona - Spain

PostPosted: Aug 31st, 2014, 2:19am    Post subject: Re: ListView with CheckBoxes (Select&Unselect Control) Reply with quote

Thank you very much for all code provided. You are amazing.

Finally, you could generate a api code to create gridlines in this listbox.

Thank you very much.

Pere.
Back to top
View user's profile Send private message Send e-mail
Brent
Site Admin


Joined: 01 Jul 2005
Posts: 797

PostPosted: Aug 31st, 2014, 6:10am    Post subject: Re: ListView with CheckBoxes (Select&Unselect Control) Reply with quote

Gridlines are an extended style of ListViews. You just need to get the value of the constant LVS_EX_GRIDLINES, which happens to be 1, and add it to the dwStyle argument passed to SetExtendedStyle in the code I posted previously.

If you want to use these fancy controls, you should really take the time to understand the basics. All the information is on MSDN. Well, most of it--I usually Google for the values of constants that LB lacks. For example, I searched for "define lvs_ex_gridlines". Of course, you need to understand some C syntax to know how to translate it to LB.

_________________
Brent
Back to top
View user's profile Send private message Send e-mail
STPendl
Full Member


Joined: 20 Aug 2007
Posts: 161
Location: Austria

PostPosted: Aug 31st, 2014, 1:59pm    Post subject: Re: ListView with CheckBoxes (Select&Unselect Control) Reply with quote

Brent wrote:
Of course, you need to understand some C syntax to know how to translate it to LB.


This is easy, since Brent provides a translation table at Converting C types to LB types.

At the top of each page is a gray bar, which contains links to more information, see the Hints section for more useful information.

_________________
Stefan

Any code I post can be freely used, just give credit.
Back to top
View user's profile Send private message
Petrus
Junior Member


Joined: 12 Jul 2014
Posts: 13
Location: Barcelona - Spain

PostPosted: Aug 31st, 2014, 3:30pm    Post subject: Re: ListView with CheckBoxes (Select&Unselect Control) Reply with quote

Thank you very much to you both.

works perfectly.

Greetings




' Example
'
Sub SetExtendedStyleListView hLV, t
if t=1 then dwStyle = 1 + 4 + 32 'LVS_EX_GRIDLINES + LVS_EX_CHECKBOXES + LVS_EX_FULLROWSELECT
if t=0 then dwStyle = 32
CallDLL #user32, "SendMessageA", _
hLV As ULong, _
4150 As ULong, _ 'LVM_SETEXTENDEDLISTVIEWSTYLE
dwStyle As ULong, _
dwStyle As ULong, _
ret As Long
End Sub
Back to top
View user's profile Send private message Send e-mail
Petrus
Junior Member


Joined: 12 Jul 2014
Posts: 13
Location: Barcelona - Spain

PostPosted: Aug 31st, 2014, 5:24pm    Post subject: Re: ListView with CheckBoxes (Select&Unselect Control) Reply with quote

Finally,

if I create a listview without checkboxes, as I do to capture the selected item with doubleclick ?

Can anyone teach me this ?

Thanks for all, this forum is very formative
Listview get select item text on double click
Back to top
View user's profile Send private message Send e-mail
Brent
Site Admin


Joined: 01 Jul 2005
Posts: 797

PostPosted: Sep 1st, 2014, 5:17am    Post subject: Re: ListView with CheckBoxes (Select&Unselect Control) Reply with quote

I'm not exactly sure I understand, but if you want to know which item was double-clicked in a ListView, it requires some sophisticated programming. You have to use my WMLiberty DLL, a callback function, and some pointer manipulations.

I have a demo at www.b6sw.com/forum/content.php?mode=snips2&t=443.

_________________
Brent
Back to top
View user's profile Send private message Send e-mail
Petrus
Junior Member


Joined: 12 Jul 2014
Posts: 13
Location: Barcelona - Spain

PostPosted: Sep 1st, 2014, 1:38pm    Post subject: Re: ListView with CheckBoxes (Select&Unselect Control) Reply with quote

ok, perfect.

Thank you very much.

Pere
Back to top
View user's profile Send private message Send e-mail
Display posts from previous:   
Post new topic   Reply to topic    Bay Six Software Forum Index -> General Discussions All times are GMT
Page 1 of 1
Jump to:  
Quick Reply
Username:
Message:
   Shortcut keys: Alt+Q to activate, Alt+P to preview, Alt+S to submit
You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum



Lo-Fi Version
Powered by phpBB © 2001, 2005 phpBB Group