q410225 发表于 2016-12-17 23:53:22

[已解决]将数组的内容展示出来并可以添加删除修改

本帖最后由 q410225 于 2016-12-18 15:49 编辑

要实现的功能

1.将INI文件读取到一个二维数组里(已实现)

2.将这个二维数组展示到GUI界面上(已实现)

3.可以在这个GUI界面上对这个数组进行添加删除修改操作

现在卡在第三步上,只要删除,程序就会崩溃,用一个比喻的话,第三步是把第二步的地基抽掉,在做一个空中楼阁.大家看一下,这个流程该如何修改呢.

好吧,找到解决办法了

kk_lee69 发表于 2016-12-18 01:52:16

回复 1# q410225


    http://www.autoitx.com/forum.php?mod=viewthread&tid=28087&highlight=treeview

q410225 发表于 2016-12-18 10:25:16

回复 2# kk_lee69

不是这个问题,我想在treeview删除后,原来的数组也删除,比如我删除sub,那么数组里面sub这个一行也需要删除

问题是删除数组的值提取不到

kk_lee69 发表于 2016-12-18 15:55:25

回复 1# q410225

你的問題是....3.可以在这个GUI界面上对这个数组进行添加删除修改操作

那至少要先做到 在 GUI介面刪除 資料 才能 刪除 數組的資料 不是

你一開始的問題也是這個但是我看不出 你哪裡 有辦法刪除 GUI 的資料

所以你應該先做到 刪除 GUI 資料才對呀

所以我才給你看那個範例的......

不會有 那麼巧的事情 人家的想法邏輯 剛好跟你的一樣   所以有一樣的範例可以給你看

要不 你試著先別管數組先可以刪除 GUI 上面資料我看看   我在幫你補上數組段的...

q410225 发表于 2016-12-19 15:03:55

回复 4# kk_lee69
这是config.ini
;功能名称
1.Name=功能1
;类图标
1.ICon=1.ico
;是否默认展开(0/1,否/是)
1.expand=1


1.1.ex1=选中与否
1.1.ex2=名称
1.1.ex3=路径
1.1.ex4=参数
1.1.ex5=时间
1.1.ex6=隐藏


1.2.ex1=1
1.2.ex2=3
1.2.ex3=4
1.2.ex4=5
1.2.ex5=6



2.Name=功能2
2.Icon=2.ico
2.Expand=1


2.1.ex1=开关
2.1.ex2=名称
2.1.ex3=路径
2.1.ex4=参数
2.1.ex5=时间
2.1.ex6=隐藏


2.2.ex1=1
2.2.ex2=3
2.2.ex3=4
2.2.ex4=5
2.2.ex5=6




3.Name=功能3
3.Icon=3.ico
3.Expand=1


3.1.ex1=开关
3.1.ex2=名称
3.1.ex3=运行时间
3.1.ex4=灵敏度
3.1.ex5=映像名
3.1.ex6=路径


3.2.ex1=1
3.2.ex2=3
3.2.ex3=4
3.2.ex4=5
3.2.ex5=6




4.Name=功能4
4.Icon=4.ico
4.Expand=1

4.1.ex1=开关
4.1.ex2=名称
4.1.ex3=路径
4.1.ex4=参数
4.1.ex5=图标
4.1.ex6=图标编号


4.2.ex1=1
4.2.ex2=3
4.2.ex3=4
4.2.ex4=5
4.2.ex5=6



5.Name=功能5
5.Icon=5.ico
5.Expand=1


5.1.ex1=开关
5.1.ex2=路径
5.1.ex3=1
5.1.ex4=1
5.1.ex5=1


5.2.ex1=1
5.2.ex2=路径
5.2.ex3=1
5.2.ex4=1
5.2.ex5=1


#include <GuiTreeView.au3>
;~ #include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <ButtonConstants.au3>
#include <Array.au3>
#include <ComboConstants.au3>
#include <GUIConstantsEx.au3>

