(已解决)数组 标识符 引起的 事件问题,新手求指导!
本帖最后由 imutraveler 于 2016-8-27 21:41 编辑因为要从数据库中读取分组列表,需要用循环,所以标识符$GroupItem用的数组,来记录读出来的所有数据!
然后点击每个分组,就把每个分组下的具体项输出到listview中
代码中,case那里,$GroupItem和$GroupItem这样分开写,就没问题,
只要case后面写$GroupItem[$i] 就会报错,应该是提示下标超了。
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
Case $GroupItem[$i]
_GUICtrlListView_DeleteAllItems($ListView)
Call(ConnectDB)
$RS = ObjCreate("ADODB.Recordset")
$RS.ActiveConnection = $Conn
$RS.Open("Select * From " & $TableServer & " Where Groups='" & _GUICtrlTreeView_GetText($TreeView, $GroupItem[$i]) & "'")
While Not $RS.eof And Not $RS.bof
If @error = 1 Then ExitLoop
GUICtrlCreateListViewItem($RS.Fields(1).value & "|" & $RS.Fields(7).value & "|" & $RS.Fields(2).value & "|" & $RS.Fields(3).value & "|" & $RS.Fields(9).value, $ListView )
$RS.movenext
WEnd
$RS.close
$Conn.Close
EndSwitch
WEnd
应该怎么解决?实际使用中,分组列表会非常多,肯定不可能一条一条写case匹配
这是能正常执行的!#Region ;**** 由 AccAu3Wrapper_GUI 创建指令 ****
#AccAu3Wrapper_OutFile=Client.exe
#AccAu3Wrapper_UseUpx=Y
#AccAu3Wrapper_UseX64=n
#AccAu3Wrapper_Res_Comment=
#AccAu3Wrapper_Res_Description=
#AccAu3Wrapper_Res_Fileversion=1.0
#AccAu3Wrapper_Res_LegalCopyright=
#AccAu3Wrapper_Res_Language=2052
#AccAu3Wrapper_Res_requestedExecutionLevel=None
#EndRegion ;**** 由 AccAu3Wrapper_GUI 创建指令 ****
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <GuiStatusBar.au3>
#include <WindowsConstants.au3>
#include <StaticConstants.au3>
#include <GuiListView.au3>
#include <ListViewConstants.au3>
#include <GuiTreeView.au3>
#include <TreeViewConstants.au3>
#include <GuiImageList.au3>
#include <GuiButton.au3>
#include <String.au3>
; 定义变量
Global $DbPath, $DbPass, $TableGroup, $TableServer, $TableUsers, $Conn
Global $GroupItem
$Form1 = GUICreate("控制台", 960, 615)
; 创建 TreeView
$TreeView = GUICtrlCreateTreeView(10, 50, 235, 508, BitOR($TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS), $WS_EX_CLIENTEDGE)
$Group = GUICtrlCreateTreeViewItem("已分组列表", $TreeView) ;创建TreeView项目
GUICtrlSetColor(-1, 0xff0000)
GUICtrlSetState(-1, BitOR($GUI_EXPAND, $GUI_DEFBUTTON))
; 创建 ListView
$ListView = GUICtrlCreateListView("", 250, 50, 700, 310)
_GUICtrlListView_AddColumn($ListView, "名称", 140) ;创建列
_GUICtrlListView_AddColumn($ListView, "隶属分组", 120) ;创建列
_GUICtrlListView_AddColumn($ListView, "IP", 110) ;创建列
_GUICtrlListView_AddColumn($ListView, "端口", 60) ;创建列
_GUICtrlListView_AddColumn($ListView, "备注", 240) ;创建列
_GUICtrlListView_SetExtendedListViewStyle($ListView, BitOR($LVS_EX_FULLROWSELECT, $LVS_EX_GRIDLINES)) ;设置ListView样式
GUISetState(@SW_SHOW)
; 读取 分组列表
Call(ConnectDB)
$RS = ObjCreate("ADODB.Recordset")
$RS.ActiveConnection = $Conn
$RS.Open("Select * From " & $TableGroup & " Order By GroupName")
$i = 0
While Not $RS.eof And Not $RS.bof
If @error = 1 Then ExitLoop
$GroupItem[$i] = GUICtrlCreateTreeViewItem($RS.Fields(1).value, $Group)
;~ MsgBox(0, "", _GUICtrlTreeView_GetText($TreeView, $GroupItem[$i]))
$i += 1
$RS.movenext
WEnd
$RS.close
$Conn.Close
_GUICtrlTreeView_Expand($TreeView, $Group)
_GUICtrlTreeView_SelectItemByIndex($TreeView, $Group, 0)
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
Case $GroupItem
_GUICtrlListView_DeleteAllItems($ListView)
Call(ConnectDB)
$RS = ObjCreate("ADODB.Recordset")
$RS.ActiveConnection = $Conn
$RS.Open("Select * From " & $TableServer & " Where Groups='" & _GUICtrlTreeView_GetText($TreeView, $GroupItem) & "'")
While Not $RS.eof And Not $RS.bof
If @error = 1 Then ExitLoop
GUICtrlCreateListViewItem($RS.Fields(1).value & "|" & $RS.Fields(7).value & "|" & $RS.Fields(2).value & "|" & $RS.Fields(3).value & "|" & $RS.Fields(9).value, $ListView )
$RS.movenext
WEnd
$RS.close
$Conn.Close
Case $GroupItem
_GUICtrlListView_DeleteAllItems($ListView)
Call(ConnectDB)
$RS = ObjCreate("ADODB.Recordset")
$RS.ActiveConnection = $Conn
$RS.Open("Select * From " & $TableServer & " Where Groups='" & _GUICtrlTreeView_GetText($TreeView, $GroupItem) & "'")
While Not $RS.eof And Not $RS.bof
If @error = 1 Then ExitLoop
GUICtrlCreateListViewItem($RS.Fields(1).value & "|" & $RS.Fields(7).value & "|" & $RS.Fields(2).value & "|" & $RS.Fields(3).value & "|" & $RS.Fields(9).value, $ListView )
$RS.movenext
WEnd
$RS.close
$Conn.Close
EndSwitch
WEnd
;=============== 函数 - 定义 ===============
Func ConnectDB() ;连接数据库
$DbPath = @ScriptDir & "\" & "Data.mdb"
$DbPass = "123"
$TableGroup = "TB_Group"
$TableServer = "TB_Server"
$TableUsers = "TB_Users"
$Conn = ObjCreate("ADODB.Connection")
$Conn.Open("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & $DbPath & "; Jet OLEDB:Database Password=" & $DbPass)
EndFunc ;==>ConnectDB
回复 1# imutraveler
改进的关键地方已添加注释, 注意看吧.#Region ;**** 参数创建于 ACNWrapper_GUI ****
#PRE_UseX64=n
#PRE_Res_requestedExecutionLevel=None
#EndRegion ;**** 参数创建于 ACNWrapper_GUI ****
#Region ;**** 由 AccAu3Wrapper_GUI 创建指令 ****
#AccAu3Wrapper_OutFile=Client.exe
#AccAu3Wrapper_UseUpx=Y
#AccAu3Wrapper_UseX64=n
#AccAu3Wrapper_Res_Comment=
#AccAu3Wrapper_Res_Description=
#AccAu3Wrapper_Res_Fileversion=1.0
#AccAu3Wrapper_Res_LegalCopyright=
#AccAu3Wrapper_Res_Language=2052
#AccAu3Wrapper_Res_requestedExecutionLevel=None
#EndRegion ;**** 由 AccAu3Wrapper_GUI 创建指令 ****
#include <array.au3>
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <GuiStatusBar.au3>
#include <WindowsConstants.au3>
#include <StaticConstants.au3>
#include <GuiListView.au3>
#include <ListViewConstants.au3>
#include <GuiTreeView.au3>
#include <TreeViewConstants.au3>
#include <GuiImageList.au3>
#include <GuiButton.au3>
#include <String.au3>
;~ #include <Constants.au3>;响应托盘事件 UDF
; 定义变量
Global $DbPath, $DbPass, $TableGroup, $TableServer, $TableUsers, $Conn
Global $TrItem, $iS = 0 ; itemsquence 分组序数
$Form1 = GUICreate("控制台", 960, 615)
; 创建 TreeView
$TreeView = GUICtrlCreateTreeView(10, 50, 235, 508, BitOR($TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS), $WS_EX_CLIENTEDGE)
$Group = GUICtrlCreateTreeViewItem("已分组服务器列表", $TreeView) ;创建TreeView项目
GUICtrlSetColor(-1, 0xff0000)
GUICtrlSetState(-1, BitOR($GUI_EXPAND, $GUI_DEFBUTTON))
; 读取 分组列表
ConnectDB()
$RS = ObjCreate("ADODB.Recordset")
$RS.ActiveConnection = $Conn
$RS.Open("Select * From " & $TableGroup & " Order By GroupName")
While Not $RS.eof And Not $RS.bof
If @error Then ExitLoop
ReDim $TrItem[$iS+1] ; 数组大小 +1
$TrItem[$iS] = GUICtrlCreateTreeViewItem($RS.Fields(1).value, $Group)
;~ MsgBox(0, "", _GUICtrlTreeView_GetText($TreeView, $TrItem[$iS]))
$iS += 1
$RS.movenext
WEnd
$RS.close
$Conn.Close
_GUICtrlTreeView_Expand($TreeView, $Group)
_GUICtrlTreeView_SelectItemByIndex($TreeView, $Group, 0)
; 创建 ListView
$ListView = GUICtrlCreateListView("", 250, 50, 700, 310)
_GUICtrlListView_AddColumn($ListView, "名称", 140) ;创建列
_GUICtrlListView_AddColumn($ListView, "隶属分组", 120) ;创建列
_GUICtrlListView_AddColumn($ListView, "IP", 110) ;创建列
_GUICtrlListView_AddColumn($ListView, "端口", 60) ;创建列
_GUICtrlListView_AddColumn($ListView, "备注", 240) ;创建列
_GUICtrlListView_SetExtendedListViewStyle($ListView, BitOR($LVS_EX_FULLROWSELECT, $LVS_EX_GRIDLINES)) ;设置ListView样式
GUISetState(@SW_SHOW)
#cs
; $TrItem[$nMsg - 5] 为什么要减 5 ?
; 每个GUI创建的第一个控件ID都是 3
, 这个GUI里就是 $TreeView = GUICtrlCreateTreeView ,也就是说 $TreeView = 3
; 下一控件就是 4 , $Group = GUICtrlCreateTreeViewItem $Group = 4
; 接着的控件ID 5 .... 就是 $TrItem[$iS] = GUICtrlCreateTreeViewItem($RS.Fields(1).value, $Group) 这些
; 示例一共是10个, 也就是 5 to 14 ; $TrItem = 5 , $TrItem = 14
; 响应 $TrItem的点击时, $nMsg = 5, 为了保证下句操作的正确性,(也就是取出数组对应值),所以必须要 $nMsg - 5
$RS.Open("Select * From " & $TableServer & " Where Groups='" & _GUICtrlTreeView_GetText($TreeView, $TrItem[$nMsg - 5]) & "'") ;
#ce
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
Case $TrItem To $TrItem ; 点击任意 treeviewitem 时 ,进行以下操作
_GUICtrlListView_DeleteAllItems($ListView)
ConnectDB()
$RS = ObjCreate("ADODB.Recordset")
$RS.ActiveConnection = $Conn
$RS.Open("Select * From " & $TableServer & " Where Groups='" & _GUICtrlTreeView_GetText($TreeView, $TrItem[$nMsg - 5]) & "'") ;
While Not $RS.eof And Not $RS.bof
If @error = 1 Then ExitLoop
GUICtrlCreateListViewItem($RS.Fields(1).value & "|" & $RS.Fields(7).value & "|" & $RS.Fields(2).value & "|" & $RS.Fields(3).value & "|" & $RS.Fields(9).value, $ListView )
$RS.movenext
WEnd
$RS.close
$Conn.Close
EndSwitch
WEnd
;=============== 函数 - 定义 ===============
Func ConnectDB() ;连接数据库
$DbPath = @ScriptDir & "\" & "Data.mdb"
;~ $mdbpass = "traveler@)!^"
$DbPass = "123"
$TableGroup = "TB_Group"
$TableServer = "TB_Server"
$TableUsers = "TB_Users"
$Conn = ObjCreate("ADODB.Connection")
$Conn.Open("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & $DbPath & "; Jet OLEDB:Database Password=" & $DbPass)
EndFunc ;==>ConnectDB
本帖最后由 imutraveler 于 2016-8-17 23:45 编辑
回复 2# Alam
谢谢兄弟,懂了,
意思就是在这个GUI内,根据第一个TreeViewItem前面控件的数量,相应的调整$nMsg该减去的数!
但是需要手动计算前面有多少个控件,并且在后续修改或添加控件,都需要重新计算这个数
研究了半天,发现只要把$nMsg - 5修改为$nMsg - $TrItem,感觉就完美了 回复 3# imutraveler
顶呱呱~!青出蓝胜于蓝啊!
论坛规矩:
问题解决了,就编辑帖子标题,加上"(已解决)"的字样吧! 回复 4# Alam
兄弟,又遇到了点难题,不知道能否加你QQ,向你请教一下,能方便一些!
如果可以的话,留个QQ吧,,
页:
[1]