找回密码
 加入
搜索
查看: 3350|回复: 6

listview选中赋值给INPUT输入框

[复制链接]
发表于 2009-9-29 12:24:52 | 显示全部楼层 |阅读模式
本帖最后由 siuchingchow 于 2009-10-11 08:48 编辑

<br>

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <ListViewConstants.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <GUIListView.au3>
$database="common.mdb"
$password=""
$con=ObjCreate("adodb.connection")
$sql=$con.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source="&$database&";Jet Oledb:Database Password="&$password)
#Region ### START Koda GUI section ### Form=D:\netgame\acess.kxf
$Form1 = GUICreate("Form1", 276, 392, 192, 124)
$ListView1 = GUICtrlCreateListView("编号|用户名|年龄|QQ|Email", 0, 0, 273, 265)
$Group1 = GUICtrlCreateGroup("", 0, 264, 273, 96)
$username = GUICtrlCreateInput("用户名", 8, 280, 85, 21)
$userage = GUICtrlCreateInput("年龄", 96, 280, 85, 21)
$QQ = GUICtrlCreateInput("QQ", 184, 280, 85, 21)
$Email = GUICtrlCreateInput("Email", 8, 304, 260, 21)
$add_data = GUICtrlCreateButton("添加", 8, 328, 62, 25, $WS_GROUP)
$modi_data = GUICtrlCreateButton("修改", 136, 328, 62, 25, $WS_GROUP)
$del_data = GUICtrlCreateButton("删除", 200, 328, 62, 25, $WS_GROUP)
$read_data = GUICtrlCreateButton("读取", 72, 328, 62, 25, $WS_GROUP)
GUICtrlCreateGroup("", -99, -99, 1, 1)
$Label1 = GUICtrlCreateLabel("[广西很傻很天真制作,QQ:275988582]", 16, 368, 212, 17)
read()
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
$nMsg = GUIGetMsg()
Switch $nMsg
        Case $GUI_EVENT_CLOSE
        Exit
        Case $read_data
                read()
    Case $add_data
                add()
                read()
        Case $del_data
                 del()
                 read()
         Case $modi_data
                 
EndSwitch
WEnd
Func add()
        $username=GUICtrlRead($username)
        $userage=GUICtrlRead($userage)
        $Email=GUICtrlRead($Email)
        ;IF StringRegExp ($QQ,"^[0-9]\d*$",0)=0  Then
                ;MsgBox(4096, "提示:"," QQ号码错误!")
        ;Else
        $QQ=GUICtrlRead($QQ)               
                $con.Execute("insert into t_user(username,userage,qq,email) values('"&$username&"','"&$userage&"','"&$QQ&"','"&$Email&"')")
                MsgBox(64, "提示:"," 成功写入数据库!")
        ;EndIf
EndFunc       
Func read()
        GUICtrlSendMsg($ListView1,$LVM_DELETEALLITEMS, 0, 0);由于listview control在创建时是不可见的,现在我们把它显示出来.还有 要把其中的所有元素(如果有的话)删掉.
        $rs=ObjCreate("adodb.recordset")
        $rs.activeconnection=$con
                $rs.open("select * from T_user")
                        while Not $RS.eof And Not $RS.bof
           if @error =1 Then ExitLoop
                   ;if not $RS.eof Then
            GUICtrlCreateListViewItem ( $RS.Fields (0).value&"|"& $RS.Fields (1).value&"|"& $RS.Fields (2).value&"|"& $RS.Fields (3).value&"|"& $RS.Fields (4).value,$ListView1 )
             $rs.movenext
              WEnd
               $rs.close
EndFunc
Func del()
    $str=GUICtrlRead(GUICtrlRead($ListView1))
         $strsplit=StringSplit($str,"|")
         $con.execute("delete from  T_user  WHERE id="&$strsplit[1])
         MsgBox(64, "温馨提示:","删除成功!!!")
EndFunc

小弟刚学AU3几天,这是修改别人的代码的,请问怎样写代码才能在选中ListView中某行在下面的输入框里显示相应的值,望各位大侠帮忙看下,不胜感激!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?加入

