Jump to content

Move columns in Listview


 Share

Recommended Posts

Attempting to move columns in Listview and see changes in an array.

I can get the column to move in the Listview.

I can get an array from the Listview.

The problem is the array reflects the original column placement in the Listview, instead of the moved placement.

Perhaps I am misinterpreting the commands I found in the helpfile. Originally posted without any code, so here is a small example.

Any help would be appreciated.

Hobbyist

#include <Array.au3>
#include <File.au3>
#include <GuiListView.au3>
#include <GuiButton.au3>
#include <GUIConstantsEx.au3>
$main = GUICreate("Column Move - Listview", 680, 515, 150, 100) ;height was 480

$Button12 = GUICtrlCreateButton("Import Files", 10, 60, 158, 33)
GUICtrlSetState($Button12,$GUI_enABLE)
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
GUICtrlSetColor(-1, 0xFF0000)
GUICtrlSetBkColor(-1, 0xE3E3E3)

$Button13 = GUICtrlCreateButton("Convert Record", 10, 100, 158, 33)
GUICtrlSetState($Button13,$GUI_enABLE)
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
GUICtrlSetColor(-1, 0xFF0000)
GUICtrlSetBkColor(-1, 0xE3E3E3)

$List10 =  GUICtrlCreateListview("",  192, 40, 200, 200);$LVS_EX_HEADERDRAGDROP;,$LVS_SINGLESEL, $LVS_EX_GRIDLINES + $LVS_EX_FULLROWSELECT)
_GUICtrlListView_SetExtendedListViewStyle ($List10, $LVS_EX_HEADERDRAGDROP )
_GUICtrlListView_AddColumn($List10, "",40)
_GUICtrlListView_AddColumn($List10, "", 40)
_GUICtrlListView_AddColumn($List10, "", 40)
_GUICtrlListView_AddColumn($List10, "", 40)

GUISetState(@SW_SHOW)


While 1

        $iMsg = GUIGetMsg()
         Switch $iMsg

                    Case $GUI_EVENT_CLOSE


                 Exit

             case $Button12         ;populate array then populate listview

                local $myarray [6][4]
                for $i = 1 to 5
                    for $j = 1 to 3
                    $myarray [$i][$j] = $i*2+$j
                    Next
                next

                _ArrayDisplay($myarray, "populate to see")

                _GUICtrlListView_AddArray($List10,  $myarray)


             case $Button13  ;upon clicking this button, expecting to see "new array" reflecting moved columns

                 $a_order = _GUICtrlListView_GetColumnOrderArray($List10);perhaps iI have misinterpreted what this does ???
                 _GUICtrlListView_SetColumnOrderArray($List10, $a_order) ;perhaps iI have misinterpreted what this does ???
                _GUICtrlListView_CreateArray()


         EndSwitch

WEnd

Func _GUICtrlListView_CreateArray()
    ;temp array to register changes in listview

  Local $iRows = _GUICtrlListView_GetItemCount($list10)
   Local $iCols = _GUICtrlListView_GetColumnCount($list10)
   Local $aListView[$iRows][$iCols]
   For $i = 0 To $iRows -1
      For $j = 0 To $iCols -1
          Local $aItem = _GUICtrlListView_GetItem($list10, $i, $j)
          $aListView[$i][$j] = $aItem[3]

      Next
  Next
  redim $aListView [$iRows][$iCols]
_ArrayDisplay($aListView, "see it")
EndFunc
Link to comment
Share on other sites

Setting the column order to the order it's already in won't change anything. When you're reading from the listview, it's showing you that just because the order of the columns have changed doesn't mean that their indexes have. You'd have to read each column in the order that the listview sees them as, and not in the order the index says, because the index doesn't change when you move the column.

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

BrewManNH

thanks for the reply.

isn't that what the helpfile is saying though..(ie isn't that the new index?)

Returns an array with the following format:
    [0] - Number of items in array (n)
    [1] - First column index
    [2] - First column index
    [n] - Last column index

If not how should I have the listview read each column AFTER the column move?