Global Const $inifile = @ScriptDir & "\config.ini"
Global $gu_form1 = GUICreate("", 592, 420, -1, -1)
Global $gu_Treeview1 = GUICtrlCreateTreeView(16, 16, 177, 342, BitOR($gui_ss_default_treeview, $tvs_checkboxes, $ws_border))
$gu_button1 = GUICtrlCreateButton("", 450, 370, 50, 30, $BS_FLAT)
$gu_button2 = GUICtrlCreateButton("", 510, 370, 50, 30, $BS_FLAT)
Global $up_form, $up_input, $up_button, $up_group = ["", "功能1", "功能2", "功能3", "功能4", "功能5"]
Local $z
For $z = 1 To 5
        $up_form[$z] = GUICreate("", 367, 345, 209, 13, $WS_POPUp, $WS_EX_MDICHILD, $gu_form1)
        GUICtrlCreateGroup($up_group[$z], 1, 1, 367, 345)
        $up_input[$z] = GUICtrlCreateInput("", 26, 50, 250, 20)
        $up_input[$z] = GUICtrlCreateInput("", 26, 85, 250, 20)
        $up_input[$z] = GUICtrlCreateInput("", 26, 120, 250, 20)
        $up_input[$z] = GUICtrlCreateInput("", 26, 150, 250, 20)
        $up_button[$z] = GUICtrlCreateButton("...", 281, 50, 30, 20, $BS_FLAT)
        $up_button[$z] = GUICtrlCreateButton("...", 281, 85, 30, 20, $BS_FLAT)
        $up_button[$z] = GUICtrlCreateButton("...", 281, 120, 30, 20, $BS_FLAT)
        $up_button[$z] = GUICtrlCreateButton("...", 281, 150, 30, 20, $BS_FLAT)
Next

GUISetState(@SW_SHOW, $gu_form1)


Global $Array = _IniToArray($inifile)

_main()
Exit
Func _main()
       
        _createtree($Array)
Local $aArray=$Array
        While 1
                Local $nMsg = GUIGetMsg()
               
                _TreeViewCTL($Array, $nMsg)
                _buttonCTL($aArray,$nMsg)
        WEnd
EndFunc   ;==>_main


Func _IniToArray($inifile)
        Local $Array, $p = 1
        Local $i = 1
        While 1
                Local $clsName = IniRead($inifile, $i, $i & ".Name", "")
                If $clsName = "" Then ExitLoop
                ReDim $Array[$p + 1]
                $Array[$p] = "root"
                $Array[$p] = 0
                $Array[$p] = IniRead($inifile, $i, $i & ".Name", "")
                $Array[$p] = IniRead($inifile, $i, $i & ".Icon", "")
                $Array[$p] = IniRead($inifile, $i, $i & ".expand", "")
                $p += 1
;~         开始读取子节点
                Local $j = 1
                While 1
                        Local $sw_path = IniRead($inifile, $i, $i & "." & $j & ".ex1", "")
                        If $sw_path = "" Then ExitLoop
                        ReDim $Array[$p + 1]
                        $Array[$p] = "sub"
                        $Array[$p] = IniRead($inifile, $i, $i & "." & $j & ".ex1", "")
                        $Array[$p] = IniRead($inifile, $i, $i & "." & $j & ".ex2", "")
                        $Array[$p] = IniRead($inifile, $i, $i & "." & $j & ".ex3", "")
                        $Array[$p] = IniRead($inifile, $i, $i & "." & $j & ".ex4", "")
                        $Array[$p] = IniRead($inifile, $i, $i & "." & $j & ".ex5", "")
                        $Array[$p] = IniRead($inifile, $i, $i & "." & $j & ".ex6", "")
                        $j += 1
                        $p += 1
                WEnd
                $i += 1
        WEnd
        $Array = UBound($Array, 1) - 1
        Return $Array
EndFunc   ;==>_IniToArray

