Sign in to follow this  
Followers 0
benners

Using $LVSCW_AUTOSIZE with listview items that have checkboxes

4 posts in this topic

I have a script that contains listview controls and using $LVSCW_AUTOSIZE I can get the columns to resize to the longest item string. However when adding checkboxes into the mix due to the space the checkbox takes up the column is resized but the last 3 or 4 characters of the item string are not shown.

My questions are does the $LVSCW_AUTOSIZE store the string length anywhere so I can add a number to it to show the full string or is there another way to acomplish what I require which is to autosize the column that includes a checkbox to show the complete string. If not I will have to add another column just for the checkbox.

Thanks.

CODE
#Region ;**** Directives created by AutoIt3Wrapper_GUI ****

#AutoIt3Wrapper_icon=icons\inibuilder.ico

#AutoIt3Wrapper_outfile=C:\WINDOWS\inibuilder.exe

#AutoIt3Wrapper_Res_Description=Creates ini files for backup.exe

#AutoIt3Wrapper_AU3Check_Stop_OnWarning=y

#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****

#include <GuiCombo.au3>

#include <GuiListView.au3>

Global Const $ini = @WindowsDir & '\backup.ini'

Global $main, $user_cbo, $user_lbl, $users, $input, $src, $dest, $file, $i, $actions, $var, $act, $listView, $Attrib, $input, $output

#Region ### START Koda GUI section ### Form=Z:\XPDVD\AutoIt\Backup\Ini Builder.kxf

$main = GUICreate("Ini Builder", 500, 442, 222, 114)

GUISetIcon("C:\WINDOWS\system32\shell32.dll", 69)

$user_cbo = GUICtrlCreateCombo("", 112, 24, 153, 25, BitOR($CBS_DROPDOWNLIST, $CBS_AUTOHSCROLL, $CBS_SORT))

$user_lbl = GUICtrlCreateLabel("Username : ", 16, 27, 61, 17)

$action_tab = GUICtrlCreateTab(16, 104, 470, 300)

$dir_tab = GUICtrlCreateTabItem("Directory Copy ")

$dir_lv = GUICtrlCreateListView("Source|Destination", 24, 152, 450, 235, BitOR($LVS_REPORT, $LVS_SHOWSELALWAYS), BitOR($WS_EX_CLIENTEDGE, $LVS_EX_GRIDLINES, $LVS_EX_FULLROWSELECT))

_GUICtrlListViewSetColumnWidth(-1, 0, 222)

_GUICtrlListViewSetColumnWidth(-1, 1, 222)

$file_tab = GUICtrlCreateTabItem("File Copy ")

$file_lv = GUICtrlCreateListView("Source|Destination", 24, 152, 450, 235, BitOR($LVS_REPORT, $LVS_SHOWSELALWAYS), BitOR($WS_EX_CLIENTEDGE, $LVS_EX_GRIDLINES, $LVS_EX_FULLROWSELECT))

_GUICtrlListViewSetColumnWidth(-1, 0, 222)

_GUICtrlListViewSetColumnWidth(-1, 1, 222)

$del_tab = GUICtrlCreateTabItem("File\Directory Delete")

$del_lv = GUICtrlCreateListView("Source|Attributes", 24, 152, 450, 235, BitOR($LVS_REPORT, $LVS_SHOWSELALWAYS), BitOR($WS_EX_CLIENTEDGE, $LVS_EX_GRIDLINES, $LVS_EX_FULLROWSELECT))

_GUICtrlListViewSetColumnWidth(-1, 0, 222)

_GUICtrlListViewSetColumnWidth(-1, 1, 222)

$file_rdo = GUICtrlCreateRadio("File", 32, 130, 49, 17)

GUICtrlSetState(-1, $GUI_CHECKED)

GUICtrlSetTip(-1, "Select to add a file delete action")

$dir_rdo = GUICtrlCreateRadio("Directory", 96, 130, 65, 17)

GUICtrlSetTip(-1, "Select to delete a directory")

$reg_tab = GUICtrlCreateTabItem("Registry Export")

$reg_lv = GUICtrlCreateListView("Keyname|Destination", 24, 152, 450, 235, BitOR($LVS_REPORT, $LVS_SHOWSELALWAYS), BitOR($WS_EX_CLIENTEDGE, $LVS_EX_GRIDLINES, $LVS_EX_FULLROWSELECT))

_GUICtrlListViewSetColumnWidth(-1, 0, 222)