Just as important - what is the purpose of the two order array commands (get/set), if they don't perform the index change themselves?

Link to comment
Share on other sites

BrewManNH

thanks for the reply.

isn't that what the helpfile is saying though..(ie isn't that the new index?)

 

Returns an array with the following format:

    [0] - Number of items in array (n)

    [1] - First column index

    [2] - First column index

    [n] - Last column index

Like I said in my reply, the INDEX doesn't change, just the order OF the indexes. So when you read the indexes, [1] holds whatever index is holding the information for column one. That index number may contain the information from the original column 2 or 3.

The original list, if you haven't moved any columns would be 1|2|3. So if you move column 3 to the first column, the column indexes would read 3|1|2.

If not how should I have the listview read each column AFTER the column move?

You'd need to read the order of the columns, find out what index is in which column, read the row in the order of the indexes. You can't use a simple loop as you would do if the listview didn't have movable columns.

Just as important - what is the purpose of the two order array commands (get/set), if they don't perform the index change themselves?

They do exactly what the help file says they do, they programmatically set the column order instead of having to move them by hand. The first one reads the column order, the second one sets them to the exact same order they're already in, so using it is pointless the way you're doing it. The column set command does NOT change the indexes that are applied to the columns, it merely moves the index to the columns you specify.

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

example to move data:

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


Example()

Func Example()
    Local $iI, $iTimer, $idListview

    ; Create GUI
    GUICreate("ListView Add Array", 400, 300)
    $idListview = GUICtrlCreateListView("", 2, 2, 394, 268)
    GUISetState(@SW_SHOW)

    ; Add columns
    _GUICtrlListView_AddColumn($idListview, "Items", 100)
    _GUICtrlListView_AddColumn($idListview, "SubItems 1", 100)
    _GUICtrlListView_AddColumn($idListview, "SubItems 2", 100)
    _GUICtrlListView_AddColumn($idListview, "SubItems 3", 100)

    _GUICtrlListView_SetItemCount($idListview, 5000)

    ; One column load
    Local $aItems[5000][1]
    For $iI = 0 To UBound($aItems) - 1
        $aItems[$iI][0] = "Item " & $iI
    Next
    $iTimer = TimerInit()
    _GUICtrlListView_AddArray($idListview, $aItems)

    _GUICtrlListView_DeleteAllItems(GUICtrlGetHandle($idListview)) ; items added with UDF function can be deleted using UDF function
    ; Four column load
    Local $aItems[5000][4]
    For $iI = 0 To UBound($aItems) - 1
        $aItems[$iI][0] = "Item " & $iI
        $aItems[$iI][1] = "Item " & $iI & "-1"
        $aItems[$iI][2] = "Item " & $iI & "-2"
        $aItems[$iI][3] = "Item " & $iI & "-3"
    Next
    $iTimer = TimerInit()
    _GUICtrlListView_AddArray($idListview, $aItems)


    MsgBox(1,1,"move them around")

    $temp1 = _GUICtrlListView_GetColumn($idListview, 1)
    $temp2 = _GUICtrlListView_GetColumn($idListview, 3)
    _GUICtrlListView_SetColumn($idListview,1,$temp2[5])
    _GUICtrlListView_SetColumn($idListview,3,$temp1[5])
    _GUICtrlListView_DeleteAllItems(GUICtrlGetHandle($idListview)) ; items added with UDF function can be deleted using UDF function

    For $i = 0 To UBound($aItems) - 1
        $temp = $aItems[$i][3]
        $aItems[$i][3] = $aItems[$i][1]
        $aItems[$i][1] = $temp
    Next

    _GUICtrlListView_AddArray($idListview, $aItems)

    ; Loop until the user exits.
    Do
    Until GUIGetMsg() = $GUI_EVENT_CLOSE
    GUIDelete()
EndFunc   ;==>Example
IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.
Link to comment
Share on other sites

Thanks for the replies.

