请教ListView 控件项目操作问题“已解决”
本帖最后由 nuoyan 于 2021-9-16 19:31 编辑#include <File.au3>
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <ListViewConstants.au3>
#include <StaticConstants.au3>
#include <TreeViewConstants.au3>
#include <WindowsConstants.au3>
Opt("GUIDataSeparatorChar", ",")
DirCreate(@ScriptDir & "\项目1")
DirCreate(@ScriptDir & "\项目2")
IniWrite(@ScriptDir & "\项目1\Config.ini", "项目", "数据", "项目一测试1,项目一测试2,项目一测试3")
IniWrite(@ScriptDir & "\项目2\Config.ini", "项目", "数据", "项目二测试4,项目二测试5,项目二测试6")
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 610, 511)
$TreeView1 = GUICtrlCreateTreeView(16, 16, 161, 473, BitOR($TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS), $WS_EX_CLIENTEDGE)
$generalitem1 = GUICtrlCreateTreeViewItem("项目列表", $TreeView1)
GUICtrlSetColor(-1, 0x0000C0)
$FileList = _FileListToArray(@ScriptDir & "\", "*", 2)
For $y = 1 To $FileList
$Child = GUICtrlCreateTreeViewItem($FileList[$y], $generalitem1)
Next
$Label1 = GUICtrlCreateLabel("Label1", 256, 40, 108, 25)
$ListView1 = GUICtrlCreateListView("项目数据 ", 232, 80, 153, 281)
$Input1 = GUICtrlCreateInput("", 232, 376, 153, 21)
$Button1 = GUICtrlCreateButton("添加", 232, 416, 75, 25)
$Button2 = GUICtrlCreateButton("删除", 320, 416, 75, 25)
GUICtrlSetState($generalitem1, BitOR($GUI_EXPAND, $GUI_DEFBUTTON))
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
Case $Child - $FileList To $Child
If GUICtrlRead($nMsg, 1) = "项目列表" Then
GUICtrlSetData($Label1, "")
Else
GUICtrlSetData($Label1, GUICtrlRead($nMsg, 1))
$Single = IniRead(@ScriptDir & "\" & GUICtrlRead($nMsg, 1) & "\Config.ini", "项目", "数据", "")
If $Single <> "" Then
$aDays = StringSplit($Single, ",")
For $a = 1 To $aDays
GUICtrlCreateListViewItem($aDays[$a], $ListView1)
Next
EndIf
EndIf
Case $Button1
If GUICtrlRead($Input1, 0) = "" Then
MsgBox(16, "错误", "请输入要添加的数据。")
Else
GUICtrlCreateListViewItem(GUICtrlRead($Input1, 0), $ListView1)
EndIf
Case $Button2
GUICtrlDelete(GUICtrlRead($ListView1))
MsgBox(64, "提示", "删除成功。")
EndSwitch
WEnd
请教下,项目1和项目2之间切换的时候,怎么才能先清空ListView 控件之前读取的项目,还有添加和删除操作要怎么把 ListView 控件当前剩余显示的子项读取出来以逗号分隔的形式重新写回对应的INI文件。
老生常谈了,论坛上关于 ListView 操作的示例有很多,楼主可以搜索下。 操作:
1:清空列表=GUICtrlSendMsg($ListView1, $LVM_DELETEALLITEMS, 0, 0)
2:判断树型项目是否等于项目1或项目2
3:删除配置文件
4:读取ListView1列表数据
5:重新写入。
chzj589 发表于 2021-9-15 09:43
操作:
1:清空列表=GUICtrlSendMsg($ListView1, $LVM_DELETEALLITEMS, 0, 0)
2:判断树型项目是否等于项 ...
本帖最后由 nuoyan 于 2021-9-17 12:57 编辑
chzj589 发表于 2021-9-17 10:21
现在就是卡在 点击添加后,将列表中返回的数组文本“项目二测试4”“项目二测试5”
“项目二测试6” “BBBB” 以逗号分隔的形式再写回INI文件
比如像下面这个例子 ,ini 写入的结果如何改为下面这种格式
222=a,b,c,d,e
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <ListViewConstants.au3>
#include <WindowsConstants.au3>
#include <GuiListView.au3>
#include <array.au3>
#include <File.au3>
#include <Array.au3>
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 220, 395)
$ListView1 = GUICtrlCreateListView("row", 30, 20, 156, 326)
$ListView1_0 = GUICtrlCreateListViewItem("a", $ListView1)
$ListView1_1 = GUICtrlCreateListViewItem("b", $ListView1)
$ListView1_2 = GUICtrlCreateListViewItem("c", $ListView1)
$ListView1_3 = GUICtrlCreateListViewItem("d", $ListView1)
$ListView1_4 = GUICtrlCreateListViewItem("e", $ListView1)
$Button1 = GUICtrlCreateButton("确定", 70, 355, 65, 21)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
Case $Button1
Local $aArray
For $i = 0 To _GUICtrlListView_GetItemCount($ListView1) - 1
Local $aItem = _GUICtrlListView_GetItemTextArray($ListView1, $i)
For $j = 1 To UBound($aItem) - 1
$aArray[$i][$j - 1] = $aItem[$j]
Next
Next
IniWrite(@ScriptDir & "\111.ini", "111", "222", _ArrayToString($aArray, "", 0, UBound($aArray) - 1))
EndSwitch
WEnd
nuoyan 发表于 2021-9-17 12:49
现在就是卡在 点击添加后,将列表中返回的数组文本“项目二测试4”“项目二测试5”
“项目二测试6 ...
3:删除配置文件
4:读取ListView1列表数据
5:重新写入。 chzj589 发表于 2021-9-17 12:51
3:删除配置文件
4:读取ListView1列表数据
5:重新写入。
要的结果不是删除配置文件,,读ListView1 已经读到了。返回的的值是数组值,怎么将数组值再以逗号相隔的形式写入到回去。 nuoyan 发表于 2021-9-17 13:01
要的结果不是删除配置文件,,读ListView1 已经读到了。返回的的值是数组值,怎么将数组值再以逗号相隔的 ...
Local $cont = _GUICtrlListView_GetItemCount($ListView1);取得数组的行列数
Local $h1 = ""
For $id = 0 To $cont - 1
$str = _GUICtrlListView_GetItemTextArray($ListView1, $id)
$h1 &= $str & ","
Next
Local $sString = StringTrimRight($h1, 1) ; 删除字符串右起 1个字符.
IniWrite(@ScriptDir & "\项目1\Config.ini", "项目", "数据", $sString) nuoyan 发表于 2021-9-17 13:01
要的结果不是删除配置文件,,读ListView1 已经读到了。返回的的值是数组值,怎么将数组值再以逗号相隔的 ...
我想你还没有理解我的:
2:判断树型项目是否等于项目1或项目2。没有判断项目1或项目2,就无法得到配置文件的路径
3:删除配置文件。不删除的话,如是增加可以写入。如是减少那就没用了。
4:读取ListView1列表数据
5:重新写入配置文件。
nuoyan 发表于 2021-9-17 13:01
要的结果不是删除配置文件,,读ListView1 已经读到了。返回的的值是数组值,怎么将数组值再以逗号相隔的 ...
把你的代码改一下
#include <File.au3>
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <GUIListView.au3>
#include <ListViewConstants.au3>
#include <StaticConstants.au3>
#include <TreeViewConstants.au3>
#include <WindowsConstants.au3>
Opt("GUIDataSeparatorChar", ",")
DirCreate(@ScriptDir & "\项目1")
DirCreate(@ScriptDir & "\项目2")
;IniWrite(@ScriptDir & "\项目1\Config.ini", "项目", "数据", "项目一测试1,项目一测试2,项目一测试3")
;IniWrite(@ScriptDir & "\项目2\Config.ini", "项目", "数据", "项目二测试4,项目二测试5,项目二测试6")
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 610, 511)
$TreeView1 = GUICtrlCreateTreeView(16, 16, 161, 473, BitOR($TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS), $WS_EX_CLIENTEDGE)
$generalitem1 = GUICtrlCreateTreeViewItem("项目列表", $TreeView1)
GUICtrlSetColor(-1, 0x0000C0)
$FileList = _FileListToArray(@ScriptDir & "\", "*", 2)
For $y = 1 To $FileList
$Child = GUICtrlCreateTreeViewItem($FileList[$y], $generalitem1)
Next
$Label1 = GUICtrlCreateLabel("Label1", 256, 40, 108, 25)
$ListView1 = GUICtrlCreateListView("项目数据 ", 232, 80, 153, 281)
$Input1 = GUICtrlCreateInput("", 232, 376, 153, 21)
$Button1 = GUICtrlCreateButton("添加", 232, 416, 75, 25)
$Button2 = GUICtrlCreateButton("删除", 320, 416, 75, 25)
GUICtrlSetState($generalitem1, BitOR($GUI_EXPAND, $GUI_DEFBUTTON))
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
Case $Child - $FileList To $Child
If GUICtrlRead($nMsg, 1) = "项目列表" Then
GUICtrlSetData($Label1, "")
Else
GUICtrlSetData($Label1, GUICtrlRead($nMsg, 1))
$Single = IniRead(@ScriptDir & "\" & GUICtrlRead($nMsg, 1) & "\Config.ini", "项目", "数据", "")
If $Single <> "" Then
$aDays = StringSplit($Single, ",")
GUICtrlSendMsg($ListView1, $LVM_DELETEALLITEMS, 0, 0)
For $a = 1 To $aDays
GUICtrlCreateListViewItem($aDays[$a], $ListView1)
Next
EndIf
EndIf
Case $Button1
If GUICtrlRead($Input1, 0) = "" Then
MsgBox(16, "错误", "请输入要添加的数据。")
Else
GUICtrlCreateListViewItem(GUICtrlRead($Input1, 0), $ListView1)
EndIf
$Lab=GUICtrlRead($Label1)
FileDelete(@ScriptDir & "\" & $Lab & "\Config.ini")
Local $cont = _GUICtrlListView_GetItemCount($ListView1);取得数组的行列数
Local $h1 = ""
For $id = 0 To $cont - 1
$str = _GUICtrlListView_GetItemTextArray($ListView1, $id)
$h1 &= $str & ","
Next
Local $sString = StringTrimRight($h1, 1) ; 删除字符串右起 1个字符.
IniWrite(@ScriptDir & "\" & $Lab & "\Config.ini", "项目", "数据", $sString)
Case $Button2
GUICtrlDelete(GUICtrlRead($ListView1))
MsgBox(64, "提示", "删除成功。")
$Lab=GUICtrlRead($Label1)
FileDelete(@ScriptDir & "\" & $Lab & "\Config.ini")
Local $cont = _GUICtrlListView_GetItemCount($ListView1);取得数组的行列数
Local $h1 = ""
For $id = 0 To $cont - 1
$str = _GUICtrlListView_GetItemTextArray($ListView1, $id)
$h1 &= $str & ","
Next
Local $sString = StringTrimRight($h1, 1) ; 删除字符串右起 1个字符.
IniWrite(@ScriptDir & "\" & $Lab & "\Config.ini", "项目", "数据", $sString)
EndSwitch
WEnd
页:
[1]