Setting an item in a list box

Technical support and scripting issues

Moderators: JRL, Dorian (MJT support)

Post Reply
kpassaur
Automation Wizard
Posts: 696
Joined: Wed Jul 07, 2004 1:55 pm

Setting an item in a list box

Post by kpassaur » Tue May 25, 2010 2:15 pm

I am using ver 12 and have a dialog that displays a list box with an up and down button. The user selects an item, clicks on the button and it moves it up or down. The same sort of thing you see all time. Almost exactly what you see in ver 12 for setting the tab order. If you cannot use the auto function which is really cool.

Anyway, it works and was simple to create with ver 12. However, I cannot seem to figure out how to have the item selected once it refreshes. The reason being is that the user could just keep pressing a button to contine to move it up or down. Instead the user has to select the item again each time they want to move it up one in the list.




VBSTART
Const ssfCOMMONAPPDATA = &H23

Function GetSpecialFolder(FolderID)
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.Namespace(FolderID)
Set objFolderItem = objFolder.Self
GetSpecialFolder = objFolderItem.Path
End Function
VBEND

VBEval>GetSpecialFolder(ssfCOMMONAPPDATA),pathComAppData
Let>settinsdir=%pathComAppData%\edocfile\PDF Router

Let>comma=,

Dialog>RulesDialog
object RulesDialog: TForm
Left = 1647
Top = 112
HelpContext = 5000
BorderIcons = [biSystemMenu]
Caption = ' Builder'
ClientHeight = 338
ClientWidth = 424
Color = clWhite
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
OldCreateOrder = True
Position = poDesktopCenter
ShowHint = True
OnTaskBar = False
PixelsPerInch = 96
TextHeight = 13
object Label5: TLabel
Left = 96
Top = 16
Width = 144
Height = 16
Caption = 'Rule Priority'
Font.Charset = ANSI_CHARSET
Font.Color = clWindowText
Font.Height = -16
Font.Name = 'Gotthard'
Font.Style = []
ParentFont = False
end
object ebutton: tMSButton
Left = 330
Top = 281
Width = 75
Height = 25
Caption = 'Exit'
DoubleBuffered = True
ModalResult = 2
ParentDoubleBuffered = False
TabOrder = 2
DoBrowse = False
BrowseStyle = fbOpen
end
object Down: tMSButton
Left = 341
Top = 89
Width = 44
Height = 25
Caption = 'Down'
DoubleBuffered = True
ParentDoubleBuffered = False
TabOrder = 0
DoBrowse = False
BrowseStyle = fbOpen
end
object Up: tMSButton
Left = 341
Top = 57
Width = 44
Height = 25
Caption = 'Up'
DoubleBuffered = True
ParentDoubleBuffered = False
TabOrder = 1
DoBrowse = False
BrowseStyle = fbOpen
end
object ruleslistbox: tMSListBox
Left = 40
Top = 48
Width = 257
Height = 249
ItemHeight = 13
TabOrder = 11
SelectedIndex = -1
end
end
EndDialog>RulesDialog

IfDirExists>%settinsdir%,haveappdir
Let>RP_WAIT=1
Let>RP_WINDOWMODE=0
RunProgram>cmd /c MD "%settinsdir%"
Label>haveappdir

Iffileexists>%settinsdir%\Rules.csv
ReadFile>%settinsdir%\Rules.csv,rlist
Else
Let>rlist=
Endif


SetDialogProperty>RulesDialog,ruleslistbox,Text,%rlist%
AddDialogHandler>RulesDialog,Up,OnClick,Move_Up
AddDialogHandler>RulesDialog,Down,OnClick,Move_Down



SRT>Move_Up
GetDialogProperty>RulesDialog,ruleslistbox,SelectedIndex,rn
Let>rl=%rn%
Let>rn=%rn%+1
If>%rn%=0
Goto>end_of_up_sub
Endif
ReadLn>%settinsdir%\Rules.csv,rn,Line
ReadFile>%settinsdir%\Rules.csv,rlist
StringReplace>%rlist%,%Line%%CRLF%,,nline
DeleteFile>%settinsdir%\temp.csv
Let>WLN_NOCRLF=1
WriteLn>%settinsdir%\temp.csv,result,nline
Let>WLN_NOCRLF=0
DeleteFile>%settinsdir%\Rules.csv
Let>u=1
Label>start_move_up
ReadLn>%settinsdir%\temp.csv,%u%,uline
If>uline=##EOF##,done_with_up
If>%u%=%rl%
WriteLn>%settinsdir%\Rules.csv,result,%Line%
WriteLn>%settinsdir%\Rules.csv,result,%uline%
Else
WriteLn>%settinsdir%\Rules.csv,result,%uline%
Endif
Let>u=u+1
Goto>start_move_up


