找回密码
 加入
搜索
查看: 7816|回复: 15

[AU3基础] [已解决]如何保存listview里面的内容,使之在下次程序启动时显示在listview上?

 火.. [复制链接]
发表于 2012-1-18 01:18:56 | 显示全部楼层 |阅读模式
本帖最后由 qq271859852 于 2012-1-31 16:58 编辑


为方便描述问题,做了个简单界面如图示
我通过input输入信息,点击"保存"按扭,input里的内容就显示到listview控件上。
问题:如何把这些数据保存在程序里?
也就是说,我要达到的效果是:下次程序启动时,还会把以前保存的且显示在listview控件上的信息显示出来?
请问一下各位大大们,我该如何达到这种效果?

代码如下:
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <ListViewConstants.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 245, 438, 192, 124)
$ListView1 = GUICtrlCreateListView("姓名|编号", 24, 16, 209, 209)
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 0, 100)
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 1, 100)
$ListView1_0 = GUICtrlCreateListViewItem("张三|201203", $ListView1)
$ListView1_1 = GUICtrlCreateListViewItem("李四|201204", $ListView1)
$ListView1_2 = GUICtrlCreateListViewItem("王五|201205", $ListView1)
$Input1 = GUICtrlCreateInput("", 51, 232, 177, 21)
$Input2 = GUICtrlCreateInput("", 51, 258, 177, 21)
$Label1 = GUICtrlCreateLabel("姓名", 16, 232, 28, 17)
$Label2 = GUICtrlCreateLabel("编号", 15, 261, 28, 17)
$Button1 = GUICtrlCreateButton("保存", 72, 288, 105, 33)
GUISetState(@SW_SHOW)
#endregion ### END Koda GUI section ###

Local $p = 0
Local $Date[1][2]
While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
                Case $GUI_EVENT_CLOSE
                        Exit
                Case $Button1
                        ReDim $Date[$p + 1][2]
                        $Date[$p][0] = GUICtrlRead($Input1)
                        $Date[$p][1] = GUICtrlRead($Input2)
                        GUICtrlCreateListViewItem($Date[$p][0] & "|" & $Date[$p][1], $ListView1)
                        GUICtrlSetData($Input1, "")
                        GUICtrlSetData($Input2, "")
                        $p += 1
        EndSwitch
WEnd

本帖子中包含更多资源

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

×
发表于 2012-1-18 01:29:23 | 显示全部楼层
简单点用ini保存一下!
 楼主| 发表于 2012-1-18 01:35:31 | 显示全部楼层
回复 2# gzh888666


    我尝试过,可是这样效果,至少我做出来的效果不太好。在第二次启动程序时,点保存,就会混乱了
或者你补充一下代码可以吗?
发表于 2012-1-18 08:04:44 | 显示全部楼层
怎么个乱哇?无图无代码无真相!
简单点的用AU3+INI,AU3+ACCESS
发表于 2012-1-18 08:58:41 | 显示全部楼层
坛子有例子。。搜索就有了
#include <GuiListView.au3>
#include <GuiImageList.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <ButtonConstants.au3>
#include <WindowsConstants.au3>
#include <ListViewConstants.au3>

$dir = @ScriptDir&"\config.ini"

$Form1 = GUICreate("列表框操作", 280, 250)
$ListView1 = GUICtrlCreateListView("账号           |密码           |", 10, 10, 260, 150)
$Button1 = GUICtrlCreateButton("添加", 185, 178, 75, 65, $WS_GROUP)
$Input1 = GUICtrlCreateInput("", 60, 180, 120, 21)
$Input2 = GUICtrlCreateInput("", 60, 220, 120, 21)
$Label1 = GUICtrlCreateLabel("账号:", 20, 183, 36, 17)
$Label2 = GUICtrlCreateLabel("密码:", 20, 223, 36, 17)
GUISetState(@SW_SHOW)
Read()

While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
                Case $GUI_EVENT_CLOSE
                        Exit
                Case $Button1
                        tian()
        EndSwitch
WEnd

Func Read()
        $z = 0
        _GUICtrlListView_DeleteAllItems($ListView1)
        $read = IniReadSection($dir,"config")
        If Not @error Then
                For $i = 1 To $read[0][0]
                        GUICtrlCreateListViewItem($read[$i][0], $ListView1)
                        _GUICtrlListView_AddSubItem($ListView1, $z, $read[$i][1], 1, $z+1)
                        $z += 1                
                Next
        EndIf
EndFunc

Func tian()
        $a = GUICtrlRead($Input1)
        $b = GUICtrlRead($Input2)
        
        If $a <> "" And $b <> "" Then 
                IniWrite($dir,"config",$a,$b)
                MsgBox(0,"提示"," 账号: "&$a&" 密码: "&$b&" 保存成功 ")
                Read()
                GUICtrlSetData($Input1,"")
                GUICtrlSetData($Input2,"")
        EndIf        
