Having issues resetting a listbox with multiselect enabled.

Hints, tips and tricks for newbies

Moderators: Dorian (MJT support), JRL

Post Reply
sarver311
Pro Scripter
Posts: 84
Joined: Tue Jun 17, 2008 6:37 pm

Having issues resetting a listbox with multiselect enabled.

Post by sarver311 » Mon Oct 19, 2009 10:03 pm

After banging my head against the wall for several hours I discovered that it doesn't seem to be possible to alter the itemindex value for a listbox that has multiselect set to true.

Basically what I'm trying to do is put a reset button in my script that will clear all my edit, comboboxes, listboxes etc. and clear them out and deselect any listbox selections. However I am having a hard time figuring out a good way to do this. I found the following post ironically enough by a different Josh Sarver (small world) that looked like it would allow me to change my selection but I can't figure out how to deselect items.

http://www.mjtnet.com/forum/viewtopic.p ... 997c09033e

Here is an example script of what point I had the script at so far but selects everything instead of nothing.

onevent>dialog_event,dialog1,90,changelist
let>dialog1.mslistbox2.itemindex=2
Dialog>Dialog1
Caption=Dialog1
Width=445
Height=250
Top=124
Left=1044
Button=msButton1,126,154,75,25,90
ListBox=msListBox2,264,35,121,97,This%CRLF%is%CRLF%my%CRLF%list%CRLF%Example,true
EndDialog>Dialog1

show>dialog1

label>actionloop
getdialogaction>dialog1,r

goto>actionloop

//I want this sub to just deselect anything in this listbox that has been selected
srt>changelist
libfunc>user32,handle
let>lb_setsel=389
LibFunc>user32,SendMessageA,SMres,dialog1.mslistBox2.handle,LB_SETSEL,1,-1

libfree>handle
resetdialogaction>dialog1
END>changelist

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

Post by JRL » Mon Oct 19, 2009 11:20 pm

The post you refered to is only good for selecting items programatically. If you want to alter the list you need to change the value of the listbox list variable. In this case that variable is named "DIALOG1.MSLISTBOX2.ITEMS.TEXT". You should be able to see this in the watch list while stepping through your code in the editor.

Try this:

Code: Select all

onevent>dialog_event,dialog1,90,changelist
onevent>dialog_event,dialog1,100,emptylist
let>dialog1.mslistbox2.itemindex=2
Dialog>Dialog1
   Caption=Dialog1
   Width=445
   Height=250
   Top=404
   Left=44
   Button=New List,126,114,75,25,90
   Button=Clear List,126,152,75,25,100
   ListBox=msListBox2,264,35,121,97,This%CRLF%is%CRLF%my%CRLF%list%CRLF%Example,true
EndDialog>Dialog1

show>dialog1

label>actionloop
  getdialogaction>dialog1,r
  If>r=2
    Exit>0
  EndIf
goto>actionloop

//I want this sub to just deselect anything in this listbox that has been selected
srt>changelist
  ///To Set the listbox to a new list
  Let>DIALOG1.MSLISTBOX2.ITEMS.TEXT=item1%CRLF%item2%CRLF%item3
  resetdialogaction>dialog1
END>changelist

srt>emptylist
  ///To set the listbox to empty
  Let>DIALOG1.MSLISTBOX2.ITEMS.TEXT=
  resetdialogaction>dialog1
END>emptylist

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

Post by JRL » Tue Oct 20, 2009 3:44 am

On re-reading I think I may have misunderstood. If you want to clear the
selections try setting the "DIALOG1.MSLISTBOX2.ITEMS.TEXT" variable to nothing, reset the dialog, then set "DIALOG1.MSLISTBOX2.ITEMS.TEXT" back to its original value and reset the dialog again.

Try this instead:

Code: Select all


onevent>dialog_event,dialog1,90,ClearSelections

let>dialog1.mslistbox2.itemindex=2
Dialog>Dialog1
   Caption=Dialog1
   Width=445
   Height=250
   Top=404
   Left=44
   Button=Clear Selections,126,152,125,25,90
   ListBox=msListBox2,264,35,121,97,This%CRLF%is%CRLF%my%CRLF%list%CRLF%Example,true
EndDialog>Dialog1

show>dialog1

label>actionloop
  getdialogaction>dialog1,r
  If>r=2
    Exit>0
  EndIf
goto>actionloop

//I want this sub to just deselect anything in this listbox that has been selected
srt>ClearSelections
  Let>var=DIALOG1.MSLISTBOX2.ITEMS.TEXT
  Let>DIALOG1.MSLISTBOX2.ITEMS.TEXT=
  resetdialogaction>dialog1
  Let>DIALOG1.MSLISTBOX2.ITEMS.TEXT=var
  resetdialogaction>dialog1
END>ClearSelections

sarver311
Pro Scripter
Posts: 84
Joined: Tue Jun 17, 2008 6:37 pm

Post by sarver311 » Tue Oct 20, 2009 2:23 pm

UGH! That was so simple yet perfect! I was so focused on the stupid itemindex I failed to think outside the box.

Thanks JRL that works perfectly.

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