_GUICtrlListViewSetColumnWidth(-1, 1, 222)

GUICtrlCreateTabItem("")

$backup_lbl = GUICtrlCreateLabel("Backup Directory :", 16, 66, 92, 17)

$backup_inp = GUICtrlCreateInput("", 112, 64, 329, 21, BitOR($ES_AUTOHSCROLL, $ES_READONLY))

$open_btn = GUICtrlCreateButton("Open INI", 328, 408, 75, 25, 0)

GUICtrlSetTip(-1, "Open the backup.ini")

$add_btn = GUICtrlCreateButton("Add", 16, 408, 55, 25, 0)

GUICtrlSetTip(-1, "Add a new action")

$remove_btn = GUICtrlCreateButton("Remove", 80, 408, 55, 25, 0)

GUICtrlSetTip(-1, "Remove the selected action")

$run_btn = GUICtrlCreateButton("Run Backup", 410, 408, 75, 25, 0)

GUICtrlSetTip(-1, "Perform the backup routine")

$useradd_btn = GUICtrlCreateButton("Add", 275, 22, 50, 25, 0)

GUICtrlSetTip(-1, "Add a new user")

$userremove_btn = GUICtrlCreateButton("Remove", 332, 22, 50, 25, 0)

GUICtrlSetTip(-1, "Remove the selected user")

$backup_btn = GUICtrlCreateButton("....", 448, 62, 24, 24, 0)

GUICtrlSetTip(-1, "Browse for a backup location")

$remove_btncontext = GUICtrlCreateContextMenu($remove_btn)

$del_current = GUICtrlCreateMenuItem("All Actions - Current Tab", $remove_btncontext)

$del_all = GUICtrlCreateMenuItem("All Actions - All Tabs", $remove_btncontext)

#EndRegion ### END Koda GUI section ###

Global $tabs[2] [4] = [['Directory', 'File', 'Delete', 'Registry'], [$dir_lv, $file_lv, $del_lv, $reg_lv]]

INI_Check() ; Check for the ini file, create one if needed.

; Add the usernames in the ini to the combo list.

For $i = 1 To $users[0]

_GUICtrlComboAddString($user_cbo, $users[$i])

If @error Then ExitLoop

Next

; Set the 1st username in the combo box, load the actions, show the GUI.

GUICtrlSetData($user_cbo, $users[1], '')

INI_Load()

GUISetState(@SW_SHOW)

While 1

$nMsg = GUIGetMsg()

Switch $nMsg

Case $GUI_EVENT_CLOSE

Exit

Case $dir_lv ; Directory copy listview clicked.

ListView_Reset_Column()

Case $file_lv ; File copy listview clicked.

ListView_Reset_Column()

Case $del_lv ; File\Folder delete listview clicked.

ListView_Reset_Column()

Case $reg_lv ; Registry listview clicked.

ListView_Reset_Column()

Case $useradd_btn ; Add a username.

$input = InputBox(' Add User', 'Type Username:', @UserName, '', 170, 80) ; Show an input box

If @error = 0 Then ; If OK is pressed

; Check if the username is already in the list, show a msg if it is.

If StringInStr(_GUICtrlComboGetList($user_cbo), $input) Then

MsgBox(64, 'User Exists', 'The selected username is already in the list...', 3)

Else

; Add the username to the combo list, set as selected, load the ini.

_GUICtrlComboAddString($user_cbo, $input)

GUICtrlSetData($user_cbo, $input, '')

IniWrite($ini, $input, 'BackupDir', '')

For $i = 0 To 3

IniWrite($ini, $input, $tabs[0][$i], 0) ; Write the actions to the ini.

Next

INI_Load()

EndIf

EndIf

Case $userremove_btn ; Remove a user section.

; Delete the current users section, reload the combo box, reload the ini.

IniDelete($ini, GUICtrlRead($user_cbo))

$var = _GUICtrlComboGetCurSel($user_cbo)

_GUICtrlComboDeleteString($user_cbo, $var)

_GUICtrlComboSetCurSel($user_cbo, 0)

INI_Load()

Case $user_cbo ; Username combo box is clicked.

INI_Load()

Case $backup_btn ; Select a backup folder.

; Open a folder browse dialog, add the folder to the input box.

$var = FileSelectFolder('Select A Destination Folder', '::{20D04FE0-3AEA-1069-A2D8-08002B30309D}', 3)

If Not @error Then

; Create a file to test for write access, show an error it its not created.

If FileClose(FileOpen($var & '\test.txt', 1)) = 0 Then

