NewBieAuto

How to make my Function _AddToListView run faster?

18 posts in this topic

I want to add some item to list view, and here's my code:

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <ListViewConstants.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <GuiListViewEx.au3>
#include <Array.au3>
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 281, 304, 685, 145)
$Label1 = GUICtrlCreateLabel("Item 2", 12, 12, 33, 17)
$Item2Ip = GUICtrlCreateInput("", 48, 9, 70, 21)
$Label2 = GUICtrlCreateLabel("Item 3", 126, 9, 30, 17)
$Item3Ip = GUICtrlCreateInput("", 159, 9, 64, 21)
$Add = GUICtrlCreateButton("Add", 231, 9, 40, 22, $BS_DEFPUSHBUTTON)
$ListView = GUICtrlCreateListView("Item 1|Item 2|Item3|iTem4", 12, 42, 259, 250)
$LVIdx = _GUIListViewEx_Init($ListView, '', 0, Default, Default, 1+2+4)
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 0, 50)
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 1, 50)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
         case $Add
            _AddToListView()
    EndSwitch
WEnd


Func _AddToListView()
   $Item2 = Guictrlread($Item2Ip)
   $Item3 = GUICtrlRead($Item3Ip)
   $PhanLoaiChuong = ''
   $Item4 = ''

   $Arr = _GUICtrlListView_CreateArray($ListView)
   ;_ArrayDisplay($Arr)
   if $Arr[1][1] = '' then
      $SubLine2 = 0
   Else
      $SubLine2 = Number($Arr[1][1])
   EndIf
   if $Arr[1][2] = '' then
      $SubLine1 = 0
   Else
      $SubLine1 = Number($Arr[1][2])
   EndIf

     if $Item2 <> '' AND $Item3 = '' Then
         local $AddInfo[][] = [[$PhanLoaiChuong, $Item2, '', '']]
         _GUIListViewEx_InsertSpec($LVIdx, -1, $AddInfo)
         GUICtrlSetState($Item2Ip, $GUI_FOCUS)
         _MyListView_Scroll($ListView, "IT1")
      EndIf

      if  $Item3 <> '' AND $Item2 = '' Then
         _GUIListViewEx_ChangeItem($LVIdx, $SubLine1, 3 , $Item4)
         _GUIListViewEx_ChangeItem($LVIdx, $SubLine1, 2 , $Item3)
         GUICtrlSetState($Item3Ip, $GUI_FOCUS)
         _MyListView_Scroll($ListView, "IT2")
       EndIf

   if $Item2 <> '' And $Item3 <> '' Then
       local $AddInfo[][] = [[$PhanLoaiChuong, $Item2, $Item3, $Item4]]
      _GUIListViewEx_InsertSpec($LVIdx, -1, $AddInfo)
      GUICtrlSetState($Item2Ip, $GUI_FOCUS)
      _MyListView_Scroll($ListView, "IT2")
   EndIf

   GUICtrlSetData($Item2Ip, '')
   GUICtrlSetData($Item3Ip, '')
EndFunc


Func _GUICtrlListView_CreateArray($idListview)
   $TotalColumn = _GUICtrlListView_GetColumnCount($idListview)
   Local $aArray[_GUICtrlListView_GetItemCount($idListview) + 1][$TotalColumn]
   ;_ArrayDisplay($aArray)
   For $i = 0 To UBound($aArray) - 2
       For $j = 0 To UBound($aArray, 2) - 1
           $aArray[$i + 1][$j] = _GUICtrlListView_GetItem($idListview, $i, $j)[3]
           If $aArray[$i + 1][$j] <> "" Then $aArray[0][$j] += 1
       Next
           ;_ArrayDisplay($aArray)
   Next

   ;Get Column text name to first row
   _ArrayInsert($aArray, 0)
   for $a = 0 to $TotalColumn - 1
      $Col1Name = _GUICtrlListView_GetColumn($idListview, $a)[5]
      $aArray[0][$a] = $Col1Name
   Next


   return $aArray
EndFunc   ;==>_GUICtrlListView_CreateArray