Func _createtree(ByRef $Array)
        _GUICtrlTreeView_BeginUpdate($gu_Treeview1)
        Local $i
        For $i = 1 To $Array
                Local $root
                If $Array[$i] = "root" Then
                        $Array[$i] = GUICtrlCreateTreeViewItem($Array[$i], $gu_Treeview1)
                        $root = $Array[$i]
                Else
                        $Array[$i] = GUICtrlCreateTreeViewItem($Array[$i], $root)
                EndIf
        Next
        Local $i
        For $i = $Array To 1 Step -1
                If $Array[$i] = 1 Then
                        _GUICtrlTreeView_SetChecked($gu_Treeview1, $Array[$i], True)
                EndIf
                If $Array[$i] = "root" And $Array[$i] = 1 Then
                        _GUICtrlTreeView_Expand($gu_Treeview1, $Array[$i], True)

                EndIf
        Next
        _GUICtrlTreeView_EndUpdate($gu_Treeview1)
EndFunc   ;==>_createtree



Func _TreeViewCTL(ByRef $Array, $nMsg)
        Local $i
        For $i = 1 To $Array
                Switch $nMsg
                        Case $Array[$i]
                                _DisplaysGui($i, $Array)
                                ;主节点
                                If $Array[$i] = "root" Then
                                        If _GUICtrlTreeView_GetChecked($gu_Treeview1, $Array[$i]) = True _
                                                        And $Array[$i] = 0 Then
                                                $Array[$i] = 1
                                                Local $j
                                                For $j = $i + 1 To $Array
                                                        If $Array[$j] = "sub" Then
                                                                _GUICtrlTreeView_SetChecked($gu_Treeview1, $Array[$j], True)
                                                                $Array[$j] = 1
                                                        Else
                                                                ExitLoop
                                                        EndIf
                                                Next

                                        ElseIf _GUICtrlTreeView_GetChecked($gu_Treeview1, $Array[$i]) = False _
                                                        And $Array[$i] = 1 Then
                                                $Array[$i] = 0
                                                Local $j
                                                For $j = $i + 1 To $Array
                                                        If $Array[$j] = "sub" Then
                                                                _GUICtrlTreeView_SetChecked($gu_Treeview1, $Array[$j], False)
                                                                $Array[$j] = 0
                                                        Else
                                                                ExitLoop
                                                        EndIf

                                                Next
                                        EndIf
                                        ;节点,节点若被选中,主节点则被选中
                                ElseIf $Array[$i] = "sub" Then
                                        If _GUICtrlTreeView_GetChecked($gu_Treeview1, $Array[$i]) = True _
                                                        And $Array[$i] = 0 Then
                                                $Array[$i] = 1
                                                Local $j
                                                For $j = $i - 1 To 1 Step -1
                                                        If $Array[$j] = "root" Then
                                                                _GUICtrlTreeView_SetChecked($gu_Treeview1, $Array[$j], True)
                                                                $Array[$j] = 1
                                                                ExitLoop
                                                        EndIf

                                                Next
                                        ElseIf _GUICtrlTreeView_GetChecked($gu_Treeview1, $Array[$i]) = False _
                                                        And $Array[$i] = 1 Then
                                                $Array[$i] = 0
                                                Local $roothdl
                                                Local $flag = 0
                                                Local $j
                                                ;向前,查找其他节点状态
                                                For $j = $i + 1 To $Array
                                                        If $Array[$j] = "root" Then
                                                                ExitLoop
                                                        Else
                                                                If _GUICtrlTreeView_GetChecked($gu_Treeview1, $Array[$j]) = True Then
                                                                        $flag = 1
                                                                        ExitLoop
                                                                EndIf
                                                        EndIf
                                                Next
                                                ;向后查找其他节点状态
                                                For $j = $i - 1 To 1 Step -1
                                                        If $Array[$j] = "root" Then
                                                                $roothdl = $Array[$j]
                                                                ExitLoop
                                                        Else

                                                                If _GUICtrlTreeView_GetChecked($gu_Treeview1, $Array[$j]) = True Then
                                                                        $flag = 1
                                                                        ExitLoop
                                                                EndIf
                                                        EndIf
                                                Next
                                                If $flag = 0 Then
                                                        _GUICtrlTreeView_SetChecked($gu_Treeview1, $roothdl, False)
                                                        $Array[$j] = 0
                                                EndIf
                                        EndIf
                                EndIf
                EndSwitch
        Next

        ;显示视图的具体内容
