Jump to content

Add Checkbox(s) in a GUI List


Recommended Posts

Hi guys,

I have this script:

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

$Form1 = GUICreate("Form1", 248, 180)
$Edit = GUICtrlCreateEdit("", 8, 8, 233, 121)
$Button = GUICtrlCreateButton("Save", 8, 136, 233, 33)
GUISetState(@SW_SHOW)

$TestFile = @WorkingDir & "Test.txt"
FileOpen($TestFile, 2)
FileWrite($TestFile, "Word_1" & @CRLF & "Word_2" & @CRLF & "Word_3" & @CRLF & "Word_4")
GUICtrlSetData($Edit, FileRead($TestFile))

While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
Case $Button
     FileDelete(@WorkingDir & "Result.txt")
     FileWrite(@WorkingDir & "Result.txt", GUICtrlRead($Edit))
     ShellExecute(@WorkingDir & "Result.txt") EndSwitch
WEnd

I don't know how, i'd like to add a checkbox on every line of this list, so if i unchecked some and click on the button it save only the element with the checkbox enabled. I don't know how hany line has .txt, but i think we can read by array[0]

Hope I was clear, if not just ask and I'll try to post more information.

Thanks ;)

Edited by johnmcloud
Link to comment
Share on other sites

Not possible in an Edit control, you can do it in a ListView control though.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Below is a ruff script of what you could do. It gets buggy when you start scrolling, but there is a probably a workaround.

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include<array.au3>
$Form1 = GUICreate("Form1", 248, 180)
$Edit = GUICtrlCreateEdit("", 20, 8, 233, 121)
$Button = GUICtrlCreateButton("Save", 8, 136, 233, 33)

Local $iBoxes,$aBoxes[1], $sMSG, $iLines=1

GUISetState(@SW_SHOW)




GUICtrlSetData($Edit,"a"&@crlf&"b"&@crlf&"c"&@crlf)



While 1
$nMsg = GUIGetMsg()

$aLines=StringRegExp(GUICtrlRead($Edit),"(.*)?(?:v)?",3)

$iLines=UBound($aLines)

$iBoxes= UBound($aBoxes)
If $iBoxes>$iLines Then
For $x=$iLines To UBound($aBoxes)-1
GUICtrlDelete($aBoxes[$x])
Next
EndIf

ReDim $aBoxes[$iLines]
If $iBoxes<$iLines Then
For $x=$iBoxes To UBound($aBoxes)-1
$aBoxes[$x-1]=GUICtrlCreateCheckbox("",2,($x-1)*14+8,14)
GUICtrlSetState(-1,$GUI_CHECKED)
Next

EndIf


Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
Case $Edit

Case $Button
$sMSG=""
$aEdit=StringRegExp(GUICtrlRead($Edit),"(.*)?(?:v)?",3)


For $x=0 To UBound($aBoxes)-1

If BitAnd(GUICtrlRead ($aBoxes[$x]),$GUI_CHECKED) Then

$sMSG&=$aEdit[$x]
EndIf
Next
MsgBox (0,"",$sMSG)
EndSwitch

WEnd
Edited by DicatoroftheUSA
Link to comment
Share on other sites

Ok guys, for now i have make this:

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <ListviewConstants.au3>

Dim $item[6] = [5]

GUICreate("Example", 216, 201, -1, -1, -1, $WS_EX_ACCEPTFILES)
$listview = GUICtrlCreateListView("Name1", 10, 10, 192, 150, -1, BitOR($WS_EX_CLIENTEDGE, $LVS_EX_CHECKBOXES))
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 0, 185)
$item[1] = GUICtrlCreateListViewItem("Select/Unselect All", $listview)
$item[2] = GUICtrlCreateListViewItem("Word1", $listview)
$item[3] = GUICtrlCreateListViewItem("Word2", $listview)
$item[4] = GUICtrlCreateListViewItem("Word3", $listview)
$item[5] = GUICtrlCreateListViewItem("Word4", $listview)
$button = GUICtrlCreateButton("Save", 11, 162, 186, 20)
GUISetState(@SW_SHOW)

