duyan 发表于 2011-4-4 23:31:48

从INI文件中按要求归类

刚学AU3没几天,想请教一个基础的问题:
这是文件main.ini的一部分,

name1=刘德华
age1=44
sex1=男
address1=香港
name2=宋慧乔
age2=34
sex2=女
address2=韩国
name3=范冰冰
age3=32
sex3=女
address3=中国
name4=成龙
age4=58
sex4=男
address4=香港
name5=权相宇
age5=38
sex5=男
address5=韩国
我现在想点击button1时,在listview中显示韩国人,点击button2时,在listview中显示香港人,
请教高手,代码应如何编写!谢谢!

kxing 发表于 2011-4-5 07:29:02

你这个应该将美各国家列一个字段。
而且每个人的信息最好在一个键值中,用分隔符隔开。
[香港]
1=刘德华,44,男
2=成龙,58,男
……

lixiaolong 发表于 2011-4-5 09:29:11

本帖最后由 lixiaolong 于 2011-4-5 10:20 编辑

回复 1# duyan

看看这个可以不?$var = IniReadSection(@DesktopDir & '\main.ini', "main")
$address = '韩国'
For $i = 1 To $var
        If $var[$i] = $address Then
                $i += 1
                MsgBox(4096, "", $var[$i] & @CRLF & $var[$i + 1] & "岁" & @CRLF & $var[$i + 2])
                $i += 1
        EndIf
Next
main.ini
address0=韩国
name1=刘德华
age1=44
sex1=男
address1=香港
name2=宋慧乔
age2=34
sex2=女
address2=韩国
name3=范冰冰
age3=32
sex3=女
address3=中国
name4=成龙
age4=58
sex4=男
address4=香港
name5=权相宇
age5=38
sex5=男当练习吧#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Global $var = IniReadSection(@DesktopDir & '\main.ini', "main")
Global $address = '韩国'

GUICreate($address & '人', 220, 250, 100, 200, -1, $WS_EX_ACCEPTFILES)
GUISetBkColor(0x00E0FFFF) ; 设置背景色

$listview = GUICtrlCreateListView("姓名|年龄|性别", 10, 10, 200, 150);,$LVS_SORTDESCENDING)
$button = GUICtrlCreateButton("查看选中项目的数据", 40, 170, 120, 20)
Global $item1 = GUICtrlCreateListViewItem("", $listview)
Global $item2 = GUICtrlCreateListViewItem("", $listview)
Global $item3 = GUICtrlCreateListViewItem("", $listview)
GUISetState()
address()

Do
        $msg = GUIGetMsg()

        Select
                Case $msg = $button
                        MsgBox(0, "listview 项目", GUICtrlRead(GUICtrlRead($listview)), 2)
                Case $msg = $listview
                        MsgBox(0, "listview", "被点击列的索引值 = " & GUICtrlGetState($listview), 2)
        EndSelect
Until $msg = $GUI_EVENT_CLOSE

Func address()
        Local $s = 1
        For $i = 1 To $var
                If $var[$i] = $address Then
                        $i += 1
                        GUICtrlSetData(Eval('item' & $s), $var[$i] & '|' & $var[$i + 1] & '|' & $var[$i + 2])
                        $s += 1
                        $i += 4;这个可以去掉.
                EndIf
        Next
EndFunc   ;==>address如果想显示中国人只把 Global $address = '韩国' 改成 Global $address = '中国' 就可以了.
页: [1]
查看完整版本: 从INI文件中按要求归类