Jump to content
Sign in to follow this  
Damein

Put in an array into a Combobox, then delete selected item

Recommended Posts

Damein

So I have an ini document I am pulling data from and throwing into a combobox, and then allowing someone to select it and remove it if wanted. But I'm running into two problems.

A: It removes all but 1

B: If I remove all comments and then try to check that again, it errors out.

Obviously the B I can just throw in a check to see if there are any values in the ini, but I think I am going about this the wrong way.

Opt("GUIOnEventMode", 1)

#include <Array.au3>


$CurrentGui = "Comments"
$RemoveCommentGui = GUICreate("Comments", 200, 110)
$CommentList = GUICtrlCreateCombo("", 20, 10, 160, 30)
$CurrentComment = IniReadSection(@ScriptDir & "\Test.ini", "Comments")
For $i = 1 To $CurrentComment[0][0]
    GUICtrlSetData(-1, $CurrentComment[$i][1])
Next
$DeleteButton = GUICtrlCreateButton("Remove", 50, 40, 100, 30)
GUICtrlSetOnEvent(-1, "_RemoveCommentFromList")
$CancelButton = GUICtrlCreateButton("Cancel", 50, 80, 100, 30)
GUISetBkColor(0xFFFFFF)
GUISetFont(12)
GUISetState()


Func _RemoveCommentFromList()
    $CommentToRemove = GUICtrlRead($CommentList)
    $Find = _ArraySearch($CurrentComment, $CommentToRemove)
    $Delete = _ArrayDelete($CurrentComment, $Find)
    IniDelete(@ScriptDir & "\Test.ini", "Comments")
    For $i = 1 To $CurrentComment[0][0] - 1
        IniWriteSection(@ScriptDir & "\Test.ini", "Comments", $i & "=" & $CurrentComment[$i][1])
    Next
EndFunc   ;==>_RemoveCommentFromList

While 1
    Sleep(10)
WEnd

Just create an INI with the script named Test.ini

Thanks!


MCR.jpg?t=1286371579

Most recent sig. I made

Quick Launcher W/ Profiles Topic Movie Database Topic & Website | LiveStreamer Pro Website | YouTube Stand-Alone Playlist Manager: Topic | Weather Desktop Widget: Topic | Flash Memory Game: Topic | Volume Control With Mouse / iTunes Hotkeys: Topic | Weather program: Topic | Paws & Tales radio drama podcast mini-player: Topic | Quick Math Calculations: Topic

Share this post


Link to post
Share on other sites
orbs

when you write to ini, you incorrectly use IniWriteSection() while you actually mean to use IniWrite() , like this:

IniWrite(@ScriptDir & "\Test.ini", "Comments", $i, $CurrentComment[$i][1])

also, your code will only work for one time. this is because after an element is removed,

1) the ComboBox is not updated. what is a user supposed to think?

2) you do not update element [0][0] to be reduced by one.

so basically, you have to quit the GUI when one element is removed - or take care of the above conditions.

PS did my reply to the topic below work for you?

'?do=embed' frameborder='0' data-embedContent>>

Edited by orbs

Share this post


Link to post
Share on other sites
Damein

I attempted to revise the code how I felt like I needed but it still doesn't work right.

Opt("GUIOnEventMode", 1)

#include <Array.au3>
#include <GUIConstantsEx.au3>

Global $CurrentComment, $CommentList, $RemoveCommentGUI

_Test()

Func _Test()
$CurrentGui = "Comments"
$RemoveCommentGui = GUICreate("Comments", 200, 110)
GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")
$CommentList = GUICtrlCreateCombo("", 20, 10, 160, 30)
$CurrentComment = IniReadSection(@ScriptDir & "\Test.ini", "Comments")
For $i = 1 To $CurrentComment[0][0]
    GUICtrlSetData(-1, $CurrentComment[$i][1])
Next
$DeleteButton = GUICtrlCreateButton("Remove", 50, 40, 100, 30)
GUICtrlSetOnEvent(-1, "_RemoveCommentFromList")
$CancelButton = GUICtrlCreateButton("Cancel", 50, 80, 100, 30)
GUISetBkColor(0xFFFFFF)
GUISetFont(12)
GUISetState()
EndFunc


Func _RemoveCommentFromList()
    $CommentToRemove = GUICtrlRead($CommentList)
    $Find = _ArraySearch($CurrentComment, $CommentToRemove)
    $Delete = _ArrayDelete($CurrentComment, $Find)
    IniDelete(@ScriptDir & "\Test.ini", "Comments")
    For $i = 1 To $CurrentComment[0][0] - 1
        IniWrite(@ScriptDir & "\Test.ini", "Comments", $i, $CurrentComment[$i][1])
    Next
    IniWrite(@ScriptDir & "\Test.ini", "Comments", 1, "")
    GuiDelete($RemoveCommentGUI)
    _test()
EndFunc   ;==>_RemoveCommentFromList

Func _Exit()
    Exit
EndFunc

While 1
    Sleep(10)
WEnd

And I replied to your reply lol


MCR.jpg?t=1286371579

Most recent sig. I made

Quick Launcher W/ Profiles Topic Movie Database Topic & Website | LiveStreamer Pro Website | YouTube Stand-Alone Playlist Manager: Topic | Weather Desktop Widget: Topic | Flash Memory Game: Topic | Volume Control With Mouse / iTunes Hotkeys: Topic | Weather program: Topic | Paws & Tales radio drama podcast mini-player: Topic | Quick Math Calculations: Topic

Share this post


Link to post
Share on other sites
orbs

in Func _RemoveCommentFromList() you have this line near the end: IniWrite(@ScriptDir & "Test.ini", "Comments", 1, "")

this clears line #1 in your comments ini file. if this is intentional, then that code works fine for me (superficial testing, removed 2 comments from a 5 comments ini). what issue do you encounter?

besides, may i say that you made at-least 2 poor choices in your script:

1) using combobox for multiple selection.

for every action, the user must click the combobox arrow, then click the comment, then click the remove button. that's 3 clicks.

instead, i'd use listview, so the user can select several comments to remove in one action.

2) using recursion where not needed. in such a small script that may not seem important, but don't make it a habit. in larger operations you'll notice degradation of performance, not to mention much harder code to debug.

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
Sign in to follow this  

×