EndFunc   ;==>_TreeViewCTL

Func _DisplaysGui($i, $Array)
        ;$j 归属
        ;$i 位置
        GUISetState(@SW_HIDE, $up_form)
        GUISetState(@SW_HIDE, $up_form)
        GUISetState(@SW_HIDE, $up_form)
        GUISetState(@SW_HIDE, $up_form)
        GUISetState(@SW_HIDE, $up_form)

        Local $j
        If $Array[$i] = "root" Then
                $j = $i
        Else
                For $j = $i To 1 Step -1
                        If $Array[$j] = "root" Then ExitLoop
                Next
        EndIf

        Switch $Array[$j]
                Case "功能1"
                        GUISetState(@SW_SHOW, $up_form)
                        GUICtrlSetData($up_button, "路径")
                        GUICtrlSetData($up_button, "参数")
                        GUICtrlSetData($up_button, "隐藏")
                        GUICtrlSetData($up_button, "延迟")
                        If $j = $i Then
                                GUICtrlSetData($gu_button2, "添加(&t)")
                                GUICtrlSetState($gu_button1, $GUI_DISABLE)
                        Else
                                GUICtrlSetState($gu_button1, $GUI_ENABLE)
                                GUICtrlSetData($gu_button2, "应用(&a)")
                                GUICtrlSetData($gu_button1, "删除(&t)")
                                GUICtrlSetData($up_input, $Array[$i])
                                GUICtrlSetData($up_input, $Array[$i])
                                GUICtrlSetData($up_input, $Array[$i])
                                GUICtrlSetData($up_input, $Array[$i])
                        EndIf
                Case "功能2"
                        GUISetState(@SW_SHOW, $up_form)
                        GUICtrlSetData($up_button, "路径")
                        GUICtrlSetData($up_button, "参数")
                        GUICtrlSetData($up_button, "隐藏")
                        GUICtrlSetData($up_button, "延迟")
                        If $j = $i Then
                                GUICtrlSetData($gu_button2, "添加(&t)")
                                GUICtrlSetState($gu_button1, $GUI_DISABLE)
                        Else
                                GUICtrlSetState($gu_button1, $GUI_ENABLE)
                                GUICtrlSetData($gu_button2, "应用(&a)")
                                GUICtrlSetData($gu_button1, "删除(&t)")
                                GUICtrlSetData($up_input, $Array[$i])
                                GUICtrlSetData($up_input, $Array[$i])
                                GUICtrlSetData($up_input, $Array[$i])
                                GUICtrlSetData($up_input, $Array[$i])
                        EndIf
                Case "功能3"

                        GUISetState(@SW_SHOW, $up_form)
                        GUICtrlSetData($up_button, "路径")
                        GUICtrlSetData($up_button, "参数")
                        GUICtrlSetData($up_button, "隐藏")
                        GUICtrlSetData($up_button, "延迟")
                        If $j = $i Then
                                GUICtrlSetData($gu_button2, "添加(&t)")
                                GUICtrlSetState($gu_button1, $GUI_DISABLE)
                        Else
                                GUICtrlSetState($gu_button1, $GUI_ENABLE)
                                GUICtrlSetData($gu_button2, "应用(&a)")
                                GUICtrlSetData($gu_button1, "删除(&t)")
                                GUICtrlSetData($up_input, $Array[$i])
                                GUICtrlSetData($up_input, $Array[$i])
                                GUICtrlSetData($up_input, $Array[$i])
                                GUICtrlSetData($up_input, $Array[$i])
                        EndIf

                Case "功能4"
                        GUISetState(@SW_SHOW, $up_form)
                        GUICtrlSetData($up_button, "路径")
                        GUICtrlSetData($up_button, "参数")
                        GUICtrlSetData($up_button, "隐藏")
                        GUICtrlSetData($up_button, "延迟")
                        If $j = $i Then
                                GUICtrlSetData($gu_button2, "添加(&t)")
                                GUICtrlSetState($gu_button1, $GUI_DISABLE)
                        Else
                                GUICtrlSetState($gu_button1, $GUI_ENABLE)
                                GUICtrlSetData($gu_button2, "应用(&a)")
                                GUICtrlSetData($gu_button1, "删除(&t)")
                                GUICtrlSetData($up_input, $Array[$i])
                                GUICtrlSetData($up_input, $Array[$i])
                                GUICtrlSetData($up_input, $Array[$i])
                                GUICtrlSetData($up_input, $Array[$i])
                        EndIf
                Case "功能5"
                        GUISetState(@SW_SHOW, $up_form)
                        GUICtrlSetData($up_button, "路径")
                        GUICtrlSetData($up_button, "参数")
                        GUICtrlSetData($up_button, "隐藏")
                        GUICtrlSetData($up_button, "延迟")
                        If $j = $i Then
                                GUICtrlSetData($gu_button2, "添加(&t)")
                                GUICtrlSetState($gu_button1, $GUI_DISABLE)
                        Else
                                GUICtrlSetState($gu_button1, $GUI_ENABLE)
                                GUICtrlSetData($gu_button2, "应用(&a)")
                                GUICtrlSetData($gu_button1, "删除(&t)")
                                GUICtrlSetData($up_input, $Array[$i])
                                GUICtrlSetData($up_input, $Array[$i])
                                GUICtrlSetData($up_input, $Array[$i])
                                GUICtrlSetData($up_input, $Array[$i])
                        EndIf
        EndSwitch
