antonioj84

deleting listview item

15 posts in this topic

#1 ·  Posted

need some help, i have a listview. i want display the last item only and delete the previous item(s). for instance if i have 4 items i will delete item 1, 2,3  only the 4 item will be visible on the listview, the code below seems to work partly  thanks ahead

$aListViews = GUICtrlCreateListView("| UCS Subnet/ 28||", 76, 8, 361, 473, -1, BitOR($WS_EX_CLIENTEDGE,$LVS_EX_GRIDLINES,$LVS_EX_FULLROWSELECT)) ;56



            if $Selection1 = True Then
                  $noip = $sValueread
              ; load row in listview


                For $j = 1 To  $noip
                    GUICtrlCreateListViewItem('Static|SREI' & StringFormat('%05d', $checknum) & 'RE0' & $j & '|' & $ip2[UBound($ip2) - 1] + 15 + $j, $aListViews)
                    ;GUICtrlCreateListViewItem('Static|Register ' & $j & '|' & $ip2[UBound($ip2) - 1] + 15 + $j, $alistviews)
                    ConsoleWrite($j &"$noip"&@CRLF)
Next
ConsoleWrite(@CRLF)

For $j=1 To  $noip-1
                 _GUICtrlListView_DeleteItem($aListviews,  $j)
                  ; _GUICtrlListView_DeleteItemsSelected($aListviews)

Next

the attach example  the max item was 2, it should have deleted item 1, only item 2 should have been visible on the listview

listviweCapture.JPG

Share this post


Link to post
Share on other sites



#2 ·  Posted

If you will show me your complete script I will test it, coz im getting lots of syntax error and Im quite lazy to re-construct it :D


ill get to that... i still need to learn and understand a lot of codes graduated.gif

Correct answer, learn to walk before you take on that marathon.

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

The code is huge and connected to a database, I just want the snippet here;s an example the help file. as you can see I try to modify it, however  it does not give me the result I was expecting.

#include <GUIConstantsEx.au3>
#include <GuiListView.au3>
#include <MsgBoxConstants.au3>

Example()

Func Example()
    GUICreate("ListView Item Deletion", 400, 500)
    Local $idListview = GUICtrlCreateListView("Col 1               |Col 2      |Col 3      ", 10, 10, 380, 480, $LVS_SHOWSELALWAYS)
    GUISetState(@SW_SHOW)

    For $i = 0 To 9
        GUICtrlCreateListViewItem("Native Item " & $i & "|Item " & "-1|Item "  & "-2", $idListview)
    Next
    ;For $i = 10 To 20
     ;   _GUICtrlListView_AddItem($idListview, "UDF Item " & $i, -1, 1000 + $i)
      ;  _GUICtrlListView_AddSubItem($idListview, $i, "Item " & $i & "-1", 1)
       ; _GUICtrlListView_AddSubItem($idListview, $i, "Item " & $i & "-2", 2)
    ;Next

    ; Pass the controlID of a native-created ListView to delete both native- and UDF-created Items
    ;MsgBox($MB_SYSTEMMODAL, "Delete item", "Deleting UDF-created Item 12")
    ;_GUICtrlListView_DeleteItem($idListview, 12)

    MsgBox($MB_SYSTEMMODAL, "Single", "Deleting native-created Item 9")

    For $i = 0 To 8

    _GUICtrlListView_DeleteItem($idListview, $i)
     Next


    ; Loop until the user exits
    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                ExitLoop

        EndSwitch
    WEnd

    ; Delete the previous GUI and all controls.
    GUIDelete()
EndFunc   ;==>Example

 

Edited by antonioj84
error

Share this post


Link to post
Share on other sites

#4 ·  Posted

To delete a range of items you need to do so in reverse order or else when you delete item one, item two becomes the new item one and so on.
 

For $i = 8 To 0 Step -1
    _GUICtrlListView_DeleteItem($idListview, $i)
Next

 

1 person likes this

Share this post


Link to post
Share on other sites

#5 ·  Posted

5 minutes ago, Tekk said:

To delete a range of items you need to do so in reverse order or else when you delete item one, item two becomes the new item one and so on.
 

For $i = 8 To 0 Step -1
    _GUICtrlListView_DeleteItem($idListview, $i)
Next

 

it works,it delete now, just need to figure out   how to not have the last item deleted 

Share this post


Link to post
Share on other sites

#6 ·  Posted

question is  how would you skip the last item ?

Share this post


Link to post
Share on other sites

#7 ·  Posted

For $i = _GUICtrlListView_GetItemCount($idListview) - 2 To 0 Step - 1
    _GUICtrlListView_DeleteItem($idListview, $i)
Next

or 

Local $nItems = _GUICtrlListView_GetItemCount($idListview)

_DeleteRange($idListview, 0, $nItems - 1)

Func _DeleteRange($idListview, $nStart, $nCount)
    _GUICtrlListView_BeginUpdate($idListview)

    While $nCount > 0
        _GUICtrlListView_DeleteItem($idListview, $nStart)
        $nCount -= 1
    WEnd

    _GUICtrlListView_EndUpdate($idListview)
EndFunc

 

1 person likes this

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

I dont really know your condition to delete LV items so here's what I've got.

#include <GUIConstantsEx.au3>
#include <GuiListView.au3>
#include <MsgBoxConstants.au3>

Example()

Func Example()
    $GUI = GUICreate("ListView Item Deletion", 400, 500)
    $INPUT = GUICtrlCreateInput("", 10, 10, 100, 20)
    $button = GUICtrlCreateButton("Delete", 115, 10, 50, 20)
    $button1 = GUICtrlCreateButton("Load", 175, 10, 50, 20)
    Local $idListview = GUICtrlCreateListView("Col 1               |Col 2      |Col 3      ", 10, 75, 380, 480, $LVS_SHOWSELALWAYS)
    GUISetState(@SW_SHOW)

    For $i = 0 To 9
        $lv = GUICtrlCreateListViewItem("Native Item " & $i & "|Item " & "-1|Item " & "-2", $idListview)
    Next
    ;For $i = 10 To 20
    ;   _GUICtrlListView_AddItem($idListview, "UDF Item " & $i, -1, 1000 + $i)
    ;  _GUICtrlListView_AddSubItem($idListview, $i, "Item " & $i & "-1", 1)
    ; _GUICtrlListView_AddSubItem($idListview, $i, "Item " & $i & "-2", 2)
    ;Next

    ; Pass the controlID of a native-created ListView to delete both native- and UDF-created Items
    ;MsgBox($MB_SYSTEMMODAL, "Delete item", "Deleting UDF-created Item 12")
    ;_GUICtrlListView_DeleteItem($idListview, 12)

    ; Loop until the user exits
    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                ExitLoop
            Case $button
                $read = GUICtrlRead($INPUT)
                _GUICtrlListView_DeleteItem($idListview, $read)
            Case $button1
                _GUICtrlListView_DeleteAllItems($idListview)
                For $i = 0 To 9
                    $lv = GUICtrlCreateListViewItem("Native Item " & $i & "|Item " & "-1|Item " & "-2", $idListview)
                Next
        EndSwitch
    WEnd


    ; Delete the previous GUI and all controls.
    GUIDelete()
EndFunc   ;==>Example

 

Edited by 232showtime
1 person likes this

ill get to that... i still need to learn and understand a lot of codes graduated.gif

Correct answer, learn to walk before you take on that marathon.

Share this post


Link to post
Share on other sites

#9 ·  Posted

Your goal is to remove "Native Item 0" to  "Native Item 8" leaving only "Native Item 9" in the list correct?

Share this post


Link to post
Share on other sites

#10 ·  Posted

39 minutes ago, Tekk said:
For $i = _GUICtrlListView_GetItemCount($idListview) - 2 To 0 Step - 1
    _GUICtrlListView_DeleteItem($idListview, $i)
Next

or 

Local $nItems = _GUICtrlListView_GetItemCount($idListview)