MsgBox(16, 'Backup Location', 'Unable to write to ' & $var & ' Please select another location')

Else

; Delete the test file, update the input box, write to the ini.

FileDelete($var & '\test.txt')

GUICtrlSetData($backup_inp, $var)

IniWrite($ini, GUICtrlRead($user_cbo), 'BackupDir', GUICtrlRead($backup_inp))

EndIf

EndIf

Case $add_btn ; Add a new action.

$var = GUICtrlRead($action_tab) ; Get the current tab.

Select

Case $var = 0 ; Directory Tab

; Open a folder browse dialog & if not cancelled open a folder browse dialog, add the folder to the listview, write the ini.

$src = FileSelectFolder('Select A Source Directory', '::{20D04FE0-3AEA-1069-A2D8-08002B30309D}', 3)

If Not @error Then $dest = FileSelectFolder('Select A Destination Directory' & @CRLF & '!!WARNING!! FILES IN THE DESTINATION WILL BE DELETED.' & _

' TO KEEP USE MAKE NEW FOLDER', '::{20D04FE0-3AEA-1069-A2D8-08002B30309D}', 3, GUICtrlRead($backup_inp))

If Not @error Then GUICtrlCreateListViewItem($src & '|' & $dest, $dir_lv)

INI_Write()

Case $var = 1 ; File Tab

$src = FileOpenDialog(' Select A File(s) To Copy', '', 'All (*.*)', 4) ; Open a file browse dialog.

If Not @error Then

; Check for multiple files.

If Not StringInStr($src, '|') Then

; Single file - Open a folder browse dialog, add the filenames to the listview.

$dest = FileSelectFolder('Select A Destination Directory', '::{20D04FE0-3AEA-1069-A2D8-08002B30309D}', 3, GUICtrlRead($backup_inp))

If Not @error Then GUICtrlCreateListViewItem($src & '|' & $dest, $file_lv)

Else

; Multiple files - Split the multipile files, open a folder browse dialog.

$src = StringSplit($src, '|')

$dest = FileSelectFolder('Select A Destination Directory', '::{20D04FE0-3AEA-1069-A2D8-08002B30309D}', 2, GUICtrlRead($backup_inp))

If Not @error Then

For $i = 1 To $src[0]

; Dont add the directory as a filename

If $i = 1 Then ContinueLoop

; If the file is in the root don't add a trailing slash.

If StringRight($src[1], 1) <> '\' Then $src[1] &= '\'

GUICtrlCreateListViewItem($src[1] & $src[$i] & '|' & $dest, $file_lv)

Next

EndIf

EndIf

EndIf

INI_Write()

Case $var = 2 ; Delete Tab

; If the file radio is checked.

If GUICtrlRead($file_rdo) = $GUI_CHECKED Then

$src = FileOpenDialog(' Select A File To Delete', '', 'All (*.*)', 4) ; Open a file browse dialog.

If Not @error Then

; Check for multiple files,

If Not StringInStr($src, '|') Then

; Single file - Add filename and attributes to the listview.

File_Get_Attributes($src)

GUICtrlCreateListViewItem($src & '|' & $Attrib, $del_lv)

Else

$src = StringSplit($src, '|') ; Multiple files - Add filenames and attributes to the listview.

For $i = 1 To $src[0]

If $i = 1 Then ContinueLoop ; Ignore directory.

If StringRight($src[1], 1) <> '\' Then $src[1] &= '\' ; If the directorty is root don't add a trailing slash.

File_Get_Attributes($src[1] & $src[$i]) ; Get the file attributes.

GUICtrlCreateListViewItem($src[1] & $src[$i] & '|' & $Attrib, $del_lv) ; Add the filenames and attributes to the listview.

Next

EndIf

EndIf

Else

; Open a folder browse dialog.

$src = FileSelectFolder('Select A Folder to Delete', '::{20D04FE0-3AEA-1069-A2D8-08002B30309D}', 2)

If Not @error Then

; Get the directory attributes, write them to the ini.

File_Get_Attributes($src)

GUICtrlCreateListViewItem($src & '|' & $Attrib, $del_lv)

EndIf

EndIf

INI_Write()

Case $var = 3 ; Registry Tab

ClipPut('') ; Clear the clipboard,

RunWait('regedit') ; run Regedit, wait until finished,

$src = ClipGet() ; get the key from the clipboard.

If ClipGet() <> "" Then ; If the clipboard isn't blank

; Use the registry keys shortname