×
发表于 2009-9-29 15:43:41 | 显示全部楼层
方法如下 其他的请自行修改
[au3]#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <ListViewConstants.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <GUIListView.au3>
$database = "common.mdb"
$password = ""
$con = ObjCreate("adodb.connection")
$sql = $con.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & $database & ";Jet Oledb:Database Password=" & $password)
#Region ### START Koda GUI section ### Form=D:\netgame\acess.kxf
$Form1 = GUICreate("Form1", 276, 392, 192, 124)
$ListView1 = GUICtrlCreateListView("编号|用户名|年龄|QQ|Email", 0, 0, 273, 265)
$Group1 = GUICtrlCreateGroup("", 0, 264, 273, 96)
$username = GUICtrlCreateInput("用户名", 8, 280, 85, 21)
$userage = GUICtrlCreateInput("年龄", 96, 280, 85, 21)
$QQ = GUICtrlCreateInput("QQ", 184, 280, 85, 21)
$Email = GUICtrlCreateInput("Email", 8, 304, 260, 21)
$add_data = GUICtrlCreateButton("添加", 8, 328, 62, 25, $WS_GROUP)
$modi_data = GUICtrlCreateButton("修改", 136, 328, 62, 25, $WS_GROUP)
$del_data = GUICtrlCreateButton("删除", 200, 328, 62, 25, $WS_GROUP)
$read_data = GUICtrlCreateButton("读取", 72, 328, 62, 25, $WS_GROUP)
GUICtrlCreateGroup("", -99, -99, 1, 1)
$Label1 = GUICtrlCreateLabel("[广西很傻很天真制作,QQ:275988582]", 16, 368, 212, 17)
GUIRegisterMsg($WM_NOTIFY, "WM_Notify_Events")
read()
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
                Case $GUI_EVENT_CLOSE
                        Exit
                Case $read_data
                        read()
                Case $add_data
                        add()
                        read()
                Case $del_data
                        del()
                        read()
                Case $modi_data
;~                  view()
        EndSwitch
WEnd



Func WM_Notify_Events($hWndGUI, $MsgID, $wParam, $lParam)
        #forceref $hWndGUI, $MsgID, $wParam
        Local $tagNMHDR, $event, $hwndFrom, $code
        $tagNMHDR = DllStructCreate("int;int;int", $lParam) ;NMHDR (hwndFrom, idFrom, code)
        If @error Then Return
        $event = DllStructGetData($tagNMHDR, 3)
        Select
                Case $wParam = $ListView1
                        Select
                                Case $event = $NM_Click
                                        $ret = StringSplit(_GUICtrlListView_GetItemTextString($ListView1, Number(_GUICtrlListView_GetSelectedIndices($ListView1))), "|")
MsgBox(0,0,"用户名:"&$ret[2]&@CRLF&"年龄:"&$ret[3])
                                Case Else
                        EndSelect
        EndSelect
        $tagNMHDR = 0
        $event = 0
        $lParam = 0
EndFunc   ;==>WM_Notify_Events


Func add()
        $username = GUICtrlRead($username)
        $userage = GUICtrlRead($userage)
        $Email = GUICtrlRead($Email)
        ;IF StringRegExp ($QQ,"^[0-9]\d*$",0)=0  Then
        ;MsgBox(4096, "提示:"," QQ号码错误!")
        ;Else
        $QQ = GUICtrlRead($QQ)
        $con.Execute("insert into t_user(username,userage,qq,email) values('" & $username & "','" & $userage & "','" & $QQ & "','" & $Email & "')")
        MsgBox(64, "提示:", " 成功写入数据库!")
        ;EndIf
EndFunc   ;==>add
Func read()
        GUICtrlSendMsg($ListView1, $LVM_DELETEALLITEMS, 0, 0);由于listview control在创建时是不可见的,现在我们把它显示出来.还有 要把其中的所有元素(如果有的话)删掉.
        $rs = ObjCreate("adodb.recordset")
        $rs.activeconnection = $con
        $rs.open("select * from T_user")
        While Not $rs.eof And Not $rs.bof
                If @error = 1 Then ExitLoop
                ;if not $RS.eof Then
                GUICtrlCreateListViewItem($rs.Fields(0).value & "|" & $rs.Fields(1).value & "|" & $rs.Fields(2).value & "|" & $rs.Fields(3).value & "|" & $rs.Fields(4).value, $ListView1)
                $rs.movenext
        WEnd
        $rs.close