While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
Case $button
For $i = 1 To $item[0]
    If BitAND(GUICtrlRead($item[$i], 1), $GUI_CHECKED) Then
     FileDelete(@WorkingDir & "Test.txt")
     FileWrite(@WorkingDir & "Test.txt", StringTrimRight(GUICtrlRead($item[$i]),1))
     ShellExecute(@WorkingDir & "Test.txt")
;~   MsgBox(0, 0, GUICtrlRead($item[$i]), 2)
    EndIf EndIf
Next
EndSwitch
WEnd

There are a couple of problems:

1) This script not read the information from a file, the problem is not how to do, but i don't know how many item has the file. In the example are 4, but may be even 500

2) Click on checkbox "Select/Unselect All" and make it working, so if is selected, all are selected and viceversa

Thanks to all ;)

Edited by johnmcloud
Link to comment
Share on other sites

I have resolved for the Select/Unselect all

Case $item[1]
For $i = 2 To $item[0]
If BitAND(GUICtrlRead($item[1], 1), $GUI_CHECKED) Then
     GUICtrlSetState($item[$i], $GUI_CHECKED)
Else
     GUICtrlSetState($item[$i], $GUI_UNCHECKED)
EndIf
Next

But sometime seems not work...if you click fast.

And this part not work:

If BitAND(GUICtrlRead($item[$i], 1), $GUI_CHECKED) Then
     FileDelete(@WorkingDir & "Test.txt")
     FileWrite(@WorkingDir & "Test.txt", StringTrimRight(GUICtrlRead($item[$i]), 1))
     ShellExecute(@WorkingDir & "Test.txt")
;~   MsgBox(0, 0, GUICtrlRead($item[$i]), 2)
EndIf

And i need also only to know how read an unknow number of line from a txt...

Please some advice/example

Thanks

Edited by johnmcloud
Link to comment
Share on other sites

Not work:

Case $button
For $i = 1 To $item[0]
     If _GUICtrlListView_GetItemChecked($listview, $i-1) Then
         FileDelete(@WorkingDir & "Test.txt")
         FileWrite(@WorkingDir & "Test.txt", StringTrimRight(GUICtrlRead($item[$i]), 1))
     EndIf
Next
ShellExecute(@WorkingDir & "Test.txt")

The .txt has only one element also if if select more, so if i select one work, but two or more not...

Edited by johnmcloud
Link to comment
Share on other sites

This works for me...

Also for me, but when i write on a file i have only one value. I need to use FileWrite?

For $i = 1 To $item[0]
     If _GUICtrlListView_GetItemChecked($listview, $i - 1) Then
         FileDelete(@WorkingDir & "Test.txt")
         FileWrite(@WorkingDir & "Test.txt", _GUICtrlListView_GetItemText($listview, $i - 1) & @CRLF)
     EndIf
Next
ShellExecute(@WorkingDir & "Test.txt")
Edited by johnmcloud
Link to comment
Share on other sites

Thanks Zedna, update:

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <ListviewConstants.au3>
#include <GuiListView.au3>

Dim $item[6] = [5]

GUICreate("Example", 216, 201, -1, -1, -1, $WS_EX_ACCEPTFILES)
$iListView = GUICtrlCreateListView("Name1", 10, 10, 192, 150, -1, BitOR($WS_EX_CLIENTEDGE, $LVS_EX_CHECKBOXES))
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 0, 185)
$hListView = GUICtrlGetHandle($iListView)
$item[1] = GUICtrlCreateListViewItem("Select/Unselect All", $iListView)
$item[2] = GUICtrlCreateListViewItem("Word1", $iListView)
$item[3] = GUICtrlCreateListViewItem("Word2", $iListView)
$item[4] = GUICtrlCreateListViewItem("Word3", $iListView)
$item[5] = GUICtrlCreateListViewItem("Word4", $iListView)
$button = GUICtrlCreateButton("Save", 11, 162, 186, 20)
GUISetState(@SW_SHOW)