$src = StringReplace($src, 'HKEY_CLASSES_ROOT', 'HKCR')

$src = StringReplace($src, 'HKEY_CURRENT_USER', 'HKCU')

$src = StringReplace($src, 'HKEY_LOCAL_MACHINE', 'HKLM')

; Open a file save dialog & if cancel isn't pressed add the entries.

$dest = FileSaveDialog('Select a Destination', GUICtrlRead($backup_inp), 'Registry Files (*.reg)')

If Not @error Then GUICtrlCreateListViewItem($src & '|' & $dest & '.reg', $reg_lv)

EndIf

INI_Write()

EndSelect

Case $remove_btn ; Delete selected action(s).

; Delete the selected items, update the ini.

_GUICtrlListViewDeleteItemsSelected($tabs[1][GUICtrlRead($action_tab) ])

ListView_Set_Column()

INI_Write()

Case $del_current ; Delete the actions in the current tab.

; Delete all the items in the current tab, update the ini.

_GUICtrlListViewDeleteAllItems($tabs[1][GUICtrlRead($action_tab) ])

ListView_Set_Column()

INI_Write()

Case $del_all ; Delete the actions in all the tabs.

; Delete the items from all the tabs, reset the column widths.

ListView_Clear()

ListView_Set_Column()

For $i = 0 To 3

$var = IniRead($ini, GUICtrlRead($user_cbo), $tabs[0][$i], 0) ; Get the number of actions for each tab.

For $x = 1 To $var

IniDelete($ini, GUICtrlRead($user_cbo), $tabs[0][$i] & $x) ; Delete all the actions for the current tab.

If @error Then ExitLoop

Next

IniWrite($ini, GUICtrlRead($user_cbo), $tabs[0][$i], 0) ; Set the number of actions to 0

Next

Case $open_btn ; Open the ini file.

ShellExecute($ini) ; Open the ini file with the default viewer.

Case $run_btn ; Run the backup program.

If Not FileExists(@WindowsDir & '\backup.exe') Then

MsgBox(16, 'Missing Progam', 'Backup.exe was not found in the Windows directory', 3)

Else

Run(@WindowsDir & '\backup.exe ' & GUICtrlRead($user_cbo))

EndIf

EndSwitch

WEnd

Func File_Get_Attributes($file) ; Get the attributes of the file passed.

$Attrib = FileGetAttrib($file)

If Not @error Then

; load possible attributes & descriptions into a string array.

$input = StringSplit("R,A,S,H,N,D,O,C,T", ",")

$output = StringSplit(" Read-only /, Archive /, System /, Hidden /, Normal /, Directory /, Offline /, Compressed /, Temporary /", ",")

For $i = 1 To 9

$Attrib = StringReplace($Attrib, $input[$i], $output[$i], 0, 1) ; Replace the attributes with the descriptions.

Next

$Attrib = StringTrimRight($Attrib, 2) ; Remove the trailing slash.

EndIf

EndFunc ;==>File_Get_Attributes

Func INI_Check() ; Check for existence of backup.ini

$users = IniReadSectionNames($ini) ; Get the usernames from the ini

; If the ini file does not exist or there are no users, create it, add a section for the current user.

If Not FileExists($ini) Or $users = 1 Then

IniWriteSection($ini, @UserName, '', '')

IniWrite($ini, @UserName, 'BackupDir', '')

For $i = 0 To 3

IniWrite($ini, @UserName, $tabs[0][$i], 0) ; Write the actions to the ini.

Next

$users = IniReadSectionNames($ini) ; Re read the usernames.

EndIf

EndFunc ;==>INI_Check

Func INI_Load() ; Load the users actions from the ini.

; Clear the previous users actions, get the backup location.

ListView_Clear()

GUICtrlSetData($backup_inp, IniRead($ini, GUICtrlRead($user_cbo), 'BackupDir', ""))

For $i = 0 To 3

ListView_Load($tabs[0][$i], $tabs[1][$i]) ; Load the listviews with the actions.(gives action and listview)

Next

EndFunc ;==>INI_Load

Func INI_Write() ; Write added actions to the ini.

; Get the username, write the backup location.

$user = GUICtrlRead($user_cbo)

IniWrite($ini, $user, 'BackupDir', GUICtrlRead($backup_inp))

; Get the current tab and the original number of actions.

$var = GUICtrlRead($action_tab)

$actions = IniRead($ini, $user, $tabs[0][$var], 0)

For $i = 1 To $actions

