找回密码
 加入
搜索
查看: 4699|回复: 7

[系统综合] 请问listview如何自动编号?

  [复制链接]
发表于 2011-1-13 16:34:39 | 显示全部楼层 |阅读模式
比如说,我用循环的方法给listview添加了从1到100的序号,序号从上到下显示的是1 2 3 4 5.....
但是当我删除掉序号为2的item的时候,序号从上到下显示的就变成1 3 4 5.......了
有没有哪种办法可以自动填写编号,然后在用户删除或者添加新item的时候自动调整编号呢?
发表于 2011-1-13 17:09:16 | 显示全部楼层
偶也想知道答案,帮你顶一下,希望哪位高手不吝指教。
发表于 2011-1-13 17:17:46 | 显示全部楼层
试试:
#AutoIt3Wrapper_au3check_parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6
#include <GuiConstantsEx.au3>
#include <GuiListView.au3>

Opt('MustDeclareVars', 1)

$Debug_LV = False ; Check ClassName being passed to ListView functions, set to True and use a handle to another control to see it work

_Main()

Func _Main()
        Local $iI, $hListView

        ; Create GUI
        GUICreate("ListView Add Array", 400, 300)
        $hListView = GUICtrlCreateListView("", 2, 2, 394, 268)
        _GUICtrlListView_SetUnicodeFormat($hListView, False)
        GUISetState()

        ; Add columns
        _GUICtrlListView_AddColumn($hListView, "Items", 100)
        _GUICtrlListView_AddColumn($hListView, "SubItems 1", 100)
        _GUICtrlListView_AddColumn($hListView, "SubItems 2", 100)
        _GUICtrlListView_AddColumn($hListView, "SubItems 3", 100)

        _GUICtrlListView_SetItemCount($hListView, 5000)
        
        ; One column load
        Local $aItems[10][2]
        For $iI = 0 To UBound($aItems) - 1
                $aItems[$iI][0] = $iI+1
        Next
        _GUICtrlListView_AddArray($hListView, $aItems)        
        Local $Random=Random(0,9,1)
        MsgBox(4096,'随机删除一行',$Random+1)
        _GUICtrlListView_DeleteItem(GUICtrlGetHandle($hListView),$Random)
        _GUICtrlListView_BeginUpdate(GUICtrlGetHandle($hListView))
        For $i=$Random To _GUICtrlListView_GetItemCount(GUICtrlGetHandle($hListView))-1
                _GUICtrlListView_SetItemText($hListView,$i,$i+1)
                ;MsgBox(0,0,$i)
        Next
        _GUICtrlListView_EndUpdate(GUICtrlGetHandle($hListView))
        
        Do
        Until GUIGetMsg() = $GUI_EVENT_CLOSE
        GUIDelete()
EndFunc   ;==>_Main
 楼主| 发表于 2011-1-13 18:00:22 | 显示全部楼层
很多次一提出问题3mile总是热情帮助,在此感谢
这个可以用,不过我还有一个问题,就是如果我设置listview可以多选,这样用户可以同时删掉很多行。问题是这时候怎么判断用户选中了多少行?貌似只有知道了用户选中了多少行,才能更新listview中的序号。
 楼主| 发表于 2011-1-13 21:02:10 | 显示全部楼层
问题补充一个,设置了可以多选以后,可以按住ctrl键随即选择行,这样子删掉的行的序号就变得不连贯和不可预料了。不知道listview的udf有没有提供自动调整序号的功能,而不用像3mile那样用循环的方法给剩下的行重新编号。
发表于 2011-1-23 17:07:39 | 显示全部楼层
没有提供自动调整序号功能,只能每次重新编号
发表于 2011-1-23 21:09:34 | 显示全部楼层
回复 5# papapa314
问题还是摆在这儿吗?
就我所知道的应该只能通过循环来排序,也许应该还有另外的自动排序的函数吧,不过我没有找到。想了想,也许就算是有排序的函数应该也是通过循环来做的吧。
提供一个笨办法来解决楼主的困扰。方法很笨,慎用:
;#AutoIt3Wrapper_au3check_parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6
#include <GuiConstantsEx.au3>
#include <GuiListView.au3>
 
;Opt('MustDeclareVars', 1)
 
;$Debug_LV = False ; Check ClassName being passed to ListView functions, set to True and use a handle to another control to see it work
 
_Main()
 
Func _Main()
        Local $iI, $hListView
 
        ; Create GUI
        GUICreate("ListView Add Array", 400, 300)
        $hListView = GUICtrlCreateListView("", 2, 2, 394, 268)
        _GUICtrlListView_SetUnicodeFormat($hListView, False)
        GUISetState()
 
        ; Add columns
        _GUICtrlListView_AddColumn($hListView, "Items", 100)
        _GUICtrlListView_AddColumn($hListView, "SubItems 1", 100)
        _GUICtrlListView_AddColumn($hListView, "SubItems 2", 100)
        _GUICtrlListView_AddColumn($hListView, "SubItems 3", 100)
 
        _GUICtrlListView_SetItemCount($hListView, 5000)
        
        ; One column load
        Local $aItems[10][2]
        For $iI = 0 To UBound($aItems) - 1
                $aItems[$iI][0] ="items"& $iI+1
        Next
        _GUICtrlListView_AddArray($hListView, $aItems)  
        ;Local $Random=Random(0,9,1)
                MsgBox(4096,'随机删除三行','随机删除三行')
                _GUICtrlListView_DeleteItem(GUICtrlGetHandle($hListView),Random(0,_GUICtrlListView_GetitemCount($hListView),1))
                _GUICtrlListView_DeleteItem(GUICtrlGetHandle($hListView),Random(0,_GUICtrlListView_GetitemCount($hListView),1))
                _GUICtrlListView_DeleteItem(GUICtrlGetHandle($hListView),Random(0,_GUICtrlListView_GetitemCount($hListView),1))
                msgbox(0,0,"重新排序")
                local $count=_GUICtrlListView_GetitemCount($hListView)
                for $i=0 to $count
                        if _GUICtrlListView_GetItemText($hListView,$i,0)<>$i+1 Then
                                _GUICtrlListView_SetItemText($hListView,$i,"Items"&$i+1)
                        EndIf
                Next

        Do
        Until GUIGetMsg() = $GUI_EVENT_CLOSE
        GUIDelete()
EndFunc   ;==>_Main
发表于 2011-1-24 11:34:49 | 显示全部楼层
谢谢分享!!学习了!谢谢!!
您需要登录后才可以回帖 登录 | 加入

本版积分规则

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

GMT+8, 2024-9-21 14:44 , Processed in 0.083863 second(s), 22 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

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