Hi,
mit TreeViews habe ich kaum Erfahrung und wollte ein sortiertes 2D Array in ein Treeview abbilden.
Das Array sieht so aus und ist auch als Datei verfügbar (Path.txt
Array.png
Spalte 0 bildet die Wurzel ab und hier gibt es nur "EMEA". Spalte 9 bildet das letzte Element ab. Spalte 7 ist zu ignorieren und dient nur für die Sortierung des Arrays.
Das Array wird aus einer Excel Tabelle erstellt.
Hier ein Beispiel Code:
#include <Array.au3>
#include <GUIConstantsEx.au3>
#include <GuiTreeView.au3>
#include <WindowsConstants.au3>
Example()
Func Example()
Local $hItem, $iTree, $x, $y, $iParent
Local $iStyle = BitOR($TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS)
GUICreate("TreeView Add First", 600, 800)
$iTree = GUICtrlCreateTreeView(2, 2, 596, 768, $iStyle, $WS_EX_CLIENTEDGE)
GUISetState(@SW_SHOW)
Local $aData = StringSplit2D(FileRead(@ScriptDir & "\Path.txt"))
_GUICtrlTreeView_BeginUpdate($iTree)
Local $aTmp[UBound($aData, 2)]
For $y = 1 To UBound($aData) - 1
For $x = 0 To UBound($aData, 2) - 1
If $x = 7 Then ContinueLoop
Next
Next
_GUICtrlTreeView_EndUpdate($iTree)
; Loop until the user exits.
Do
Until GUIGetMsg() = $GUI_EVENT_CLOSE
GUIDelete()
EndFunc ;==>Example
; #FUNCTION# ========================================================================================================================================
; Name .................: StringSplit2D()
; Description ..........: Splits a string into columns instead of rows as it is done by SplitString(), like a csv file to a 2d array ;-)
; Syntax ...............: StringSplit2D($sString[, $sDelimiter = ";"[, $bStripWS = True[, $sQuotationMark = '"'[, $sDummy = "¦"[, $iWidthLen = 256]]]]])
; Parameters ...........: $sString - A string value.
; $sDelimiter - [optional] A string value. Default is ";".
; $bStripWS - [optional] A binary value. Default is True.
; $sQuotationMark - [optional] A string value. Default is '"'.
; $sDummy - [optional] A string value. Default is "¦".
; $iWidthLen - [optional] An integer value. Default is 256.
; Return values .......: Success - 2d array
; Error 1 - $sStringis not set
; Error 2 - $delimter is not set
; Error 3 - error splitting string
; Error 4 - array width exceeded
;
; Version .............: v0.97 build 2015-03-03 beta
; Author ..............: UEZ
; Modified ............:
; Comment .............: former StringSplitW
; Remarks .............: RegEx take from http://stackoverflow.com/questions/4476812/regular-expressions-how-to-replace-a-character-within-quotes
; Related .............: StringSplit, StringReplace, StringRegExpReplace, StringLen, StringStripCR
; ===================================================================================================================================================
Func StringSplit2D($sString, $sDelimiter = ";", $bStripWS = False, $sQuotationMark = '"', $sDummy = "¦", $iWidthLen = 256)
If $sString = "" Then Return SetError(1, 0, 0)
If $sDelimiter = "" Then Return SetError(2, 0, 0)
Local $aSplit = StringSplit(StringStripCR($sString), @LF)
If @error Then Return SetError(3, 0, 0)
Local $chk, $iWidth, $i, $j, $k, $iLen, $iMax = 1, $iMaxWidth, $iSum = 0
Local $aPos[1], $l = 0
Local $aVertical[$aSplit[0]][$iWidthLen], $iDelimiterLen = StringLen($sDelimiter) - 1, $sLine
For $k = 1 To $aSplit[0]
$iLen = StringLen($aSplit[$k])
If $iLen > 1 Then
Switch $bStripWS
Case True
$sLine = StringRegExpReplace($aSplit[$k], '(?m)\' & $sDelimiter & '(?=[^' & $sQuotationMark & ']*' & $sQuotationMark & '(?:[^' & $sQuotationMark & '\r\n]*' & $sQuotationMark & '[^' & $sQuotationMark & ']*' & $sQuotationMark & ')*[^' & $sQuotationMark & '\r\n]*$)', $sDummy)
Case Else ; ,(?=[^"]*"(?:[^"\r\n]*"[^"]*")*[^"\r\n]*$)
$sLine = StringRegExpReplace(StringStripWS($aSplit[$k], 4), '(?m)\' & $sDelimiter & '(?=[^' & $sQuotationMark & ']*' & $sQuotationMark & '(?:[^' & $sQuotationMark & '\r\n]*' & $sQuotationMark & '[^' & $sQuotationMark & ']*' & $sQuotationMark & ')*[^' & $sQuotationMark & '\r\n]*$)', $sDummy)
EndSwitch
$chk = StringReplace($sLine, $sDelimiter, $sDelimiter)
$iWidth = @extended
$iSum += $iWidth
If $iWidth > $iWidthLen Then Return SetError(4, 0, 0)
If $iWidth >= $iMax Then $iMax = $iWidth + 1
Switch $iWidth
Case 0
$aVertical[$l][0] = $sLine
Case Else
Dim $aPos[$iWidth * 2 + 2]
$j = 1
$aPos[0] = 1
For $i = 0 To $iWidth - 1
$aPos[$j] = StringInStr($sLine, $sDelimiter, 0, $i + 1) - 1
$aPos[$j + 1] = $aPos[$j] + 2 + $iDelimiterLen
$j += 2
Next
$aPos[UBound($aPos) - 1] = StringLen($sLine)
$j = 0
For $i = 0 To UBound($aPos) - 1 Step 2
$aVertical[$l][$j] = StringReplace(StringMid(StringReplace($sLine, $sDummy, $sDelimiter), $aPos[$i], $aPos[$i + 1] - $aPos[$i] + 1), '""', "")
;~ ConsoleWrite($aVertical[$l][$j] & @CRLF)
$j += 1
Next
EndSwitch
$l += 1
EndIf
Next
ReDim $aVertical[$l][$iMax]
Return $aVertical
EndFunc
Alles anzeigen
Hat jemand eine Idee, wie ich effizient die Tabelle als Treeview abbilden kann?
So etwa soll es aussehen:
+EMEA
_____+DE
________+Frankfurt
_________________+FFM.A
_______________________+00
__________________________+Carrier-4-D
_____________________________________+A-Carrier-4D
_________________________________________________+AC
____________________________________________________+12 (letzes Element)
...
_________________+FFM.B
_______________________+01
...
Danke!