_DeleteRange($idListview, 0, $nItems - 1)

Func _DeleteRange($idListview, $nStart, $nCount)
    _GUICtrlListView_BeginUpdate($idListview)

    While $nCount > 0
        _GUICtrlListView_DeleteItem($idListview, $nStart)
        $nCount -= 1
    WEnd

    _GUICtrlListView_EndUpdate($idListview)
EndFunc

 

works both like a charm, kudo much appreciated

Share this post


Link to post
Share on other sites

#11 ·  Posted

14 minutes ago, 232showtime said:

I dont really know your condition to delete LV items so here's what I've got.

#include <GUIConstantsEx.au3>
#include <GuiListView.au3>
#include <MsgBoxConstants.au3>

Example()

Func Example()
    $GUI = GUICreate("ListView Item Deletion", 400, 500)
    $INPUT = GUICtrlCreateInput("", 10, 10, 100, 20)
    $button = GUICtrlCreateButton("Delete", 115, 10, 50, 20)
    $button1 = GUICtrlCreateButton("Load", 175, 10, 50, 20)
    Local $idListview = GUICtrlCreateListView("Col 1               |Col 2      |Col 3      ", 10, 75, 380, 480, $LVS_SHOWSELALWAYS)
    GUISetState(@SW_SHOW)

    For $i = 0 To 9
        $lv = GUICtrlCreateListViewItem("Native Item " & $i & "|Item " & "-1|Item " & "-2", $idListview)
    Next
    ;For $i = 10 To 20
    ;   _GUICtrlListView_AddItem($idListview, "UDF Item " & $i, -1, 1000 + $i)
    ;  _GUICtrlListView_AddSubItem($idListview, $i, "Item " & $i & "-1", 1)
    ; _GUICtrlListView_AddSubItem($idListview, $i, "Item " & $i & "-2", 2)
    ;Next

    ; Pass the controlID of a native-created ListView to delete both native- and UDF-created Items
    ;MsgBox($MB_SYSTEMMODAL, "Delete item", "Deleting UDF-created Item 12")
    ;_GUICtrlListView_DeleteItem($idListview, 12)

    ; Loop until the user exits
    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                ExitLoop
            Case $button
                $read = GUICtrlRead($INPUT)
                _GUICtrlListView_DeleteItem($idListview, $read)
            Case $button1
                _GUICtrlListView_DeleteAllItems($idListview)
                For $i = 0 To 9
                    $lv = GUICtrlCreateListViewItem("Native Item " & $i & "|Item " & "-1|Item " & "-2", $idListview)
                Next
        EndSwitch
    WEnd


    ; Delete the previous GUI and all controls.
    GUIDelete()
EndFunc   ;==>Example

 

ThANK you guys you are amazing

Share this post


Link to post
Share on other sites

#12 ·  Posted (edited)

or just read $i

 

#include <GUIConstantsEx.au3>
#include <GuiListView.au3>
#include <MsgBoxConstants.au3>

Example()

Func Example()
    GUICreate("ListView Item Deletion", 400, 500)
    Local $idListview = GUICtrlCreateListView("Col 1               |Col 2      |Col 3      ", 10, 10, 380, 480, $LVS_SHOWSELALWAYS)
    GUISetState(@SW_SHOW)

    For $i = 0 To 9
        GUICtrlCreateListViewItem("Native Item " & $i & "|Item " & "-1|Item " & "-2", $idListview)
    Next
    ;For $i = 10 To 20
    ;   _GUICtrlListView_AddItem($idListview, "UDF Item " & $i, -1, 1000 + $i)
    ;  _GUICtrlListView_AddSubItem($idListview, $i, "Item " & $i & "-1", 1)
    ; _GUICtrlListView_AddSubItem($idListview, $i, "Item " & $i & "-2", 2)
    ;Next

    ; Pass the controlID of a native-created ListView to delete both native- and UDF-created Items
    ;MsgBox($MB_SYSTEMMODAL, "Delete item", "Deleting UDF-created Item 12")
    ;_GUICtrlListView_DeleteItem($idListview, 12)