EndFunc   ;==>read
Func del()
        $str = GUICtrlRead(GUICtrlRead($ListView1))
        $strsplit = StringSplit($str, "|")
        $con.execute("delete from  T_user  WHERE id=" & $strsplit[1])
        MsgBox(64, "温馨提示:", "删除成功!!!")
EndFunc   ;==>del[/au3]
发表于 2009-9-29 21:30:13 | 显示全部楼层
本帖最后由 jycel 于 2009-9-29 21:31 编辑

论坛上有类似例子!
可以参照下我写的网吧GHOST后全自动设置程序3.0源代码

GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
;上句加入开头
Func WM_NOTIFY($hWndGUI, $MsgID, $WParam, $LParam)
 
        Local $tagNMHDR, $Event, $hWndFrom, $IDFrom
        Local $tagNMHDR = DllStructCreate("int;int;int", $LParam)
        If @error Then Return $GUI_RUNDEFMSG
        $IDFrom = DllStructGetData($tagNMHDR, 2)
        $Event = DllStructGetData($tagNMHDR, 3)
        $tagNMHDR = 0
        Switch $IDFrom;选择产生事件的控件
 
                Case $ListView1
 
                        Switch $Event; 选择产生的事件

                                                                        Case $NM_CLICK ; 左击
                                                            $Index = _GUICtrlListView_GetSelectedIndices($ListView1)
                                        If Not StringLen($Index) Then; 这里用以判断是否选定了ListViewItem
                                                                                        $List_n=_GUICtrlListView_GetItemCount($ListView1)
                                                                                                        If Not $List_n Then
                                                                                                                GUICtrlSetData($Label1,"提示:当前无任何数据请先扫描")
                                                                                                        EndIf
                                               ;GUICtrlSetData($Label1,"提示:请先选中数据再进行相关操作,没有数据请先扫描")
                                                Return
                                                                                        EndIf
                                                                                                GUICtrlSetData($Label1,"提示:请点击右键操作[编缉数据]|[删除按钮]|[清空数据]|[批量编缉]")
                                                            GUICtrlSetData($Input1,_GUICtrlListView_GetItemText($ListView1, Number($Index),1))
                                                                                                GUICtrlSetData($Input2,_GUICtrlListView_GetItemText($ListView1, Number($Index),2))
                                                                                                GUICtrlSetData($Input3,_GUICtrlListView_GetItemText($ListView1, Number($Index),0))
                                                                                                GUICtrlSetData($Combo1,_GUICtrlListView_GetItemText($ListView1, Number($Index),3))
                                                                                                GUICtrlSetData($Input5,_GUICtrlListView_GetItemText($ListView1, Number($Index),4))
                                                                                                GUICtrlSetData($Input6,_GUICtrlListView_GetItemText($ListView1, Number($Index),5))
                                                                                                GUICtrlSetData($Input7,_GUICtrlListView_GetItemText($ListView1, Number($Index),6))
                                                                                                GUICtrlSetData($Input8,_GUICtrlListView_GetItemText($ListView1, Number($Index),7))
                                                                                                GUICtrlSetData($Input9,_GUICtrlListView_GetItemText($ListView1, Number($Index),9))
                                                                                                GUICtrlSetData($Combo2,_GUICtrlListView_GetItemText($ListView1, Number($Index),8))
                                                                                                GUICtrlSetData($Combo3,_GUICtrlListView_GetItemText($ListView1, Number($Index),10))                                                                        
                                 Case $NM_DBLCLK ; 双击
                                                            $Index = _GUICtrlListView_GetSelectedIndices($ListView1)
                                        If Not StringLen($Index) Then; 这里用以判断是否选定了ListViewItem
                                               GUICtrlSetData($Label1,"提示:请先选中数据再进行相关操作,没有数据请先扫描")
                                                Return
                                                                                        EndIf
                                                                                        GUICtrlSetData($Label1,"提示:请点击右键操作[编缉数据]|[删除按钮]|[清空数据]|[批量编缉]")
                                                            GUICtrlSetData($Input1,_GUICtrlListView_GetItemText($ListView1, Number($Index),1))
                                                                                                GUICtrlSetData($Input2,_GUICtrlListView_GetItemText($ListView1, Number($Index),2))
                                                                                                GUICtrlSetData($Input3,_GUICtrlListView_GetItemText($ListView1, Number($Index),0))
                                                                                                GUICtrlSetData($Combo1,_GUICtrlListView_GetItemText($ListView1, Number($Index),3))
                                                                                                GUICtrlSetData($Input5,_GUICtrlListView_GetItemText($ListView1, Number($Index),4))
                                                                                                GUICtrlSetData($Input6,_GUICtrlListView_GetItemText($ListView1, Number($Index),5))
                                                                                                GUICtrlSetData($Input7,_GUICtrlListView_GetItemText($ListView1, Number($Index),6))
                                                                                                GUICtrlSetData($Input8,_GUICtrlListView_GetItemText($ListView1, Number($Index),7))
                                                                                                GUICtrlSetData($Input9,_GUICtrlListView_GetItemText($ListView1, Number($Index),9))
                                                                                                GUICtrlSetData($Combo2,_GUICtrlListView_GetItemText($ListView1, Number($Index),8))
                                                                                                GUICtrlSetData($Combo3,_GUICtrlListView_GetItemText($ListView1, Number($Index),10))                                                                        
                                Case $NM_RCLICK ; 右击
