Move selected item up or down in Dialog ListBox

Technical support and scripting issues

Moderators: Dorian (MJT support), JRL

Post Reply
mondi
Newbie
Posts: 6
Joined: Thu May 18, 2006 9:56 pm

Move selected item up or down in Dialog ListBox

Post by mondi » Wed May 31, 2006 10:43 pm

Hey all,

I have fallen in love with dialog boxes after some direction for JRL and am racking my brain trying to figure out this little peice.

From the code below you can see what I am trying to do, move selected item(s) in a multiselect listbox up or down depending on wich button is pressed.

I have been searching all over the place for a way to re-order a list and found some stuff on alphabatizing, random sorts... but unfortunatley I couldn't make heads or tails of it.

Does anyone have any suggestions?

Is there an easy way to do this... maybe like use a string replace function to find the selected string, then replace it with the one before or after...

Or should I split it out into an array and iterate through the array?

Anything you might throw at me to get me thinking would be greatly appreciated.

Mondi

Code: Select all


Dialog>Dialog1
   Caption=Dialog1
   Width=336
   Height=434
   Top=265
   Left=619
   ListBox=msListBox1,96,64,137,209,%vList%,true
   Button=Up,80,296,75,25,6
   Button=Down,168,296,75,25,7
   Button=Exit,128,360,75,25,2
EndDialog>Dialog1
LET>vList=Item1%CRLF%Item2%CRLF%Item3%CRLF%Item4%CRLF%Item5%CRLF%Item6%CRLF%Item7%CRLF%Item8%CRLF%Item9%CRLF%Item10
SHOW>Dialog1

LABEL>lLoop1
GetDialogAction>Dialog1,vResult
IF>vResult=6
  //Reorder items so that the selected item(s) up one
ENDIF
IF>vRsult=7
  //Reorder items so that the selected item(s) down one
ENDIF
IF>vResult=2
GOTO>lEndLoop1
ENDIF
ResetDialogAction>Dialog1
GOTO>lLoop1
LABEL>lEndLoop1


User avatar
Rain
Automation Wizard
Posts: 550
Joined: Tue Aug 09, 2005 5:02 pm
Contact:

Post by Rain » Fri Jun 02, 2006 3:06 pm

Hi Mondi,

Here is a dialog that will do what you asked for. If there is a different way maybe someone can post it I couldn't come up with one.

Code: Select all

Dialog>Dialog1
   Caption=Dialog1
   Width=336
   Height=434
   Top=265
   Left=619
   ListBox=msListBox1,96,64,137,209,%vList%,true
   Button=Up,80,296,75,25,6
   Button=Down,168,296,75,25,7
   Button=Exit,128,360,75,25,2
EndDialog>Dialog1
LET>vList=Item1%CRLF%Item2%CRLF%Item3%CRLF%Item4%CRLF%Item5%CRLF%Item6%CRLF%Item7%CRLF%Item8%CRLF%Item9%CRLF%Item10
SHOW>Dialog1
let>list_item=0
press down
press up
LABEL>lLoop1
wait>0.05
GetDialogAction>Dialog1,vResult
IF>vResult=2,lEndLoop1
IF>vResult=6,Up One
IF>vResult=7,Down One
GOTO>lLoop1
Label>Down One
add>list_item,1
if>list_item>9
let>list_item=9
endif
Let>Dialog1.msListBox1.ItemIndex=%list_item%
ResetDialogAction>Dialog1
press tab *2
press up
press down
goto>lLoop1
Label>Up One
sub>list_item,1
if>list_item<0>list_item=0
endif
Let>Dialog1.msListBox1.ItemIndex=%list_item%
ResetDialogAction>Dialog1
press tab *3
press down
press up
goto>lLoop1
LABEL>lEndLoop1
I hope this helps.

User avatar
pgriffin
Automation Wizard
Posts: 460
Joined: Wed Apr 06, 2005 5:56 pm
Location: US and Europe

Post by pgriffin » Fri Jun 02, 2006 3:41 pm

Very nice, Rain.

I stuck that one in my toolbox.

mondi
Newbie
Posts: 6
Joined: Thu May 18, 2006 9:56 pm

Post by mondi » Sat Jun 03, 2006 1:06 am

Rain, thanks for the example. It's not exactly what I meant, but I am not sure I explained myself all that clearly. I wanted to move the selected item(s) up or down, not just the 'selector'.

Don't scream when you look at this, but feel free to laugh! This is what I came up with after a couple of days racking my brain. Ugly to be sure, but it works!!

One thing I ran into.. is there a way to preserve the multiple selections in the index value? Dialog.msListBox1.ItemIndex doesn't appear to hold more than just the last selected item.

Please feel free to make fun of me and suggest anything that would make this better.

Mondi

Code: Select all

Dialog>Dialog1
   Caption=Dialog1
   Width=336
   Height=434
   Top=238
   Left=-846
   ListBox=msListBox1,96,64,137,209,%vList%,true
   Button=Up,80,296,75,25,6
   Button=Down,168,296,75,25,7
   Button=Exit,128,360,75,25,2
EndDialog>Dialog1

LET>vList=Item1%CRLF%Item2%CRLF%Item3%CRLF%Item4%CRLF%Item5%CRLF%Item6%CRLF%Item7%CRLF%Item8%CRLF%Item9%CRLF%Item10
SHOW>Dialog1