Label>done_with_up
ReadFile>%settinsdir%\Rules.csv,rlist
SetDialogProperty>RulesDialog,ruleslistbox,SelectedIndex,%rn%
SetDialogProperty>RulesDialog,ruleslistbox,Text,%rlist%
Label>end_of_up_sub
END>Move_Up

SRT>Move_Down
Let>written=NO

GetDialogProperty>RulesDialog,ruleslistbox,SelectedIndex,rn
Let>rl=%rn%+2
Let>rn=%rn%+1
If>%rn%=0
Goto>end_of_down_sub
Endif
ReadLn>%settinsdir%\Rules.csv,rn,Line
ReadFile>%settinsdir%\Rules.csv,rlist
StringReplace>%rlist%,%Line%%CRLF%,,nline
DeleteFile>%settinsdir%\temp.csv
Let>WLN_NOCRLF=1
WriteLn>%settinsdir%\temp.csv,result,nline
Let>WLN_NOCRLF=0
DeleteFile>%settinsdir%\Rules.csv
Let>u=1
Label>start_move_down
ReadLn>%settinsdir%\temp.csv,%u%,uline
If>uline=##EOF##,done_with_down
If>%u%=%rl%
Let>written=YES
WriteLn>%settinsdir%\Rules.csv,result,%Line%
WriteLn>%settinsdir%\Rules.csv,result,%uline%
Else
WriteLn>%settinsdir%\Rules.csv,result,%uline%
Endif
Let>u=u+1
Goto>start_move_down

Label>done_with_down
If>written=NO
WriteLn>%settinsdir%\Rules.csv,result,%Line%
Endif

ReadFile>%settinsdir%\Rules.csv,rlist
SetDialogProperty>RulesDialog,ruleslistbox,SelectedIndex,%rn%
SetDialogProperty>RulesDialog,ruleslistbox,Text,%rlist%
Label>end_of_down_sub
END>Move_Down

Show>RulesDialog,r

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Tue May 25, 2010 4:41 pm

Without modifying your script to make it work here (would be easier if you could post it in a textarea box so that the indentation is preserved) but with a quick look I can see that you are setting SelectedIndex BEFORE setting the item text. So the selected index is probably being wiped out when you replace the items. Try setting SelectedIndex AFTER setting the text.

But if that doesn't help please re-post with indentation preserved and indicate what we need to do to make this work locally (files, paths, etc).
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

kpassaur
Automation Wizard
Posts: 696
Joined: Wed Jul 07, 2004 1:55 pm

Set Item Index

Post by kpassaur » Tue May 25, 2010 5:12 pm

Once again Marcus you were right. I just need to set it correctly now.

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

Post by sarver311 » Tue May 25, 2010 5:31 pm

Hi Marcus I am having a similar issue but I discovered my problem is slightly different.

When my Dialog pops up, I want to have one of my entries in the listbox preselected. I discovered that using the selectindex property does not work if I have the multiselect for the listbox set to true, if it is false it works fine.

Here is an example code. It will not work as is because multiselect is true but once you set this to false it will work fine. Unfortunately I need multiselect capabilities, hence my dilemma =)

Code: Select all

//Set IGNORESPACES to 1 to force script interpreter to ignore spaces.
//If using IGNORESPACES quote strings in {" ... "}
//Let>IGNORESPACES=1
Dialog>Dialog1
object Dialog1: TForm
  Left = 1526
  Top = 108
  HelpContext = 5000
  BorderIcons = [biSystemMenu]
  Caption = 'Select 2nd entry'
  ClientHeight = 216
  ClientWidth = 439
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = True
  ShowHint = True
  OnTaskBar = False
  PixelsPerInch = 96
  TextHeight = 13
  object MSListBox1: tMSListBox
    Left = 117
    Top = 26
    Width = 220
    Height = 143
    ItemHeight = 13
    MultiSelect = True
    TabOrder = 0
    SelectedIndex = -1
  end
  object MSButton1: tMSButton
    Left = 8
    Top = 126
    Width = 97
    Height = 25
    Caption = 'Select 2nd entry'
    DoubleBuffered = True
    ParentDoubleBuffered = False
    TabOrder = 1
    DoBrowse = False
    BrowseStyle = fbOpen
  end
end
EndDialog>Dialog1
AddDialogHandler>Dialog1,MSButton1,OnClick,setindex

SetDialogProperty>dialog1,mslistbox1,text,Entry 1%CRLF%Entry 2%CRLF%Entry 3%CRLF%Entry 4

show>dialog1,r

srt>setindex
Setdialogproperty>dialog1,mslistbox1,selectedindex,2
END>setindex

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