Loc Posted September 25, 2020 Share Posted September 25, 2020 I'm writing a tool that adds names to listview. When Listview is empty add a name, and when the same name shows an error message. May the high teachers expandcollapse popup#include <ButtonConstants.au3> #include <EditConstants.au3> #include <GUIConstantsEx.au3> #include <ListViewConstants.au3> #include <WindowsConstants.au3> #include <GuiListView.au3> $GUI = GUICreate("", 433, 376, 192, 124) $ListView = GUICtrlCreateListView("Name|Address", 8, 8, 409, 273) GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 0, 200) GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 1, 200) $Input = GUICtrlCreateInput("", 48, 312, 137, 21) $Button = GUICtrlCreateButton("OKE", 240, 312, 113, 25) GUISetState(@SW_SHOW) While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $Button _AddName() EndSwitch WEnd Func _AddName() Local $READ_Name, $ReadM, $List_Name $READ_Name = GUICtrlRead($Input) If $READ_Name <> '' Then $List_Name = _GUICtrlListView_GetItemCount ($ListView) For $i=0 To $List_Name-1 $ReadM = _GUICtrlListView_GetItemTextArray($ListView, $i) If $ReadM[1] = '' Then GUICtrlCreateListViewItem($READ_Name, $ListView) ElseIf $ReadM[1] <> $READ_Name Then GUICtrlCreateListViewItem($READ_Name, $ListView) EndIf Next Else MsgBox(262144 + 4096 + 16, 'Error', 'Please enter full information') EndIf EndFunc Link to comment Share on other sites More sharing options...
Subz Posted September 25, 2020 Share Posted September 25, 2020 Use: For $i=0 To $List_Name Link to comment Share on other sites More sharing options...
Loc Posted September 25, 2020 Author Share Posted September 25, 2020 Why does it only compare the first line. When line 2 doesn't match it, it still adds the name normally Link to comment Share on other sites More sharing options...
Subz Posted September 25, 2020 Share Posted September 25, 2020 Can you explain further? Are you trying to just add only unique records to the listview? I did noticed you didn't have and exitloop after each GUICtrlCreateListViewItem, which means multiple records would be written the more items that were added to the list. Link to comment Share on other sites More sharing options...
Loc Posted September 25, 2020 Author Share Posted September 25, 2020 I added exitloop after each guictrlcreatelistviewitem. Example: Adding CRISTT column name, next time adding CRIST, it is the same, but when I add CRISTT, from the next time, add CRISTT. I mean, it just duplicates the input and the first line of listview Link to comment Share on other sites More sharing options...
pixelsearch Posted September 26, 2020 Share Posted September 26, 2020 (edited) Hi MrRow The following test in your script is not enough to compare the Input name with the content of the whole listview. In fact, you're adding a new item as soon as the Input is different from any item already present in listview. This will end in plenty of duplicate names. If $ReadM[1] = '' Then GUICtrlCreateListViewItem($READ_Name, $ListView) ElseIf $ReadM[1] <> $READ_Name Then GUICtrlCreateListViewItem($READ_Name, $ListView) EndIf A simple way to fix this : 1) Loop through all existing items in listview to make sure they don't already contain the Input name (error message if duplicate is found) 2) Only then, add the new item in listview : now you're sure it's not a duplicate name. Func _AddName() Local $READ_Name, $ReadM, $List_Name $READ_Name = StringStripWS(GUICtrlRead($Input), 1 + 2) ; delete leading & trailing spaces (+++) If $READ_Name <> '' Then $List_Name = _GUICtrlListView_GetItemCount ($ListView) ; 0 first time For $i = 0 To $List_Name - 1 ; 0 To - 1 first time (ok) $ReadM = _GUICtrlListView_GetItemTextArray($ListView, $i) ; never returns an error, regardless of $i If $READ_Name = $ReadM[1] Then MsgBox(262144 + 4096 + 16, 'Error', 'Name ' & $READ_Name & ' already exists') Return EndIf Next GUICtrlCreateListViewItem($READ_Name, $ListView) Else MsgBox(262144 + 4096 + 16, 'Error', 'Please enter full information') EndIf EndFunc Another way to do it, without For... Next loop, using AutoIt function _GUICtrlListView_FindText Func _AddName() Local $READ_Name, $iItem $READ_Name = StringStripWS(GUICtrlRead($Input), 1 + 2) ; delete leading & trailing spaces (+++) If $READ_Name <> '' Then $iItem = _GUICtrlListView_FindText($ListView, $READ_Name, -1, False) ; -1 = search from start, False = no partial If $iItem >= 0 Then ; Input name already exists in listview MsgBox(262144 + 4096 + 16, 'Error', 'Name ' & $READ_Name & ' already exists') Return Else ; -1 (Input name not found in listview) GUICtrlCreateListViewItem($READ_Name, $ListView) EndIf Else MsgBox(262144 + 4096 + 16, 'Error', 'Please enter full information') EndIf EndFunc Good luck Edited September 26, 2020 by pixelsearch added a 2nd way to do it, using _GUICtrlListView_FindText Link to comment Share on other sites More sharing options...
Loc Posted September 26, 2020 Author Share Posted September 26, 2020 @pixelsearch, Thank you very much Link to comment Share on other sites More sharing options...
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