papapa314 发表于 2011-1-13 16:34:39

请问listview如何自动编号?

比如说,我用循环的方法给listview添加了从1到100的序号,序号从上到下显示的是1 2 3 4 5.....
但是当我删除掉序号为2的item的时候,序号从上到下显示的就变成1 3 4 5.......了
有没有哪种办法可以自动填写编号,然后在用户删除或者添加新item的时候自动调整编号呢?

kingfirekkk 发表于 2011-1-13 17:09:16

偶也想知道答案,帮你顶一下,希望哪位高手不吝指教。

3mile 发表于 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
        For $iI = 0 To UBound($aItems) - 1
                $aItems[$iI] = $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

papapa314 发表于 2011-1-13 18:00:22

很多次一提出问题3mile总是热情帮助,在此感谢
这个可以用,不过我还有一个问题,就是如果我设置listview可以多选,这样用户可以同时删掉很多行。问题是这时候怎么判断用户选中了多少行?貌似只有知道了用户选中了多少行,才能更新listview中的序号。

papapa314 发表于 2011-1-13 21:02:10

问题补充一个,设置了可以多选以后,可以按住ctrl键随即选择行,这样子删掉的行的序号就变得不连贯和不可预料了。不知道listview的udf有没有提供自动调整序号的功能,而不用像3mile那样用循环的方法给剩下的行重新编号。

weiyun 发表于 2011-1-23 17:07:39

没有提供自动调整序号功能,只能每次重新编号

3mile 发表于 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
      For $iI = 0 To UBound($aItems) - 1
                $aItems[$iI] ="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

masterpcc 发表于 2011-1-24 11:34:49

谢谢分享!!学习了!谢谢!!
页: [1]
查看完整版本: 请问listview如何自动编号?