For $i = 1 To $item[0]
GUICtrlSetState($item[$i], $GUI_CHECKED)
Next

While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
Case $button
FileOpen(@WorkingDir & "Test.txt", 2)
For $i = 2 To $item[0]
    If _GUICtrlListView_GetItemChecked($iListView, $i - 1) Then
     FileWrite(@WorkingDir & "Test.txt", _GUICtrlListView_GetItemText($iListView, $i - 1) & @CRLF)
    EndIf
Next
If _GUICtrlListView_AllUnChecked($hListView) = True Then
    MsgBox(0, 0, "No element(s) selected")
Else
    ShellExecute(@WorkingDir & "Test.txt")
EndIf
Case $item[1]
For $i = 2 To $item[0]
    If BitAND(GUICtrlRead($item[1], 1), $GUI_CHECKED) Then
     GUICtrlSetState($item[$i], $GUI_CHECKED)
    Else
     GUICtrlSetState($item[$i], $GUI_UNCHECKED)
    EndIf
Next
EndSwitch
WEnd

Func _GUICtrlListView_AllUnChecked($hListView)
Local $iChecked = 0, $iCount = _GUICtrlListView_GetItemCount($hListView)
For $i = 0 To $iCount - 1
If Not _GUICtrlListView_GetItemChecked($hListView, $i) Then
$iChecked += 1
EndIf
Next
Return $iChecked = $iCount ; Returns True - all items are unchecked or False - some are unchecked.
EndFunc ;==>_GUICtrlListView_AllUnChecked

Problems:

1) The Func _GUICtrlListView_AllUnChecked read also the first line ( select/unselect all ), how to remove from the _GUICtrlListView_GetItemCount ?

2) Read the ListView information from a txt, without know the number of the item

Edited by johnmcloud
Link to comment
Share on other sites

New update:

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <ListviewConstants.au3>
#include <GuiListView.au3>

Dim $SelectAll[1]

GUICreate("Example", 216, 201, -1, -1, -1, $WS_EX_ACCEPTFILES)
$iListView = GUICtrlCreateListView("Name1", 10, 10, 192, 150, -1, BitOR($WS_EX_CLIENTEDGE, $LVS_EX_CHECKBOXES))
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 0, 185)
$hListView = GUICtrlGetHandle($iListView)
$SelectAll[0] = GUICtrlCreateListViewItem("Select/Unselect All", $iListView)
$Button = GUICtrlCreateButton("Save", 11, 162, 186, 20)
GUISetState(@SW_SHOW)

File_Create()
Load_Value()

While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
Case $Button
FileOpen(@WorkingDir & "Test.txt", 2)
For $i = 2 To $SelectAll[0]
    If _GUICtrlListView_GetItemChecked($iListView, $i - 1) Then
     FileWrite(@WorkingDir & "Test.txt", _GUICtrlListView_GetItemText($iListView, $i - 1) & @CRLF)
    EndIf
Next
If _GUICtrlListView_AllUnChecked($hListView) = True Then
    MsgBox(0, 0, "No element(s) selected")
Else
    ShellExecute(@WorkingDir & "Test.txt")
EndIf
EndSwitch
WEnd

Func File_Create()
$Test = FileOpen(@WorkingDir & "Test.txt", 2)
FileWrite($Test, "Value11" & @LF & "Value22" & @LF & "Value33" & @LF & "Value44")
EndFunc

Func _GUICtrlListView_AllUnChecked($hListView)
Local $iChecked = 1, $iCount = _GUICtrlListView_GetItemCount($hListView)
For $i = 0 To $iCount - 1
If Not _GUICtrlListView_GetItemChecked($hListView, $i) Then
$iChecked += 1
EndIf
Next
Return $iChecked = $iCount ; Returns True - all items are unchecked or False - some are unchecked.
EndFunc ;==>_GUICtrlListView_AllUnChecked