Func _MyListView_Scroll($SourceListView, $ByElement)
   $CheckArray = _GUICtrlListView_CreateArray($SourceListView)
   $TotalCurrentTenThuoc = $CheckArray[1][1]
   $TotalCurrentViThuoc = $CheckArray[1][2]
   $XPos = _GUICtrlListView_GetItemPositionX($SourceListView, 0)
   $YPos = _GUICtrlListView_GetItemPositionY($SourceListView,$TotalCurrentViThuoc - 5)
   if $ByElement = "IT1" then
      _GUICtrlListView_Scroll($SourceListView, 0, 300)
      ;if _GUICtrlListView_GetColumnWidth($SourceListView, 1) > 85 then _GUICtrlListView_SetColumnWidth($SourceListView, 1, 130)
   EndIf
   if $ByElement = "IT2" then
      _GUICtrlListView_Scroll($SourceListView, 2, $YPos)
      ;if _GUICtrlListView_GetColumnWidth($SourceListView, 1) > 85 then _GUICtrlListView_SetColumnWidth($SourceListView, 1, 130)
   EndIf

EndFunc

How to make it runs faster??

Thanks all!

Share this post


Link to post
Share on other sites



NewBieAuto,

How about explaining just what you want to do when you press the button? At the moment it is quite difficult to work it out from your uncommented code:

  • Just where in the ListView do you want to add the new line? At the end, the beginning or next to the selected line?
  • What is the scrolling about?
  • Why all the different behaviours when one of the inputs is empty?

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

Share this post


Link to post
Share on other sites

I want to input data like excel: input by row or column and auto scroll to the last item was inputed

Share this post


Link to post
Share on other sites

NewBieAuto,

So why, as you are using my GUIListViewEx UDF, do you not edit the cells directly when they are double-clicked? That would be much easier.

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

Share this post


Link to post
Share on other sites

How to edit the cells if dont use double-clicked? I think double-clicked is the best way.

And I dont know why but in Win 7 it runs very fast. But not in Xp

Share this post


Link to post
Share on other sites

NewBieAuto,

I can see a few ways to speed up the script - but first I need to know what is the logic behind the entries as there appears to be no pattern that I can distinguish for the case where there is an Item3 with no Item 2. You provide the logic and I will see what I can do.

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

Share this post


Link to post
Share on other sites

Ah. May be that case doesnt exit. I often add item2, then add item 3

Share this post


Link to post
Share on other sites

NewBieAuto,

Just explain in simple terms what is supposed to happen in these 3 cases:

  • Both inputs contain something
  • Only Item 2 contains a value
  • Only Item 3 contains a value

I assume that if neither contains a value then nothing happens.

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

Share this post


Link to post
Share on other sites

My goal is input like that: item 2 - item 3 in each rows. So I can input with 2 way:

  • Both item2 and item3
  • Fist fill the item2 column, then fill the item3 column (the item3 offten is a number so input it with the numberic keypad must be faster)

Share this post


Link to post
Share on other sites

#10 ·  Posted

NewBieAuto,

How about this - you can enter data using the inputs or by doubleclicking the cells:

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

#include <GUIListViewEx.au3>

$Form1 = GUICreate("Form1", 281, 304, 685, 145)

$Label1 = GUICtrlCreateLabel("Item 2", 12, 12, 33, 17)
$Item2Ip = GUICtrlCreateInput("", 48, 9, 70, 21)
$Label2 = GUICtrlCreateLabel("Item 3", 126, 9, 30, 17)
$Item3Ip = GUICtrlCreateInput("", 159, 9, 64, 21)
$Add = GUICtrlCreateButton("Add", 231, 9, 40, 22, $BS_DEFPUSHBUTTON)
$ListView = GUICtrlCreateListView("Item 1|Item 2|Item3|Item4", 12, 42, 259, 250)
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 0, 50)
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 1, 50)

$LVIdx = _GUIListViewEx_Init($ListView, '', 0, Default, Default, 1 + 2 + 4)

_GUIListViewEx_SetEditStatus($LVIdx, "1-2")

GUISetState(@SW_SHOW)

_GUIListViewEx_MsgRegister()

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Add
            _AddToListView()
    EndSwitch

    _GUIListViewEx_EditOnClick()
WEnd