EndFunc   ;==>_DisplaysGui

Func _buttonCTL(ByRef $aArray,$nMsg)
       
        Local $i, $j

        For $j = 1 To UBound($Array)-1 ;8194
       If _GUICtrlTreeView_GetSelected($gu_Treeview1, $Array[$j]) Then ExitLoop
        Next

        For $i = 1 To 5
                Switch $nMsg
                        Case $up_button[$i]
                        Case $up_button[$i]
                        Case $up_button[$i]
                        Case $up_button[$i]
                EndSwitch
        Next
        Switch $nMsg
                Case $gu_button1
                        _GUICtrlTreeView_BeginUpdate($gu_Treeview1)
                        _GUICtrlTreeView_Delete($gu_Treeview1,$Array[$j])
                        _GUICtrlTreeView_EndUpdate($gu_Treeview1)
                Case $gu_button2
                Case $gui_event_close
                        Exit
        EndSwitch
EndFunc   ;==>_buttonCTL



我现在的做法是添加项目直接写入到ini里,但需要重新启动才能生效,

修改的话简单点,可以直接修改数组中的元素,然后删除ini,把数组写入到新的ini中

删除的话,是在数组中添加一个删除的标记,然后删除ini,把数组写入到新的ini中

删除和修改功能算是基本实现了,但添加功能就差点意思

我想问问有什么好的解决办法吗?

kk_lee69 发表于 2016-12-19 23:43:34

回复 5# q410225

不好意思 你寫的我實在 無法理解有點複雜

不過你既然問題出在 數組 問題   我用我的理解 寫了一個 數組的 新增 刪除的範例

勾選前請先看陣列
請一次 勾選一個 子項然後 按下刪除再看看陣列

然後 可以按下新增後 再看看陣列

P.S 陣列就是數組

希望 可以給你些 不一樣的想法

#include <GuiTreeView.au3>
;~ #include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <ButtonConstants.au3>
#include <Array.au3>
#include <ComboConstants.au3>
#include <GUIConstantsEx.au3>