Func Load_Value()
$File = FileRead(@WorkingDir & "Test.txt")
$array = StringSplit($File, @LF)
For $i = 1 To $array[0] - 1
GUICtrlCreateListViewItem(StringTrimRight($array[$i], 1), $iListView)
Next
EndFunc ;==>Load_Value

Now the script load the information from a txt ( remove last line and last letter of every line, but i need it )

I need to add only a working checkbox "Select/Unselect All"

Please :D

Edited by johnmcloud
Link to comment
Share on other sites

I think i'm not understand :(

I have not figured out how to apply it to my situation. I have no variables, I have only one value ($SelectAll), the others are generated from the contents of the file

So i want to click on "Select/Unselect All" checkbox, if checked the other are checked and viceversa.

The script, with the new button for load the value and one for save:

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <ListviewConstants.au3>
#include <GuiListView.au3>

Dim $SelectAll[1]
 
$_1 = GUICreate("Example", 217, 202, -1, -1, -1, BitOR($WS_EX_ACCEPTFILES,$WS_EX_WINDOWEDGE))
$iListView = GUICtrlCreateListView("Name1", 10, 10, 192, 150, -1, BitOR($WS_EX_CLIENTEDGE,$LVS_EX_CHECKBOXES))
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 0, 180)
$hListView = GUICtrlGetHandle($iListView)
$ButtonSave = GUICtrlCreateButton("Save", 107, 164, 92, 20)
$ButtonLoad = GUICtrlCreateButton("Load", 19, 166, 84, 20)
GUISetState(@SW_SHOW)

File_Create()

While 1
$nMsg = GUIGetMsg()
Switch $nMsg
  Case $GUI_EVENT_CLOSE
   Exit
  Case $ButtonLoad
   $SelectAll[0] = GUICtrlCreateListViewItem("Select/Unselect All", $iListView)
   Load_Value()
  Case $ButtonSave
   FileOpen(@WorkingDir & "Test.txt", 2)
   For $i = 2 To $SelectAll[0]
    If _GUICtrlListView_GetItemChecked($iListView, $i - 1) Then
     FileWrite(@WorkingDir & "Test.txt", _GUICtrlListView_GetItemText($iListView, $i - 1) & @CRLF)
    EndIf
   Next
   If _GUICtrlListView_AllUnChecked($hListView) = True Then
    MsgBox(0, 0, "No element(s) selected")
   Else
    ShellExecute(@WorkingDir & "Test.txt")
   EndIf
EndSwitch
WEnd

Func File_Create()
$Test = FileOpen(@WorkingDir & "Test.txt", 2)
FileWrite($Test, "Value11" & @LF & "Value22" & @LF & "Value33" & @LF & "Value44")
EndFunc   ;==>File_Create

Func _GUICtrlListView_AllUnChecked($hListView)
Local $iChecked = 1, $iCount = _GUICtrlListView_GetItemCount($hListView)
For $i = 0 To $iCount - 1
  If Not _GUICtrlListView_GetItemChecked($hListView, $i) Then
   $iChecked += 1
  EndIf
Next
Return $iChecked = $iCount ; Returns True - all items are unchecked or False - some are unchecked.
EndFunc   ;==>_GUICtrlListView_AllUnChecked

Func Load_Value()
$File = FileRead(@WorkingDir & "Test.txt")
$array = StringSplit($File, @LF)
For $i = 1 To $array[0] - 1
  GUICtrlCreateListViewItem(StringTrimRight($array[$i], 1), $iListView)
Next
EndFunc   ;==>Load_Value
Edited by johnmcloud
Link to comment
Share on other sites

I think i have a solution:

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <ListviewConstants.au3>
#include <GuiListView.au3>