Func _AddToListView()

    $Item2 = GUICtrlRead($Item2Ip)
    $Item3 = GUICtrlRead($Item3Ip)

    If $Item2 <> "" Then ; if Item2 then add line to ListView with both items
        $aLVContent = _GUIListViewEx_InsertSpec($LVIdx, -1, "|" & $Item2 & "|" & $Item3 & "|")
        ; And make sure item we have just added is visible
        _GUICtrlListView_EnsureVisible($ListView, UBound($aLVContent) - 1)
    Else
        If $Item3 <> "" Then ; Only Item3 filled
            ; I am assuming that we look for the first empty cell in the Item3 column
            $aLVContent = _GUIListViewEx_ReturnArray($LVIdx, 3) ; Get 2D array of ListView content
            ; Now search for the first empty cell in the Item3 column
            For $iRow = 0 To UBound($aLVContent) - 1
                If $aLVContent[$iRow][2] = "" Then
                    _GUIListViewEx_ChangeItem($LVIdx, $iRow, 2, $Item3)
                    ; Make sure item we have just added is visible
                    _GUICtrlListView_EnsureVisible($ListView, $iRow)
                    ; No point in searching further
                    ExitLoop
                EndIf
            Next
            ; If there are no empty cells then do nothing
        Else
            ; Neither item filled so do nothing
        EndIf
    EndIf

    ; Clear inputs and reset focus
    GUICtrlSetState($Item2Ip, $GUI_FOCUS)
    GUICtrlSetData($Item2Ip, "")
    GUICtrlSetData($Item3Ip, "")

EndFunc

That seems a little more streamlined, but does it do what you want?

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

Share this post


Link to post
Share on other sites

#11 ·  Posted

Wow! Thanks M23. You help me save a lot of time :D

By the way, Could you help me with how to select multi-single subitem in listview. I want to delete some items in item3 column when I make a typing mistake? 

Share this post


Link to post
Share on other sites

#12 ·  Posted

And there's a problem. If i use opt("MouseCoordMode", 2), the listview wont be editable?

How to solve it?

Share this post


Link to post
Share on other sites

#13 ·  Posted

NewBieAuto,

Quote

how to select multi-single subitem in listview

I have no idea what that means. To delete the content of a cell, just double-click it and delete the contents as for a normal edit.

Quote

If i use opt("MouseCoordMode", 2), the listview wont be editable?

Hardly surprising as the UDF uses the default MouseCoordMode when determining the click positions - why do you need to use another?

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

Share this post


Link to post
Share on other sites

#14 ·  Posted

3 minutes ago, Melba23 said:

I have no idea what that means. To delete the content of a cell, just double-click it and delete the contents as for a normal edit.

I want to delete some cells at the same time.

6 minutes ago, Melba23 said:

Hardly surprising as the UDF uses the default MouseCoordMode when determining the click positions - why do you need to use another?

But why on your old GuiListViewEx version, It work normally. I need to use this to automate a java application.

Share this post


Link to post
Share on other sites

#15 ·  Posted

NewBieAuto,

Quote

I want to delete some cells at the same time

It is not possible to select several "cells" in a ListView separately - the UDF can allow for single cell (rather then row) selection. You could perhaps select several "rows" and then delete the sub-items within those rows.

Quote

why on your old GuiListViewEx version, It work normally

I have never tested any of the UDF versions for compatibility with MouseCoordMode, so I have no idea whether they worked or not. But I would be surprised if any of the older versions worked win anything other than the default mode given that so much of the UDF depends on getting the current mouse position.

As always, it is much easier to help if the whole script is available - adding more restrictive operating conditions as we go along seldom makes for good code, and certainly annoys those who have offered help based on what are shown to be non-representative snippets. So, if you want any more help, post the full script and I will see what I can do.

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

Share this post


Link to post
Share on other sites

#17 ·  Posted (edited)

NewBieAuto,

Nothing to do with MouseCoordMode - it is a bug in the UDF which is not correctly storing the double clicked location. Now looking to see why it happens - at first glance I think it is due to a re-ordering of the WM_NOTIFY events but I need to check to be sure.

Thanks for finding it - I will report back as soon as I have any definite news..

M23

Edit: My guess was correct - in the version you posted the click location was not being correctly stored before the double click was recognised and so it did not always recognise the correct cell to edit. Fortunately I have a new version of the UDF just about ready to release and I have already fixed the problem. But you still have my thanks for finding the bug in the current version.

Edited by Melba23

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

Share this post


Link to post
Share on other sites

#18 ·  Posted

Don't mention it. I am anxious to download your new version :D

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