;~  MsgBox($MB_SYSTEMMODAL, "Single", "Deleting native-created Item 9")

    For $i = 0 To 8
        $read = GUICtrlRead($i)
        Sleep(100)
        _GUICtrlListView_DeleteItem($idListview, $read)
        ConsoleWrite(@error)


    Next


    ; Loop until the user exits
    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                ExitLoop

        EndSwitch
    WEnd

    ; Delete the previous GUI and all controls.
    GUIDelete()
EndFunc   ;==>Example

 

 

Edited by 232showtime
1 person likes this

ill get to that... i still need to learn and understand a lot of codes graduated.gif

Correct answer, learn to walk before you take on that marathon.

Share this post


Link to post
Share on other sites

#13 ·  Posted

Just now, 232showtime said:

or just read $i

 

#include <GUIConstantsEx.au3>
#include <GuiListView.au3>
#include <MsgBoxConstants.au3>

Example()

Func Example()
    GUICreate("ListView Item Deletion", 400, 500)
    Local $idListview = GUICtrlCreateListView("Col 1               |Col 2      |Col 3      ", 10, 10, 380, 480, $LVS_SHOWSELALWAYS)
    GUISetState(@SW_SHOW)

    For $i = 0 To 9
        GUICtrlCreateListViewItem("Native Item " & $i & "|Item " & "-1|Item " & "-2", $idListview)
    Next
    ;For $i = 10 To 20
    ;   _GUICtrlListView_AddItem($idListview, "UDF Item " & $i, -1, 1000 + $i)
    ;  _GUICtrlListView_AddSubItem($idListview, $i, "Item " & $i & "-1", 1)
    ; _GUICtrlListView_AddSubItem($idListview, $i, "Item " & $i & "-2", 2)
    ;Next

    ; Pass the controlID of a native-created ListView to delete both native- and UDF-created Items
    ;MsgBox($MB_SYSTEMMODAL, "Delete item", "Deleting UDF-created Item 12")
    ;_GUICtrlListView_DeleteItem($idListview, 12)

;~  MsgBox($MB_SYSTEMMODAL, "Single", "Deleting native-created Item 9")

    For $i = 0 To 8
        $read = GUICtrlRead($i)
        Sleep(100)
        _GUICtrlListView_DeleteItem($idListview, $read)
        ConsoleWrite(@error)


    Next


    ; Loop until the user exits
    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                ExitLoop

        EndSwitch
    WEnd

    ; Delete the previous GUI and all controls.
    GUIDelete()
EndFunc   ;==>Example

 

so many ways we can skin a cat, I am keeping these snitppets

 

Share this post


Link to post
Share on other sites

#14 ·  Posted

Sorry, Guys I  can NOT figure this one out, the code work great however it will not display this line below, 

" GUICtrlCreateListViewItem('|Default Gateway >|' & $ip2[UBound($ip2) - 1] + 1, $aListViews)" 

$aListViews = GUICtrlCreateListView("| UCS Subnet/ 28||", 76, 8, 361, 473, -1, BitOR($WS_EX_CLIENTEDGE, $LVS_EX_GRIDLINES, $LVS_EX_FULLROWSELECT)) ;56
  GUICtrlCreateListViewItem('|Default Gateway >|' & $ip2[UBound($ip2) - 1] + 1, $aListViews)


                If $Selection1 = True Then
                    $noip = $sValueread
                    ; load row in listview


                    For $j = 0 To $noip


        GUICtrlCreateListViewItem('Static|SREI' & StringFormat('%05d', $Checknum) & 'RE0' & $j & '|' & $ip2[UBound($ip2) - 1] + 15 + $j, $aListViews)
        ;GUICtrlCreateListViewItem('Static|Register ' & $j & '|' & $ip2[UBound($ip2) - 1] + 15 + $j, $alistviews)
                        ConsoleWrite($j & "$noip" & @CRLF)
                    Next
                    ConsoleWrite(@CRLF)

                     ; $noip = $noip -1
                      Local $nItems = _GUICtrlListView_GetItemCount($aListviews)


                   _DeleteRange($aListviews, 0, $nItems -1)


                    ;For $j = _GUICtrlListView_GetItemCount($aListViews) - 2 To 0 Step -1
                    ;   _GUICtrlListView_DeleteItem($aListViews, $j)
                    ;Next


                EndIf

