Jump to content

Cannot delete ListView columns using For loop...


Recommended Posts

Hi!

I'm trying to delete all columns of a ListView.

The columns count could differ. To delete the

columns I use a function:

Func _GUICtrlListView_DeleteAllColumns($hWnd)

For $i = 0 To _GUICtrlListView_GetColumnCount($hWnd) Step 1

ConsoleWrite($i & @CRLF)

_GUICtrlListView_DeleteColumn($hWnd, $i)

Next

EndFunc

Before the for-loop starts _GUICtrlListView_GetColumnCount() returns 29 columns.

After _GUICtrlListView_GetColumnCount() returns 14 columns.

What's wrong?

May someone could help?

Greets,

-supersonic.

Link to comment
Share on other sites

#include <GuiConstantsEx.au3>
#include <GuiListView.au3>

Dim $hGUI = GUICreate('Test', 300, 400)
Dim $hListView = _GUICtrlListView_Create($hGUI, '1|2|3|4|5|6|7|8|9|10|11|12|13|14|15', 0, 0, 300, 300)
Dim $Button = GUICtrlCreateButton('Delete', 215, 340, 70, 25)

GUISetState()

While 1
    Switch GUIGetMsg()
        Case -3
            ExitLoop
            
        case $Button
            DeleteColumns($hListView)
            GUICtrlSetState($Button, $GUI_DISABLE)
    EndSwitch
    Sleep(40)
WEnd

GUIDelete()


Func DeleteColumns($hLV)
    Local $iColumn = _GUICtrlListView_GetColumnCount($hLV)
    
    For $i = $iColumn-1 To 0 Step -1
        _GUICtrlListView_DeleteColumn($hLV, $i)
    Next
EndFunc

Link to comment
Share on other sites

Ahhh...

I got it! :-)

Thank you.

#include <GuiConstantsEx.au3>
#include <GuiListView.au3>

Dim $hGUI = GUICreate('Test', 300, 400)
Dim $hListView = _GUICtrlListView_Create($hGUI, '1|2|3|4|5|6|7|8|9|10|11|12|13|14|15', 0, 0, 300, 300)
Dim $Button = GUICtrlCreateButton('Delete', 215, 340, 70, 25)

GUISetState()

While 1
    Switch GUIGetMsg()
        Case -3
            ExitLoop
            
        case $Button
            DeleteColumns($hListView)
            GUICtrlSetState($Button, $GUI_DISABLE)
    EndSwitch
    Sleep(40)
WEnd

GUIDelete()


Func DeleteColumns($hLV)
    Local $iColumn = _GUICtrlListView_GetColumnCount($hLV)
    
    For $i = $iColumn-1 To 0 Step -1
        _GUICtrlListView_DeleteColumn($hLV, $i)
    Next
EndFunc
Link to comment
Share on other sites

Another example:

Func _GUICtrlListView_DeleteAllColumns($hWnd)
    While _GUICtrlListView_GetColumnCount($hWnd) > 0
        _GUICtrlListView_DeleteColumn($hWnd, 0)
    Wend
EndFunc   ;==>_GUICtrlListView_DeleteAllColumns

It's generally not a good idea to use a For loop when removing items from collections. I wonder whether the target value of the for loop is calculated only once at the beginning and not re-evaluated for each iteration of the loop or whether it's dynamic ...

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...