;~                                         ...
                        EndSwitch
 
        EndSwitch
 
        Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_NOTIFY
 楼主| 发表于 2009-9-30 01:09:40 | 显示全部楼层
可以了,感谢jycel 和大绯狼两位朋友,获益良多,不胜感激!
 楼主| 发表于 2009-9-30 02:22:12 | 显示全部楼层
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <ListViewConstants.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <GUIListView.au3>
$database="common.mdb"
$password=""
$con=ObjCreate("adodb.connection")
$sql=$con.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source="&$database&";Jet Oledb:Database Password="&$password)
#Region ### START Koda GUI section ### Form=D:\netgame\acess.kxf
$Form1 = GUICreate("Form1", 276, 392, 192, 124)
$ListView1 = GUICtrlCreateListView("编号|用户名|年龄|QQ|Email", 0, 0, 273, 265)
$Group1 = GUICtrlCreateGroup("", 0, 264, 273, 96)
$username = GUICtrlCreateInput("用户名", 8, 280, 85, 21)
$userage = GUICtrlCreateInput("年龄", 96, 280, 85, 21)
$QQ = GUICtrlCreateInput("QQ", 184, 280, 85, 21)
$Email = GUICtrlCreateInput("Email", 8, 304, 260, 21)
$ID_num = GUICtrlCreateInput("", 256, 328, 121, 21)
GUICtrlSetState(-1, $GUI_HIDE)
$add_data = GUICtrlCreateButton("添加", 8, 328, 62, 25, $WS_GROUP)
$modi_data = GUICtrlCreateButton("修改", 136, 328, 62, 25, $WS_GROUP)
$del_data = GUICtrlCreateButton("删除", 200, 328, 62, 25, $WS_GROUP)
$read_data = GUICtrlCreateButton("读取", 72, 328, 62, 25, $WS_GROUP)
GUICtrlCreateGroup("", -99, -99, 1, 1)
$Label1 = GUICtrlCreateLabel("[广西很傻很天真制作,QQ:275988582]", 16, 368, 212, 17)
GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
read()
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
$nMsg = GUIGetMsg()
Switch $nMsg
        Case $GUI_EVENT_CLOSE
        Exit
        Case $read_data
                read()
    Case $add_data
                add()
                read()
        Case $del_data
                 del()
                 read()
         Case $modi_data
                 modi()
                 read()