EndFunc        
 楼主| 发表于 2012-1-18 09:53:48 | 显示全部楼层
回复 4# hzxymkb


    噢,混乱的现象是:下次启动的时候,添加新内容,点击保存,会把内容添加在第一排、第二排……,即覆盖了原来的数据。
AU3+ini稍微会点,楼下热心朋友给予了AU3+INI的帮助,
能帮忙写个AU3+ACCESS的例子吗?
此外。还有其他没有方法呢?譬如说,直接储存在程序里,下次打开就调用?
 楼主| 发表于 2012-1-18 09:54:29 | 显示全部楼层
回复 5# xyhqqaa


    谢谢,昨晚找了两个小时,都没搜到此例子,请问朋友是使用什么关键字呢?
发表于 2012-1-18 10:01:23 | 显示全部楼层
学习了,知道怎么操作了
 楼主| 发表于 2012-1-18 10:02:14 | 显示全部楼层
回复 5# xyhqqaa


    请问一下,我把代码中的
GUICtrlCreateListViewItem($read[$i][0], $ListView1)
                        _GUICtrlListView_AddSubItem($ListView1, $z, $read[$i][1], 1, $z + 1)
改成下面这个也可以实现
GUICtrlCreateListViewItem($read[$i][0]&"|"&$read[$i][1], $ListView1)
请问一下这二者有何区别?什么时候用到代码1比较好呢?
发表于 2012-1-22 12:26:58 | 显示全部楼层
这个例子很方便,谢谢。
发表于 2012-1-22 16:27:32 | 显示全部楼层
本帖最后由 user3000 于 2012-1-22 16:34 编辑
回复  xyhqqaa


    请问一下,我把代码中的改成下面这个也可以实现请问一下这二者有何区别?什么时候 ...
qq271859852 发表于 2012-1-18 10:02

以下代码根据你原有的加工多一点东西, 希望能给你一点启发!
也希望你学习AU3不要太于浮躁, 要打好基础, 不要连 INI 操作都不熟就想着去学习操作数据库之类更复杂的东西了!
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <ListViewConstants.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#region ### START Koda GUI section ### Form=
Local $inifile = @ScriptDir & '\Mydat.ini'
$Form1 = GUICreate("Form1", 245, 438)
$ListView1 = GUICtrlCreateListView("姓名|编号", 24, 16, 209, 209)
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 0, 100)
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 1, 100)
Local $ReadTxt = IniReadSection($inifile, 'Config')
If @error Then
        $ListView1_0 = GUICtrlCreateListViewItem("张三|201203", $ListView1)
        $ListView1_1 = GUICtrlCreateListViewItem("李四|201204", $ListView1)
        $ListView1_2 = GUICtrlCreateListViewItem("王五|201205", $ListView1)
Else
        For $i = 1 To $ReadTxt[0][0]
                GUICtrlCreateListViewItem($ReadTxt[$i][0] & '|' & $ReadTxt[$i][1], $ListView1)
        Next
EndIf
$Input1 = GUICtrlCreateInput("", 51, 232, 177, 21)
$Input2 = GUICtrlCreateInput("", 51, 258, 177, 21)
$Label1 = GUICtrlCreateLabel("姓名", 16, 232, 28, 17)
$Label2 = GUICtrlCreateLabel("编号", 15, 261, 28, 17)
$Button1 = GUICtrlCreateButton("保存", 72, 288, 105, 33)
GUISetState(@SW_SHOW)
#endregion ### END Koda GUI section ###

While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
                Case $GUI_EVENT_CLOSE
                        Exit
                Case $Button1
                        $data = GUICtrlRead(GUICtrlRead($ListView1))
                        If $data = '' Then
                                MsgBox(48, '信息提示', '请先上面任意一条记录! ')
                        Else
                                $name = StringRegExpReplace($data, '([^\|]+?)\|.*', '\1')
                                $Num = StringRegExpReplace($data, '[^\|]+\|([^\|]+)\|', '\1')
                               ;   以上获取名字及编号也可用 StringSplit 来完成!
                                GUICtrlSetData($Input1, $name)
                                GUICtrlSetData($Input2, $Num)
                                IniWrite($inifile, 'Config', $name, $Num)
                        EndIf
        EndSwitch
WEnd
 楼主| 发表于 2012-1-22 19:46:18 | 显示全部楼层
回复 11# user3000


    谢谢朋友热心指出我的缺点,我会注意改正过来的。
发表于 2012-1-23 13:09:20 | 显示全部楼层
路过................................
发表于 2012-1-23 13:56:17 | 显示全部楼层
虚心学习!!~
发表于 2012-1-25 16:41:26 | 显示全部楼层
学习下!!!
您需要登录后才可以回帖 登录 | 加入

本版积分规则

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

GMT+8, 2024-9-30 23:32 , Processed in 0.118347 second(s), 24 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

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