In looking to create an example of using the $LVS_EX_HEADERDRAGDROP to move columns, I wanted to be able to secondly create an array of the listview data after the move, which then could be loaded into a second listview as desired.
Working with _GUICtrlListView_GetColumnOrderArray and _GUICtrlListView_SetColumnOrderArray I was able to produce 
an array of integers that holds the index values of the columsn in the control, after moving columns. When you run my example you will see the array $a_order ("column array data") of the values which are then used in the Func _GUICtrlListView_CreateArray().  The final array is
#include <Array.au3>
#include <File.au3>
#include <GuiListView.au3>
#include <GuiButton.au3>
#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>
$main = GUICreate("Column Move - Listview", 680, 515, 150, 100) ;height was 480

$Button12 = GUICtrlCreateButton("Import Files", 10, 60, 158, 33)
GUICtrlSetState($Button12,$GUI_enABLE)
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
GUICtrlSetColor(-1, 0xFF0000)
GUICtrlSetBkColor(-1, 0xE3E3E3)

$Button13 = GUICtrlCreateButton("Convert Record", 10, 100, 158, 33)
GUICtrlSetState($Button13,$GUI_enABLE)
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
GUICtrlSetColor(-1, 0xFF0000)
GUICtrlSetBkColor(-1, 0xE3E3E3)

$List10 =  GUICtrlCreateListview("",  192, 40, 380, 200);$LVS_EX_HEADERDRAGDROP;,$LVS_SINGLESEL, $LVS_EX_GRIDLINES + $LVS_EX_FULLROWSELECT)
_GUICtrlListView_SetExtendedListViewStyle ($List10, $LVS_EX_HEADERDRAGDROP )
_GUICtrlListView_AddColumn($List10, "0",40)
_GUICtrlListView_AddColumn($List10, "1", 40)
_GUICtrlListView_AddColumn($List10, "2", 40)
_GUICtrlListView_AddColumn($List10, "3", 40)
_GUICtrlListView_AddColumn($List10, "4", 40)
_GUICtrlListView_AddColumn($List10, "5", 40)
_GUICtrlListView_AddColumn($List10, "6", 40)
_GUICtrlListView_AddColumn($List10, "7", 40)
global  $a_order
GUISetState(@SW_SHOW)


While 1

        $iMsg = GUIGetMsg()
         Switch $iMsg

                    Case $GUI_EVENT_CLOSE


                 Exit

             case $Button12         ;populate array then populate listview

                local $myarray [6][8]
                for $i = 0 to 5
                    for $j = 0 to 5
                    $myarray [$i][$j] = $i*2+$j
                    Next
                next

                _ArrayDisplay($myarray, "populate to see")

                _GUICtrlListView_AddArray($List10,  $myarray)


             case $Button13  ;upon clicking this button, expecting to see "new array" reflecting moved columns



                 $a_order = _GUICtrlListView_GetColumnOrderArray($List10);perhaps iI have misinterpreted what this does ???
                _ArrayDisplay($a_order,"column array data")
                _GUICtrlListView_SetColumnOrderArray($List10, $a_order) ;perhaps iI have misinterpreted what this does ???
                _ArrayDelete (  $a_order, 0)

                _GUICtrlListView_CreateArray()


         EndSwitch

WEnd

Func _GUICtrlListView_CreateArray()
    ;temp array to register changes in listview

  Local $iRows = _GUICtrlListView_GetItemCount($list10)
   Local $iCols = _GUICtrlListView_GetColumnCount($list10)
   Local $aListView[$iRows][$iCols]
   For $i = 0 To $iRows -1
      For $j = 0 To $iCols -1
          Local $aItem = _GUICtrlListView_GetItem($list10, $i, $a_order[$j])
          $aListView[$i][$j] = $aItem[3]

      Next
  Next

_ArrayDisplay($aListView, "see it")
EndFunc

 

$aListView (""see it").
 
So import the data to the listview, drag columns around and then do the convert button.
The resulting array will match the listview that has been changed by moving columns.
From there I will want to populate a new listview, which is easily done.
I tested it several times and didn't see anything wrong.
Don't know if it could be streamlined or not, I am not that advanced yet.
 
Suggestions and comments welcomed, as I am new and still learning this stuff. 
 
Hobbyist
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...