LABEL>lLoop1
GetDialogAction>Dialog1,vResult
IF>vResult=6
Let>VAREXPLICIT=0
  //First lets get the existing list and seperate it
    //for some reason I am gettting an extra item at the end so I am going to trip the crlf before I sep
	Let>Dialog1.msListBox1.Items.Text={copy(%Dialog1.msListBox1.Items.Text%,1,length(%Dialog1.msListBox1.Items.Text%)-2)}
  SEP>%Dialog1.msListBox1.Items.Text%,%CRLF%,vUnorderedList
  //Now lets get the items that are selected
  SEP>%Dialog1.msListBox1%,%CRLF%,vSelectedItems
  //lets find what positions are selected items are in
  LET>vI=0
  REPEAT>vI
  LET>vI=%vI%+1
    LET>vI2=0
    REPEAT>vI2
      LET>vI2=%vI2%+1
	    IF>vUnorderedList_%vI2%=vSelectedItems_%vI%
		  //This actually creates variables vIndexValue_1..2..3 etc and assigns the current index value
		  //to the variable.. i.e. vIndexValue_1=2 first selected item is index 2
		  LET>vIndexValue_%vI%=%vI2%
		  GOTO>lResult6vI2END
		ENDIF
    UNTIL>vI2,vUnorderedList_count
	LABEL>lResult6vI2END
  UNTIL>vI,vSelectedItems_count
  //Now lets re-order the list
  LET>vI=0
  REPEAT>vI
    LET>vI=%vI%+1
  	//If the first item in the list is selected I want to skip it
	//becuase it cant move any higher
	IF>vIndexValue_%vI%<1>vTempIndex=vIndexValue_%vI%
	  LET>vTempItem=vUnorderedList_%vTempIndex%
	  LET>vTempNum=%vTempIndex%-1
	  LET>vUnorderedList_%vTempIndex%=vUnorderedList_%vTempNum%
	  LET>vUnorderedList_%vTempNum%=vTempItem
	ENDIF
  UNTIL>vI,vSelectedItems_count
  //now lets reassemble our list
  LET>vI=0
  //clear the existing list
  LET>Dialog1.msListBox1.Items.Text=
  REPEAT>vI
    LET>vI=%vI%+1
    CON>Dialog1.msListBox1.Items.Text,vUnorderedList_%vI%
    CON>%Dialog1.msListBox1.Items.Text%,%CRLF%
  UNTIL>vI,vUnorderedList_count
//Now lets trim the last CRLF
  Let>Dialog1.msListBox1.Items.Text={copy(%Dialog1.msListBox1.Items.Text%,1,length(%Dialog1.msListBox1.Items.Text%)-2)}
  ResetDialogAction>Dialog1
ENDIF

IF>vResult=7
 Let>VAREXPLICIT=0
  //First lets get the existing list and seperate it
  //for some reason I am gettting an extra item at the end so I am going to trip the crlf before I sep
	Let>Dialog1.msListBox1.Items.Text={copy(%Dialog1.msListBox1.Items.Text%,1,length(%Dialog1.msListBox1.Items.Text%)-2)}
  SEP>%Dialog1.msListBox1.Items.Text%,%CRLF%,vUnorderedList
  //Now lets get the items that are selected
  SEP>%Dialog1.msListBox1%,%CRLF%,vSelectedItems
  //lets find what positions are selected items are in
  LET>vI=0
  REPEAT>vI
  LET>vI=%vI%+1
    LET>vI2=0
    REPEAT>vI2
      LET>vI2=%vI2%+1
	    IF>vUnorderedList_%vI2%=vSelectedItems_%vI%
		  //This actually creates variables vIndexValue_1..2..3 etc and assigns the current index value
		  //to the variable.. i.e. vIndexValue_1=2 first selected item is index 2
		  LET>vIndexValue_%vI%=%vI2%
		  GOTO>lResult7vI2END
		ENDIF
    UNTIL>vI2,vUnorderedList_count
	LABEL>lResult7vI2END
  UNTIL>vI,vSelectedItems_count
  //Now lets re-order the list
  LET>vI=%vSelectedItems_count%+1
  REPEAT>vI
    LET>vI=%vI%-1
  	//If the last item in the list is selected I want to skip it
	//becuase it cant move any lower
	IF>vIndexValue_%vI%<vUnorderedList_count>vTempIndex=vIndexValue_%vI%
	  LET>vTempItem=vUnorderedList_%vTempIndex%
	  LET>vTempNum=%vTempIndex%+1
	  LET>vUnorderedList_%vTempIndex%=vUnorderedList_%vTempNum%
	  LET>vUnorderedList_%vTempNum%=vTempItem
	ENDIF
  UNTIL>vI,1
  //now lets reassemble our list
  LET>vI=0
  //clear the existing list
  LET>Dialog1.msListBox1.Items.Text=
  REPEAT>vI
    LET>vI=%vI%+1
    CON>Dialog1.msListBox1.Items.Text,vUnorderedList_%vI%
    CON>%Dialog1.msListBox1.Items.Text%,%CRLF%
  UNTIL>vI,vUnorderedList_count
//Now lets trim the last CRLF
  Let>Dialog1.msListBox1.Items.Text={copy(%Dialog1.msListBox1.Items.Text%,1,length(%Dialog1.msListBox1.Items.Text%)-2)}
  //trying to preserve the selected items
  Let>Dialog1.msListBox1.ItemIndex=%list_item%
  ResetDialogAction>Dialog1
ENDIF
ENDIF
IF>vResult=2
GOTO>lEndLoop1
ENDIF
//ResetDialogAction>Dialog1
GOTO>lLoop1
LABEL>lEndLoop1


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