How to delete items in a ListBox?

Hints, tips and tricks for newbies

Moderators: Dorian (MJT support), JRL

Post Reply
Kunutsu
Newbie
Posts: 5
Joined: Fri Oct 19, 2007 7:53 pm

How to delete items in a ListBox?

Post by Kunutsu » Wed Mar 12, 2008 4:47 pm

I'm trying to use a ListBox as an Eventlist. You can insert Events by clicking a button. So you don't know the names of the events.
How can i access (selected) items for instance to delete them from the ListBox.

This is a simplification of what i'm trying to do:

Code: Select all

Dialog>Dialog1
   Caption=Example
   Width=680
   Height=480
   Top=112
   Left=263
   label=Event:,24,245
   Edit=eventname,60,240,250,
   Button=ENTER,340,240,75,25,1
   Button=delete,10,420,75,25,2
   Button=abort,490,420,75,25,3
   Button=START,580,420,75,25,4
   ListBox=Events,8,310,650,100,
EndDialog>Dialog1

Let>EventIndex=0

Label>GUILoop
Show>Dialog1,result

if>%result%=1
	add>EventIndex,1
	ConCat>Dialog1.Events.Items.Text,%EventIndex%: %Dialog1.eventname%
endif

if>%result%=2
	//delete selected or last event?
endif

if>%result%=3,end

if>%result%=4,action

ResetDialogAction>Dialog1
goto>GUILoop

label>action
//process events ...

label>end

User avatar
JRL
Automation Wizard
Posts: 3526
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Wed Mar 12, 2008 6:53 pm

Not very well tested but should give you a possible method to get where you want to go.

Code: Select all

Dialog>Dialog1
   Caption=Example
   Width=680
   Height=480
   Top=112
   Left=263
   label=Event:,24,245
   Edit=eventname,60,240,250,
   Button=ENTER,340,240,75,25,1
   Button=delete,10,420,75,25,2
   Button=abort,490,420,75,25,3
   Button=START,580,420,75,25,4
   ListBox=Events,8,310,650,100,
EndDialog>Dialog1

Let>EventIndex=0

Label>GUILoop
Show>Dialog1,result

if>%result%=1
	add>EventIndex,1
	ConCat>Dialog1.Events.Items.Text,%EventIndex%: %Dialog1.eventname%
endif

if>%result%=2
StringReplace>%Dialog1.Events.Items.Text%,%dialog1.Events%%CRLF%,Dialog1.Events.Items.Text
Separate>%Dialog1.Events.Items.Text%,:,item
If>item_count=0,NoDelete
Let>Renum=1
Let>newindex=0
Let>newlist=

Repeat>Renum
  Add>renum,1
  Add>newindex,1
  Let>value=item_%Renum%
  Separate>value,%CRLF%,var
  ConCat>newlist,%newindex%:%var_1%%CRLF%
Until>Renum,%item_count%
Let>Dialog1.Events.Items.Text=%newlist%
	//delete selected or last event?
Label>NoDelete
endif

if>%result%=3,end

if>%result%=4,action

ResetDialogAction>Dialog1
goto>GUILoop

label>action
//process events ...

label>end

User avatar
JRL
Automation Wizard
Posts: 3526
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Wed Mar 12, 2008 7:59 pm

I can't leave well enough alone. The previous code had issues. If you picked delete with no selection or if all items were deleted, the eventindex variable would be wrong. This should fix those problems.

Code: Select all

Dialog>Dialog1
   Caption=Example
   Width=680
   Height=480
   Top=112
   Left=263
   Label=Event:,24,245
   Edit=eventname,60,240,250,
   Button=ENTER,340,240,75,25,1
   Button=delete,10,420,75,25,2
   Button=abort,490,420,75,25,3
   Button=START,580,420,75,25,4
   ListBox=Events,8,310,650,100,
EndDialog>Dialog1

Let>EventIndex=0

Label>GUILoop
Show>Dialog1,result

if>%result%=1
	add>EventIndex,1
	ConCat>Dialog1.Events.Items.Text,%EventIndex%: %Dialog1.eventname%%CRLF%
	Let>Dialog1.eventname=
endif

if>%result%=2
If>Dialog1.Events.Itemindex=-1,NoDelete
StringReplace>%Dialog1.Events.Items.Text%,%dialog1.Events%%CRLF%,Dialog1.Events.Items.Text
Separate>%Dialog1.Events.Items.Text%,:,item
If>item_count=0,NoDelete
Let>Renum=1
Let>newindex=0
Let>newlist=
Sub>EventIndex,1
Repeat>Renum
  Add>renum,1
  Add>newindex,1
  Let>value=item_%Renum%
  Separate>value,%CRLF%,var
  ConCat>newlist,%newindex%:%var_1%%CRLF%
Until>Renum,%item_count%
Let>Dialog1.Events.Items.Text=%newlist%
	//delete selected or last event?
Label>NoDelete
endif

if>%result%=3,end

if>%result%=4,action

ResetDialogAction>Dialog1
If>%Dialog1.Events.Items.Text%=
  Let>EventIndex=0
EndIf
goto>GUILoop

label>action
//process events ...

label>end

Post Reply
cron
Sign up to our newsletter for free automation tips, tricks & discounts