Func _DeleteRange($aListviews, $nStart, $nCount)
  _GUICtrlListView_BeginUpdate($aListviews)

    While $nCount > 0
        _GUICtrlListView_DeleteItem($aListviews, $nStart)
        $nCount -= 1
    WEnd

    _GUICtrlListView_EndUpdate($aListviews)
EndFunc






                For $j = $noprinter To 1 Step -1

                    If $j = 2 Then GUICtrlCreateListViewItem('Static|Front Printer ' & $j & '|' & $ip2[UBound($ip2) - 1] + 35, $aListViews)
                    If $j = 1 Then GUICtrlCreateListViewItem('Last Static|Back Printer ' & $j & '|' & $ip2[UBound($ip2) - 1] + 36, $aListViews)
                Next

 

I was expecting gateaway also to be displayed on the listview

 

 

 

listviewLastCapture.JPG

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now

  • Similar Content

    • Mithrandir
      By Mithrandir
      First of all, I am starting with GUIs but reading examples from the helpfile as well as the forum I made an script in which I have a listview that is going to be populated with user input and sorted at any given moment. When I sort it by the first or second column with the data that was already set when starting the script (1|D,2|C,4|A,3|D ) It sorts correctly but when I add a new element it doesn't sort it right, it seems it dismisses the new entered data. Here is a small example of my code to show the issue:


      #include <ButtonConstants.au3> #include <EditConstants.au3> #include <GUIConstantsEx.au3> #include <ListViewConstants.au3> #include <StaticConstants.au3> #include <WindowsConstants.au3> #include <GuiListView.au3> $Form1_1 = GUICreate("Form1", 969, 569, 209, 134) $ListView1 = GUICtrlCreateListView("Col1|Col2", 32, 128, 353, 321, -1, $LVS_EX_CHECKBOXES) _GUICtrlListView_RegisterSortCallBack($ListView1) GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 0, 115) GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 1, 115) $ListView1_0 = GUICtrlCreateListViewItem("1|D", $ListView1) $ListView1_1 = GUICtrlCreateListViewItem("2|C", $ListView1) $ListView1_2 = GUICtrlCreateListViewItem("4|A", $ListView1) $ListView1_3 = GUICtrlCreateListViewItem("3|B", $ListView1) $bElim = GUICtrlCreateButton("Delete element(s)", 176, 464, 201, 25) $elem = GUICtrlCreateInput("Element", 88, 16, 241, 21) $bAdd = GUICtrlCreateButton("Add letter", 112, 48, 201, 33) GUISetState(@SW_SHOW) While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE _GUICtrlListView_UnRegisterSortCallBack($ListView1) Exit Case $ListView1 _GUICtrlListView_SortItems($ListView1, GUICtrlGetState($ListView1)) Case $bElim $i = 0 $cantitems = _GUICtrlListView_GetItemCount(GUICtrlGetHandle($ListView1)) While $i <= $cantitems If _GUICtrlListView_GetItemChecked(GUICtrlGetHandle($ListView1), $i) Then _GUICtrlListView_DeleteItem(GUICtrlGetHandle($ListView1), $i) Else $i += 1 EndIf WEnd Case $bAdd $number = Random(0, 100, 1) $handlelista = GUICtrlGetHandle($ListView1) _GUICtrlListView_AddItem($handlelista, $number) _GUICtrlListView_AddSubItem($handlelista, _GUICtrlListView_GetItemCount(GUICtrlGetHandle($ListView1)) - 1, GUICtrlRead($elem), 1) EndSwitch WEnd
      Note that if you delete all the elements that are set after the script started and you add new ones, it doesn't sort them.
      What's wrong? Thanks for your help!