antonioj84 Posted April 19, 2017 Posted April 19, 2017 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
232showtime Posted April 19, 2017 Posted April 19, 2017 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 ill get to that... i still need to learn and understand a lot of codes Correct answer, learn to walk before you take on that marathon.
antonioj84 Posted April 19, 2017 Author Posted April 19, 2017 (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. expandcollapse popup#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 April 19, 2017 by antonioj84 error
Tekk Posted April 19, 2017 Posted April 19, 2017 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 antonioj84 1
antonioj84 Posted April 19, 2017 Author Posted April 19, 2017 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
antonioj84 Posted April 19, 2017 Author Posted April 19, 2017 question is how would you skip the last item ?
Tekk Posted April 19, 2017 Posted April 19, 2017 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 antonioj84 1
232showtime Posted April 19, 2017 Posted April 19, 2017 (edited) I dont really know your condition to delete LV items so here's what I've got. expandcollapse popup#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 April 19, 2017 by 232showtime antonioj84 1 ill get to that... i still need to learn and understand a lot of codes Correct answer, learn to walk before you take on that marathon.
Tekk Posted April 19, 2017 Posted April 19, 2017 Your goal is to remove "Native Item 0" to "Native Item 8" leaving only "Native Item 9" in the list correct?
antonioj84 Posted April 19, 2017 Author Posted April 19, 2017 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
antonioj84 Posted April 19, 2017 Author Posted April 19, 2017 14 minutes ago, 232showtime said: I dont really know your condition to delete LV items so here's what I've got. expandcollapse popup#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
232showtime Posted April 19, 2017 Posted April 19, 2017 (edited) or just read $i expandcollapse popup#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 April 19, 2017 by 232showtime antonioj84 1 ill get to that... i still need to learn and understand a lot of codes Correct answer, learn to walk before you take on that marathon.
antonioj84 Posted April 19, 2017 Author Posted April 19, 2017 Just now, 232showtime said: or just read $i expandcollapse popup#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
antonioj84 Posted April 21, 2017 Author Posted April 21, 2017 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)" expandcollapse popup$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
antonioj84 Posted April 23, 2017 Author Posted April 23, 2017 this solve the issue $counter=1 ; nitialize counter For $j = _GUICtrlListView_GetItemCount($aListViews) - 2 To 0 Step -1 if $counter > $noip-1 Then ExitLoop ; exit before reaching gateway _GUICtrlListView_DeleteItem($aListViews, $j) $counter += 1 ; increment next
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now