Jump to content

[SOLVED] Most efficient way to associate Control IDs with an array of data?


 Share

Recommended Posts

Hi,

What's the most efficient way to associate Control IDs with arrays of data?

Say, for example, you have a TreeView containing 25 TreeView Items and you'd like to store some additional information about each item in an array... how would you go about organising this?

Here's what I'm doing currently (the TreeView items are loaded in from an INI):

; 1. Create TreeView
Local $treeview = GUICtrlCreateTreeView();

; 2. Load some names from INI sections
Local $treeviewItemNames = loadNamesFromINISections();
Local $treeviewItemsUBound = UBound(treeviewItemNames);

; 3. Create some arrays that will be used in next step
Local $treeviewItemIDs[treeviewItemsUBound]
Local $treeviewItemDescriptions[treeviewItemsUBound]

; 4. Populate the TreeView with items using the loaded names
Local $thisName
For $i = 0 To treeviewItemsUBound Step 1
  $thisName = treeviewItemNames[$i]

  $treeviewItemIDs[$i] = GUICtrlCreateTreeViewItem($thisName, $treeview);
  $treeviewItemDescriptions[$i] = loadDescriptionFromINI($thisName)
Next

 

Now, this works fine, however, when you want to retreive information, it does require you loop through the array to find the matching Control ID to get the associated $treeviewItemDescriptions[] index. I'm just curious if there is a more efficient way?

Another idea I had was using the Control ID as the actual array index but something tells me this probably wouldn't be wise. Maybe there is a much better way of correlating TreeView items with information that I'm unaware of!?

Thanks!

Edited by cosmos
Link to comment
Share on other sites

You can store ControlIDs as keys in the new Map datatype offered in the beta, the value being the index for accessing the array of descriptions and names.

You can also store an array of name and description as value instead of an index and get rid of the other arrays.

Edited by jchd
Clarify proposal

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

Thanks! Maps sound like exactly what I need.

I've installed the Beta, but can't seem to get the ScITE script editor to recognise it (even after running "TOGGLE AU3 BETA"). Not sure if this is due to Norton flagging one of the files after installation (which I then marked as safe so it was restored). Tried uninstalling and reinstalling.

Link to comment
Share on other sites

Run with Alt-F5 and compile with Alt-F7.

Else use the directive:

#AutoIt3Wrapper_Version=b                        ;(B/P) Use Beta or Production for AutoIt3 and Aut2Eex. Default is P

Edited by jchd

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

I don't think you really need to use a map for this. Since the control ID is an int anyway, you can just use that as the index to an array holding data. (I.e., $aArrayOfData[GUICtrlCreateTreeViewItem($thisName, $treeview)])

#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>
#include <StaticConstants.au3>
#include <TreeViewConstants.au3>
#include <WindowsConstants.au3>
#include <ButtonConstants.au3>


Example()

