TreeViewToListBox问题(已解决)
本帖最后由 qq342252004 于 2010-5-16 12:22 编辑#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GuiListBox.au3>
#include <GuiTreeView.au3>
#Include <Array.au3>
Global $iStyle = BitOR($TVS_EDITLABELS, $TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS, $TVS_CHECKBOXES)
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 428, 320, -1, -1)
$TvStrat = GUICtrlCreateTreeView(8, 8, 185, 279, $iStyle, $WS_EX_CLIENTEDGE)
Dim $Father, $Son,$Number
Dim $FatherArray =
Dim $SonArray = [,, _
,[3, "控制面板", _
"网络连接", "打印机传真"]]
For $i = 1 To 4
$hFatherItem = GUICtrlCreateTreeViewItem($FatherArray[$i], $TvStrat)
_ArrayAdd($Father, $hFatherItem)
For $j = 1 To $SonArray[$i - 1]
$hSonItem = GUICtrlCreateTreeViewItem($SonArray[$i-1][$j], $hFatherItem)
_ArrayAdd($Son, $hSonItem)
Next
Next
$LsStrat = GUICtrlCreateList("", 224, 8, 193, 279)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
GUIDelete($Form1)
Exit
Case $Father To $Father, $Son To $Son
For $i = 0 To UBound($Father) - 1 ;父项目操作
If $nMsg = $Father[$i] And BitAND(GUICtrlRead($Father[$i]), $GUI_CHECKED) Then
$Number = Number(_GUICtrlTreeView_GetChildCount($TvStrat, $Father[$i])) ;全选
For $m = $Father[$i] To $Father[$i] + $Number
GUICtrlSetState($m, $GUI_CHECKED)
Next
ElseIf $nMsg = $Father[$i] And BitAND(GUICtrlRead($Father[$i]), $GUI_UNCHECKED) Then ;反选
$Number = Number(_GUICtrlTreeView_GetChildCount($TvStrat, $Father[$i]))
For $n = $Father[$i] To $Father[$i] + $Number
GUICtrlSetState($n, $GUI_UNCHECKED)
Next
EndIf
Next
For $z = 0 To UBound($Son) - 1 ;子项目操作
If $nMsg = $Son[$z] And BitAND(GUICtrlRead($Son[$z]), $GUI_CHECKED) Then ;子项目勾选,父项目跟着勾选
$Handle = _GUICtrlTreeView_GetParentParam($TvStrat, $Son[$z])
GUICtrlSetState($Handle, $GUI_CHECKED)
ElseIf $nMsg = $Son[$z] And BitAND(GUICtrlRead($Son[$z]), $GUI_UNCHECKED) Then
$Handle = _GUICtrlTreeView_GetParentParam($TvStrat, $Son[$z])
$Number = Number(_GUICtrlTreeView_GetChildCount($TvStrat, $Handle))
For $o = $Handle To $Handle + $Number
If GUICtrlRead($o) = $GUI_CHECKED Then ExitLoop
If $o = $Handle + $Number Then GUICtrlSetState($Handle, $GUI_UNCHECKED) ;子项目全部取消勾选,父项目也跟着取消勾选
Next
EndIf
Next
_TreeViewToListBox($TvStrat, $LsStrat)
EndSwitch
WEnd
Func _TreeViewToListBox($TreeView, $ListBox)
;说明
;能把TreeView中复选框选中的项目显示到指定的ListBox中
;参数
;$TreeView -----TreeView句柄
; $ListBox -----ListBox句柄
Local $TVItemString = "" ;获取TreeView中选择的Text
Local $LsIndex ;ListBox的项目数量
Local $TVSubItemString = _GUICtrlTreeView_GetFirstItem($TreeView) ;获取TreeView中第一个项目的句柄
Local $TreeViewIndexs = _GUICtrlTreeView_GetCount($TreeView) ;获取TreeView中项目的数量
For $i = 0 To $TreeViewIndexs - 1 ;循环项目数量
If _GUICtrlTreeView_GetChecked($TreeView, $TVSubItemString) = True Then ;如果TreeView该项目的复选框被选择
$TVItemString = _GUICtrlTreeView_GetText($TreeView, $TVSubItemString) ;获取TreeView被选择的项目的文本
If _GUICtrlListBox_FindInText($ListBox, $TVItemString) = -1 Then
_GUICtrlListBox_AddString($ListBox, $TVItemString)
EndIf
Else ;如果该项目的复选框不被选择
$TVItemString = _GUICtrlTreeView_GetText($TreeView, $TVSubItemString) ;获取TreeView该项目的文本
If _GUICtrlListBox_FindInText($ListBox, $TVItemString) <> -1 Then
_GUICtrlListBox_DeleteString($ListBox, _GUICtrlListBox_FindInText($ListBox, $TVItemString)) ;删除掉存在的字符串
EndIf
EndIf
$TVSubItemString = _GUICtrlTreeView_GetNext($TreeView, $TVSubItemString) ;吧TreeView句柄移至下一个
Next
EndFunc ;==>_TreeViewToListBox如何才能让父项目勾选时不加进ListBox? 这代码看起来怎么这么眼熟啊!
呵呵!猜都会遇到这样的问题。 有部分是你的! 我原来就在奇怪你怎么在LISTBOX中也显示父项的内容呢。
改一下自定义函数就行。
Func _TreeViewToListBox($TreeView, $ListBox)
For $i = 1 To UBound($Son) - 1
If BitAND(GUICtrlRead($Son[$i]), $GUI_CHECKED) Then
$Item = _GUICtrlListBox_FindInText($ListBox, GUICtrlRead($Son[$i], 1))
If $Item = -1 Then
GUICtrlSetData($ListBox, GUICtrlRead($Son[$i], 1) & "|")
EndIf
Else
$Item = _GUICtrlListBox_FindInText($ListBox, GUICtrlRead($Son[$i], 1))
If $Item <> -1 Then
_GUICtrlListBox_DeleteString($ListBox, $Item)
EndIf
EndIf
Next
EndFunc 其实比较简单,之前我是将父项目和子项目分别列入2个数组中的。
判断的时候只需要检查子项目那个数组里的项目就可以了。(嘿嘿!是不是很简单啊!) 其实比较简单,之前我是将父项目和子项目分别列入2个数组中的。
判断的时候只需要检查子项目那个数组里的项 ...
水木子 发表于 2010-5-10 14:42 http://www.autoitx.com/images/common/back.gif
看你的说法倒是方便,给个例子出来的话会更方便的!:face (33):
页:
[1]