Dim $SelectAll[1]

$_1 = GUICreate("Example", 217, 202, -1, -1, -1, BitOR($WS_EX_ACCEPTFILES, $WS_EX_WINDOWEDGE))
$iListView = GUICtrlCreateListView("Name1", 10, 10, 192, 150, -1, BitOR($WS_EX_CLIENTEDGE, $LVS_EX_CHECKBOXES))
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 0, 180)
$hListView = GUICtrlGetHandle($iListView)
$ButtonSave = GUICtrlCreateButton("Save", 107, 164, 92, 20)
$ButtonLoad = GUICtrlCreateButton("Load", 19, 166, 84, 20)
GUISetState(@SW_SHOW)

File_Create()

While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
Case $ButtonLoad
$SelectAll[0] = GUICtrlCreateListViewItem("Select/Unselect All", $iListView)
Load_Value()
Case $ButtonSave
FileOpen(@WorkingDir & "Test.txt", 2)
For $i = 2 To $SelectAll[0]
    If _GUICtrlListView_GetItemChecked($iListView, $i - 1) Then
     FileWrite(@WorkingDir & "Test.txt", _GUICtrlListView_GetItemText($iListView, $i - 1) & @CRLF)
    EndIf
Next
If _GUICtrlListView_AllUnChecked($hListView) = True Then
    MsgBox(0, 0, "No element(s) selected")
Else
    ShellExecute(@WorkingDir & "Test.txt")
EndIf
Case $SelectAll[0]
If _GUICtrlListView_GetItemChecked($iListView, $SelectAll) Then
_GUICtrlListView_SetItemChecked($iListView, $SelectAll[0], 1)
_GUICtrlListView_SetCheckedStates($hListView, 1)
EndIf
If Not _GUICtrlListView_GetItemChecked($iListView, $SelectAll) Then
_GUICtrlListView_SetItemChecked($iListView, $SelectAll, 2)
_GUICtrlListView_SetCheckedStates($hListView, 2)
EndIf
EndSwitch
WEnd

Func File_Create()
$Test = FileOpen(@WorkingDir & "Test.txt", 2)
FileWrite($Test, "Value11" & @LF & "Value22" & @LF & "Value33" & @LF & "Value44")
EndFunc ;==>File_Create

Func _GUICtrlListView_AllUnChecked($hListView)
Local $iChecked = 1, $iCount = _GUICtrlListView_GetItemCount($hListView)
For $i = 0 To $iCount - 1
If Not _GUICtrlListView_GetItemChecked($hListView, $i) Then
$iChecked += 1
EndIf
Next
Return $iChecked = $iCount ; Returns True - all items are unchecked or False - some are unchecked.
EndFunc ;==>_GUICtrlListView_AllUnChecked

Func Load_Value()
$File = FileRead(@WorkingDir & "Test.txt")
$array = StringSplit($File, @LF)
For $i = 1 To $array[0] - 1
GUICtrlCreateListViewItem(StringTrimRight($array[$i], 1), $iListView)
Next
EndFunc ;==>Load_Value

Func _GUICtrlListView_SetCheckedStates($hListView, $iType) ; By Zedna, Modified by guinness.
Local $fState = False, $iCount = _GUICtrlListView_GetItemCount($hListView)
If $iType < 0 Or $iType > 2 Then
Return SetError(1, 0, 0)
EndIf
If $iType Then
$fState = True
EndIf
For $i = 0 To $iCount - 1
If $iType = 2 Then
$fState = Not _GUICtrlListView_GetItemChecked($hListView, $i) ; Invert checked state with $iType 2.
EndIf
_GUICtrlListView_SetItemChecked($hListView, $i, $fState)
Next
EndFunc ;==>_GUICtrlListView_SetCheckedStates

There is only a problem, if you click fast on checkbox "select/unselect all" sometimes lose the function. Someone what to test for search a workaround?

Thanks

Edited by johnmcloud
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...