EndSwitch
WEnd
Func WM_NOTIFY($hWndGUI, $MsgID, $WParam, $LParam)

        Local $tagNMHDR, $Event, $hWndFrom, $IDFrom
        Local $tagNMHDR = DllStructCreate("int;int;int", $LParam)
        If @error Then Return $GUI_RUNDEFMSG
        $IDFrom = DllStructGetData($tagNMHDR, 2)
        $Event = DllStructGetData($tagNMHDR, 3)
        $tagNMHDR = 0
        Switch $IDFrom;选择产生事件的控件
                        Case $ListView1
                Switch $Event; 选择产生的事件
                       Case $NM_CLICK ; 左击
                            $Index = _GUICtrlListView_GetSelectedIndices($ListView1)
                                                        If Not StringLen($Index) Then; 这里用以判断是否选定了ListViewItem
                                $List_n=_GUICtrlListView_GetItemCount($ListView1)
                                If Not $List_n Then
                                    GUICtrlSetData($Label1,"提示:当前无任何数据请先扫描")
                                 EndIf
                                                                ;GUICtrlSetData($Label1,"提示:请先选中数据再进行相关操作,没有数据请先扫描")
                                 Return
                                                         EndIf
                                                        GUICtrlSetData($ID_num,_GUICtrlListView_GetItemText($ListView1, Number($Index),0))
                                                        GUICtrlSetData($username,_GUICtrlListView_GetItemText($ListView1, Number($Index),1))
                            GUICtrlSetData($userage,_GUICtrlListView_GetItemText($ListView1, Number($Index),2))
                            GUICtrlSetData($QQ,_GUICtrlListView_GetItemText($ListView1, Number($Index),3))
                                                        GUICtrlSetData($Email,_GUICtrlListView_GetItemText($ListView1, Number($Index),4))

                        EndSwitch

        EndSwitch

        Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_NOTIFY
Func add()
        $username=GUICtrlRead($username)
        $userage=GUICtrlRead($userage)
        $Email=GUICtrlRead($Email)
        ;IF StringRegExp ($QQ,"^[0-9]\d*$",0)=0  Then
                ;MsgBox(4096, "提示:"," QQ号码错误!")
        ;Else
        $QQ=GUICtrlRead($QQ)               
                $con.Execute("insert into t_user(username,userage,qq,email) values('"&$username&"','"&$userage&"','"&$QQ&"','"&$Email&"')")
                MsgBox(64, "提示:"," 成功写入数据库!")
        ;EndIf
EndFunc       
Func read()
        GUICtrlSendMsg($ListView1,$LVM_DELETEALLITEMS, 0, 0);由于listview control在创建时是不可见的,现在我们把它显示出来.还有 要把其中的所有元素(如果有的话)删掉.
        $rs=ObjCreate("adodb.recordset")
        $rs.activeconnection=$con
                $rs.open("select * from T_user")
                        while Not $RS.eof And Not $RS.bof
           if @error =1 Then ExitLoop
                   ;if not $RS.eof Then
            GUICtrlCreateListViewItem ( $RS.Fields (0).value&"|"& $RS.Fields (1).value&"|"& $RS.Fields (2).value&"|"& $RS.Fields (3).value&"|"& $RS.Fields (4).value,$ListView1 )
             $rs.movenext
                 WEnd
               ;$rs.close
EndFunc
Func del()
    $str=GUICtrlRead(GUICtrlRead($ListView1))
         $strsplit=StringSplit($str,"|")
         $con.execute("delete from  T_user  WHERE id="&$strsplit[1])
         MsgBox(64, "温馨提示:","删除成功!!!")
EndFunc
Func modi()
        $ID_num=GUICtrlRead($ID_num)
        $username=GUICtrlRead($username)
        $userage=GUICtrlRead($userage)
        $QQ=GUICtrlRead($QQ)
        $Email=GUICtrlRead($Email)
        $sqlstr="update t_user set username='"&$username&"',userage='"&$userage&"',QQ='"&$QQ&"',email='"&$Email&"' where id="&$ID_num
                $con.Execute($sqlstr)
                MsgBox(64, "提示:"," 成功修改数据库!")
        ;EndIf
EndFunc       
;我重新写了下,好象运行添加数据后再点击listview时,再也不能赋值给INPUT输入框,怎么回事呢?
 楼主| 发表于 2009-9-30 02:25:54 | 显示全部楼层
修改后第二点击也不能赋值,怎么回事呢?
发表于 2014-3-26 21:10:58 | 显示全部楼层
这个实用,下载下来看看。
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-9-28 11:17 , Processed in 0.073454 second(s), 21 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表