Global Const $inifile = @ScriptDir & "\config.ini"
Global $gu_form1 = GUICreate("", 592, 420, -1, -1)
Global $gu_Treeview1 = GUICtrlCreateTreeView(16, 16, 177, 342, BitOR($gui_ss_default_treeview, $tvs_checkboxes, $ws_border))
$Button1 = GUICtrlCreateButton("刪除", 350, 370, 50, 30, $BS_FLAT)
$Button2 = GUICtrlCreateButton("插入一筆資料", 450, 370, 100, 30, $BS_FLAT)
$Button3 = GUICtrlCreateButton("顯示陣列", 250, 370, 80, 30, $BS_FLAT)

DIM $Array
$Array="A"
$Array=""
$Array="A"
$Array="A1"
$Array="A"
$Array="A2"
$Array="B"
$Array=""
$Array="B"
$Array="B1"
$Array="B"
$Array="B2"

_createtree($Array)


GUISetState(@SW_SHOW, $gu_form1)


While 1
      $nMsg = GUIGetMsg()
      Switch $nMsg
                Case -3
                        Exit
                Case $Button1
                        ;_GUICtrlTreeView_Expand($hTreeView)
                                                ;MsgBox(0,"",_GUICtrlTreeView_GetSelection($gu_Treeview1))
                                                $ACHK=""
                                                For $i=UBound($Array)-1TO 0 STEP -1
                                                        IF _GUICtrlTreeView_GetItemHandle($gu_Treeview1,$Array[$i])=_GUICtrlTreeView_GetSelection($gu_Treeview1) Then
                                                                MsgBox(0,"","你勾選了陣列的第"&$i&"個")
                                                                $ACHK=$i
                                                        EndIf
                                                       
                                                       
                                                Next
                                                _GUICtrlTreeView_Delete($gu_Treeview1,$Array[$ACHK])
                                                _ArrayDelete($Array,$ACHK)
                                               
                                               
                                               
                Case $Button2
                                        $NO=UBound($Array)
                                        ReDim $Array[$NO+3]
                                        $Array[$NO]="C"
                                        $Array[$NO]=""
                                        $Array[$NO+1]="C"
                                        $Array[$NO+1]="C1"
                                        $Array[$NO+2]="C"
                                        $Array[$NO+2]="C2"
                                        _GUICtrlTreeView_DeleteAll($gu_Treeview1)
                                        _createtree($Array)
                                       
                Case $Button3
                                        _ArrayDisplay($Array)

      EndSwitch
WEnd


Func _createtree(ByRef $Array)
      _GUICtrlTreeView_BeginUpdate($gu_Treeview1)
      Local $CHK=""
               
      For $i = 0 To UBound($Array)-1
                               
                        IF         $Array[$i]<>$CHK Then
                               
                               
               
                If $Array[$i] = "" Then
                  $AA=GUICtrlCreateTreeViewItem($Array[$i], $gu_Treeview1)
                                        $Array[$i]=$AA
                                EndIf
                        Else
                                If $Array[$i] <> "" Then
                  $Array[$i]=GUICtrlCreateTreeViewItem($Array[$i], $AA)
                                EndIf
                        EndIf

                        $CHK=$Array[$i]

      Next

      _GUICtrlTreeView_EndUpdate($gu_Treeview1)
EndFunc   ;==>_createtree
       

chzj589 发表于 2016-12-20 08:28:58

回复 6# kk_lee69

我想这跟数据库操作是一样的。
读取INI显示在ListView列表,点击ListView列表条目显示在编辑框,
修改编辑框后再保存。
添加:在编辑框添加数据后保存。

kk_lee69 发表于 2016-12-20 09:06:28

回复 7# chzj589

是阿   判斷出 是哪個 被勾選了   然後刪除數組 就好

q410225 发表于 2016-12-20 12:10:47

回复 6# kk_lee69


感谢,看了你的代码,我终于明白问题在那了,既然array已经改变,那就重新生成treeview好了,而我一直想修补一下treeview.

所以,只能修改array已经存在的元素,而添加删除就是动了地基,自然不能实现了.
页: [1]
查看完整版本: [已解决]将数组的内容展示出来并可以添加删除修改