IniDelete($ini, $user, $tabs[0][$var] & $i) ; Delete the original actions.

Next

; Get the current number of actions, write it to the ini

$actions = _GUICtrlListViewGetItemCount($tabs[1][$var])

IniWrite($ini, $user, $tabs[0][$var], $actions)

For $i = 1 To $actions

; Write the actions to the ini, check whether the columns need resizing.

IniWrite($ini, $user, $tabs[0][$var] & $i, StringReplace(_GUICtrlListViewGetItemText($tabs[1][$var], ($i - 1)), '|', ','))

$string = StringSplit(_GUICtrlListViewGetItemText($tabs[1][$var], ($i - 1)), '|')

;If StringLen($string[1]) > 36 Then _GUICtrlListViewSetColumnWidth($tabs[1][$var], 0, $LVSCW_AUTOSIZE)

;If StringLen($string[2]) > 36 Then _GUICtrlListViewSetColumnWidth($tabs[1][$var], 1, $LVSCW_AUTOSIZE)

Next

EndFunc ;==>INI_Write

Func ListView_Clear() ; Remove all actions from the listview.

For $i = 0 To 3

_GUICtrlListViewDeleteAllItems($tabs[1][$i]) ; Delete all the previous actions.

For $x = 0 To 1

_GUICtrlListViewSetColumnWidth($tabs[1][$i], $x, 222) ; reset the column widths.

Next

Next

EndFunc ;==>ListView_Clear

Func ListView_Reset_Column() ; Reset the column widths to default (when listview clicked).

If _GUICtrlListViewGetColumnWidth($nMsg, 0) = 222 And _GUICtrlListViewGetItemCount($nMsg) <> 0 Then

_GUICtrlListViewSetColumnWidth($nMsg, 0, $LVSCW_AUTOSIZE)

_GUICtrlListViewSetColumnWidth($nMsg, 1, $LVSCW_AUTOSIZE)

Else

_GUICtrlListViewSetColumnWidth($nMsg, 0, 222)

_GUICtrlListViewSetColumnWidth($nMsg, 1, 222)

EndIf

EndFunc ;==>ListView_Reset_Column

Func ListView_Set_Column() ; Reset the column widths to default (when items removed).

; If there are no items in the listbox, reset the column widths

If _GUICtrlListViewGetItemCount($tabs[1][GUICtrlRead($action_tab) ]) = 0 Then

_GUICtrlListViewSetColumnWidth($tabs[1][GUICtrlRead($action_tab) ], 0, 222)

_GUICtrlListViewSetColumnWidth($tabs[1][GUICtrlRead($action_tab) ], 1, 222)

EndIf

If $nMsg = $del_all Then ; If deleting all tabs, resize all columns

For $i = 0 To 3

For $x = 0 To 1

_GUICtrlListViewSetColumnWidth($tabs[1][$i], $x, 222)

Next

Next

EndIf

EndFunc ;==>ListView_Set_Column

Func ListView_Load($act, $listView) ; Load the actions into the listviews.

$var = IniRead($ini, GUICtrlRead($user_cbo), $act, '0') ; Get the number of actions for the selected user.

For $i = 1 To $var

; Split the string to get the source and dest, write the strings to the listview.

$string = StringSplit(IniRead($ini, GUICtrlRead($user_cbo), $act & $i, ""), ',')

If @error Then ExitLoop

GUICtrlCreateListViewItem($string[1] & '|' & $string[2], $listView)

Next

EndFunc ;==>ListView_Load

Share this post


Link to post
Share on other sites

Look here if that helps.

Thanks Zedna, took one look and thought it would be easier to add another column :)

I think it may be a bug in the latest version of AutoIt, v3.2.6.0 and the beta v3.2.7.0. I reinstalled v3.2.4.9 and the script works perfectly, if I run it using the latest beta the whole string is not visible and if I install v3.2.6.0 it displays the same. Gonna post in the bug forum

Share this post


Link to post
Share on other sites

Thanks Zedna, took one look and thought it would be easier to add another column :)

I think it may be a bug in the latest version of AutoIt, v3.2.6.0 and the beta v3.2.7.0. I reinstalled v3.2.4.9 and the script works perfectly, if I run it using the latest beta the whole string is not visible and if I install v3.2.6.0 it displays the same. Gonna post in the bug forum

I thiought that it's behaviour of Windows but if previous versions of AutoIt works fine then it's probably bug.

Share this post


Link to post
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
Sign in to follow this  
Followers 0