Func Example()
    Local $aData[0]
    GUICreate("My GUI with treeview", 350, 215)

    Local $idTreeview = GUICtrlCreateTreeView(6, 6, 100, 150, BitOR($TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS), $WS_EX_CLIENTEDGE)
    Local $idGeneralitem = AddTreevViewItem($aData, $idTreeview, "General", "TreeView Demo")
    GUICtrlSetColor(-1, 0x0000C0)
    Local $idDisplayitem = AddTreevViewItem($aData, $idTreeview, "Display", "")
    GUICtrlSetColor(-1, 0x0000C0)
    Local $idAboutitem = AddTreevViewItem($aData, $idGeneralitem, "About", "This little scripts demonstates the using of a treeview-control.")
    Local $idCompitem = AddTreevViewItem($aData, $idGeneralitem, "Computer", "Name:" & @TAB & @ComputerName & @CRLF & "OS:" & @TAB & @OSVersion & @CRLF & "SP:" & @TAB & @OSServicePack)
    AddTreevViewItem($aData, $idGeneralitem, "User", @UserName)
    AddTreevViewItem($aData, $idDisplayitem, "Resolution", @DesktopWidth & "x" & @DesktopHeight)
    Local $idLast = AddTreevViewItem($aData, $idDisplayitem, "Other", "")
    Local $lblMessage = GUICtrlCreateLabel("TreeView Demo", 110, 6, 220, 150)
    GUICtrlCreateLabel("", 0, 170, 350, 2, $SS_SUNKEN)
    Local $idTogglebutton = GUICtrlCreateButton("&Toggle", 35, 185, 70, 20)
    Local $idInfobutton = GUICtrlCreateButton("&Info", 105, 185, 70, 20)
    Local $idStatebutton = GUICtrlCreateButton("Col./Exp.", 175, 185, 70, 20)
    Local $idCancelbutton = GUICtrlCreateButton("&Cancel", 245, 185, 70, 20)

    GUICtrlSetState($idGeneralitem, BitOR($GUI_EXPAND, $GUI_DEFBUTTON)) ; Expand the "General"-item and paint in bold
    GUICtrlSetState($idDisplayitem, BitOR($GUI_EXPAND, $GUI_DEFBUTTON)) ; Expand the "Display"-item and paint in bold

    GUISetState(@SW_SHOW)

    Local $idMsg, $idItem, $hItem, $sText
    ; Loop until the user exits.
    While 1
        $idMsg = GUIGetMsg()
        Select
            Case $idMsg = $idCancelbutton Or $idMsg = $GUI_EVENT_CLOSE
                ExitLoop

            Case $idMsg = $idTogglebutton ; Toggle the bold painting
                If BitAND(GUICtrlRead($idGeneralitem), $GUI_DEFBUTTON) Then
                    GUICtrlSetState($idGeneralitem, 0)
                    GUICtrlSetState($idDisplayitem, 0)
                Else
                    GUICtrlSetState($idGeneralitem, $GUI_DEFBUTTON)
                    GUICtrlSetState($idDisplayitem, $GUI_DEFBUTTON)
                EndIf

            Case $idMsg = $idInfobutton
                $idItem = GUICtrlRead($idTreeview) ; Get the controlID of the current selected treeview item
                If $idItem = 0 Then
                    MsgBox($MB_SYSTEMMODAL, "TreeView Demo", "No item currently selected")
                Else
                    $sText = GUICtrlRead($idItem, 1) ; Get the text of the treeview item
                    If $sText == "" Then
                        MsgBox($MB_SYSTEMMODAL, "Error", "Error while retrieving infos about item")
                    Else
                        MsgBox($MB_SYSTEMMODAL, "TreeView Demo", "Current item selected is: " & $sText)
                    EndIf
                EndIf

            Case $idMsg = $idStatebutton
                $idItem = GUICtrlRead($idTreeview)
                If $idItem > 0 Then
                    $hItem = GUICtrlGetHandle($idItem)
                    GUICtrlSendMsg($idTreeview, $TVM_EXPAND, $TVE_TOGGLE, $hItem)
                EndIf

                ; The following items will hide the other labels (1st and 2nd parameter) and then show the 'own' labels (3rd and 4th parameter)
            Case $idMsg >= $idGeneralitem and $idMsg <= $idLast
                GUICtrlSetData($lblMessage, $aData[$idMsg])
        EndSelect
    WEnd

    GUIDelete()
EndFunc   ;==>Example

Func AddTreevViewItem(ByRef $aItems, $idTreeView, $sText, $sMessage)
    Local $iId = GUICtrlCreateTreeViewItem($sText, $idTreeview)
    ReDim $aItems[$iId + 1]
    $aItems[$iId] = $sMessage
    Return $iId
EndFunc

 

Link to comment
Share on other sites

Agreed but if ever the IDs are not sequential or the OP want reordering things, an array index becomes unsuitable. I don't have enough information about the actual OP application to decide otherwise. Looking up a key in a map isn't going to take many cycles.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

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...