Pre-select Items in a Multi-select Listbox

Hints, tips and tricks for newbies

Moderators: JRL, Dorian (MJT support)

Pre-select Items in a Multi-select Listbox

Postby BrianM » Mon Apr 15, 2013 2:20 am

I have been digging through the forums and finally give up. I am just going to have to ask...

How do I pre-select several items in a multi-select listbox? Please and thankyou. :)
BrianM
Newbie
 
Posts: 7
Joined: Sat Feb 09, 2013 2:17 am

Postby JRL » Mon Apr 15, 2013 4:18 am

Good question. I don't know the answer. I tried several things with no success. The only thing I would suggest is to treat the dialog as if its from some other application and manipulate the list box with clicks and picks.

The following has some of the stuff I tried remarked out and also a way to get sequential lines using the SelectedIndex property as a start point and arrowing down while holding the shift key. This method requires a non-modal dialog. Non sequential selection would require mouse clicks or maybe the SelectMenu> Function with ctrl pressed. I didn't think to try that until now.

Perhaps someone else has a real answer?


Code: Select all
Dialog>Dialog1
object Dialog1: TForm
  ClientHeight = 212
  ClientWidth = 476
  object MSListBox1: tMSListBox
    Left = 20
    Top = 17
    Width = 121
    Height = 97
    Text = 'Item1'#13#10'Item2'#13#10'Item3'#13#10'Item4'#13#10
    SelectedIndex = -1
  end
end
EndDialog>Dialog1

//Let>List=Item1%crlf%Item3
AddDialogHandler>Dialog1,,OnClose,Quit
//SetDialogProperty>Dialog1,MSListBox1,MultiSelect,True
SetDialogProperty>Dialog1,MSListBox1,SelectedIndex,1
//SetDialogProperty>Dialog1,MSListBox1,SelectedItems,List
SetDialogProperty>Dialog1,MSListBox1,MultiSelect,True

Show>Dialog1

SetDialogObjectFocus>Dialog1,MSListBox1

Press shift
  Press down
Release shift

Label>Loop
  Wait>0.01
Goto>Loop

SRT>Quit
  Exit>0
END>Quit
User avatar
JRL
Automation Wizard
 
Posts: 3198
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Good Day

Postby PepsiHog » Mon Apr 15, 2013 11:31 pm

Hello BrianM, I'm BrianJ. (no joke)

In the properties window (while the listbox is selected) there is a property called MultiSelect. Set that to True. Then to select more than one, press and hold CTRL while clicking on your choices.

Drink Pepsi, Get Fat, Loose Weight, Repeat,
PepsiHog

[Edit] - Ooops. Sorry. I miss read your question. Don't know that one.(Yet)
MS v14 Windows 7 and XP sp3

PepsiHog. Yep! I drink LOTS of Pepsi (still..in 2017) AND enjoy programming. (That's my little piece of heaven!)

The immensity of the scope of possibilities within Macro Scheduler pushes the user beyond just macros!
User avatar
PepsiHog
Automation Wizard
 
Posts: 334
Joined: Wed Apr 08, 2009 4:19 pm
Location: Florida

Postby BrianM » Tue Apr 16, 2013 1:03 am

I understand how to do that. I am talking about pre-populating the listbox with selections at runtime for the user. The user can then add or take away choices already pre-selected with the CTRL and Left Click.

How do I (pre)select the choices for the user?

Brian
BrianM
Newbie
 
Posts: 7
Joined: Sat Feb 09, 2013 2:17 am

Postby Marcus Tettmar » Tue Apr 16, 2013 8:57 am

There is no one specific answer for this as list boxes can vary in the way they work - there are list boxes and there are list boxes - all constructed in different ways using different technologies.

Sometimes you can "drill down" on items using the keyboard and usually sending a space to it will check it. This is mentioned here about half way down:
http://www.mjtnet.com/blog/2006/01/16/k ... shortcuts/

Unfortunately not all list boxes support this and you might have to use other techniques. You might be able to get the index of an item you want using the GetListItem function:
http://www.mjtnet.com/manual/index.html?getlistitem.htm

Once you have the index of an item given it's text you can navigate to it easily (if it is index 5, then Home and Press Down * 4 will get you there).

You can get a list of all the items in a list using this tool:
http://www.mjtnet.com/blog/2012/07/26/r ... a-listbox/

Once you have a list of items you again know where each one you want to select is in the list, so you know how many times to move down it for each one.

And here's another solution we once had to use where we cycle through the list and use text capture for each item to see if we're at the one we want:
http://www.mjtnet.com/blog/2009/07/02/s ... down-list/

If the list isn't too long you could even use image recognition instead of text capture.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?
User avatar
Marcus Tettmar
Site Admin
 
Posts: 6789
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK

Postby Marcus Tettmar » Tue Apr 16, 2013 9:05 am

I've just realised you might be referring to a list box that you create in a custom dialog and my answer was about selecting items in a list box on a third party application. Apologies if I got that wrong.

At present I don't see a way to select multiple-items at runtime or design time for a custom dialog list box object. That will require additional functionality - we'll need to add a custom property to the object.

I've made a note in our wish list to have this done.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?
User avatar
Marcus Tettmar
Site Admin
 
Posts: 6789
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK

Postby BrianM » Thu Apr 18, 2013 5:24 am

Indeed Marcus.. I was referring to your very own Diaglog - List box. Thanks for the reply. I will work around it - at least I can stop looking. It was driving me crazy.

Brian
BrianM
Newbie
 
Posts: 7
Joined: Sat Feb 09, 2013 2:17 am

Re: Pre-select Items in a Multi-select Listbox

Postby kdtrog » Wed Mar 15, 2017 2:24 pm

I am returning to this thread because I would also like to mark items of a listbox selected within my script code. (which means the items are highlighted and items will be returned to the getdialogproperties ... SelectedItems.

Is there meanwhile a solution for this?

Klaus
kdtrog
Junior Coder
 
Posts: 26
Joined: Wed Mar 28, 2007 9:36 am

Re: Pre-select Items in a Multi-select Listbox

Postby zabros2020 » Thu Mar 16, 2017 5:32 am

Hi,

I'm curious what you are trying to solve here. Only cause you already know what needs to be added from existing variables...
Example below does not highlight but will select what you want from a list generated at runtime...

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 = 247
  Top = 96
  HelpContext = 5000
  BorderIcons = [biSystemMenu]
  Caption = 'CustomDialog'
  ClientHeight = 265
  ClientWidth = 604
  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 lstMyList: tMSListBox
    Left = 14
    Top = 9
    Width = 173
    Height = 160
    Style = lbOwnerDrawFixed
    ExtendedSelect = False
    ItemHeight = 13
    MultiSelect = True
    TabOrder = 0
    SelectedIndex = -1
  end
  object lstMyList1: tMSListBox
    Left = 348
    Top = 10
    Width = 173
    Height = 160
    ItemHeight = 13
    TabOrder = 1
    SelectedIndex = -1
  end
  object btnMove: tMSButton
    Left = 232
    Top = 48
    Width = 75
    Height = 25
    Caption = 'Move'
    TabOrder = 2
    DoBrowse = False
    BrowseStyle = fbOpen
  end
  object btnExit: tMSButton
    Left = 233
    Top = 203
    Width = 75
    Height = 25
    Caption = 'Exit'
    TabOrder = 11
    DoBrowse = False
    BrowseStyle = fbOpen
  end
end
EndDialog>Dialog1

ArrayDim>arrLst,100
Let>i=0
Let>list=
Repeat>i
  Add>i,1
  Let>tmp=ListTest%i%%CRLF%
  Let>arrLst_%i%=%tmp%
  ConCat>%list%,%tmp%
Until>i=100

SetDialogProperty>Dialog1,lstMyList,Text,%list%
SetDialogProperty>Dialog1,lstMyList,SelectedIndex,0
AddDialogHandler>Dialog1,btnMove,OnClick,Move
AddDialogHandler>Dialog1,btnExit,OnClick,Exit
Show>Dialog1,

SRT>Move
  Random>100,ran1
  Random>100,ran2
  Random>100,ran3
  Random>100,ran4
  Let>i=0
  Let>list=
  Repeat>i
    Add>i,1
    If>i=%ran1%
      Let>tmp=arrLst_%i%
      ConCat>%list%,%tmp%
    EndIf
    If>i=%ran2%
      Let>tmp=arrLst_%i%
      ConCat>%list%,%tmp%
    EndIf
    If>i=%ran3%
      Let>tmp=arrLst_%i%
      ConCat>%list%,%tmp%
    EndIf
    If>i=%ran4%
      Let>tmp=arrLst_%i%
      ConCat>%list%,%tmp%
    EndIf
    SetDialogProperty>Dialog1,lstMyList1,Text,%list%
    SetDialogProperty>Dialog1,lstMyList1,SelectedIndex,0

  Until>i=100
END>Move

SRT>Exit
  Exit>
END>Exit
Loving MS's Capabilities!!!
zabros2020
Pro Scripter
 
Posts: 69
Joined: Sun May 03, 2009 11:49 pm
Location: AU

Re: Pre-select Items in a Multi-select Listbox

Postby kdtrog » Thu Mar 16, 2017 7:40 am

Thanks for your reply and your example. My intention is to let the user define a searchstring. I will take the searchstring and find all hits in a listbox and mark them selected so they appear to the user as highlighted items.

Klaus
kdtrog
Junior Coder
 
Posts: 26
Joined: Wed Mar 28, 2007 9:36 am

Re: Pre-select Items in a Multi-select Listbox

Postby zabros2020 » Thu Mar 16, 2017 11:17 am

ok, so the following little hack will preload the list based on random number from list item.
Searchstring text box will lookup in the list exact match before it selects it.
Click move button to check the correct items were selected

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 = 247
  Top = 96
  HelpContext = 5000
  BorderIcons = [biSystemMenu]
  Caption = 'CustomDialog'
  ClientHeight = 493
  ClientWidth = 604
  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 lstMyList: tMSListBox
    Left = 14
    Top = 32
    Width = 173
    Height = 377
    Style = lbOwnerDrawFixed
    ExtendedSelect = False
    ItemHeight = 13
    MultiSelect = True
    TabOrder = 0
    SelectedIndex = -1
  end
  object lstMyList1: tMSListBox
    Left = 348
    Top = 34
    Width = 173
    Height = 160
    ItemHeight = 13
    TabOrder = 1
    SelectedIndex = -1
  end
  object btnMove: tMSButton
    Left = 232
    Top = 48
    Width = 75
    Height = 25
    Caption = 'Move'
    TabOrder = 2
    DoBrowse = False
    BrowseStyle = fbOpen
  end
  object btnExit: tMSButton
    Left = 233
    Top = 203
    Width = 75
    Height = 25
    Caption = 'Exit'
    TabOrder = 3
    DoBrowse = False
    BrowseStyle = fbOpen
  end
  object txtSearch: TEdit
    Left = 14
    Top = 6
    Width = 173
    Height = 21
    TabOrder = 12
  end
end
EndDialog>Dialog1

ArrayDim>arrLst,100
Let>i=0
Let>list=
Repeat>i
  Add>i,1
  Let>tmp=list%i%%CRLF%
  Let>arrLst_%i%=%tmp%
  ConCat>%list%,%tmp%
Until>i=100

SetDialogProperty>Dialog1,lstMyList,Text,%list%
SetDialogObjectFocus>Dialog1,txtSearch
GoSub>LoadHandlers

Show>Dialog1

'to preselect
Let>n=0
Label>Loop
  Add>n,1
  If>n=1
    GoSub>PreSelected
  EndIf
Goto>Loop

SRT>LoadHandlers
  AddDialogHandler>Dialog1,lstMyList,OnClick,Selected
  AddDialogHandler>Dialog1,txtSearch,OnChange,SearchSelect
  AddDialogHandler>Dialog1,btnMove,OnClick,Move
  AddDialogHandler>Dialog1,btnExit,OnClick,Exit
END>LoadHandlers

SRT>PreSelected
  Let>r=0
  Repeat>r
    Add>r,1
    Random>100,ran%r%
    Let>ran=ran%r%
    SetDialogProperty>Dialog1,lstMyList,SelectedIndex,%ran%
    GetWindowHandle>CustomDialog,hWndParent
    Let>tmp=arrLst_%ran%
    Trim>%tmp%,tmp
    Let>WIN_USEHANDLE=1
    UIClick>%hWndParent%,%tmp%
    Wait>0.3
  Until>r=5
  SetDialogObjectFocus>Dialog1,txtSearch
END>PreSelected

SRT>SearchSelect
  GetDialogProperty>Dialog1,txtSearch,Text,tmpStr
  Length>tmpStr,chkLen
  If>chkLen>3
    'get exact match
    Let>i=0
    Repeat>i
      Add>i,1
      Let>tmp=arrLst_%i%
      Trim>%tmp%,tmp
      If>tmp=%tmpStr%
        Position>%tmp%,%itemsSel%,1,iPos
        If>iPos=0
          SetDialogProperty>Dialog1,lstMyList,SelectedIndex,%i%
          GetWindowHandle>CustomDialog,hWndParent
          Let>WIN_USEHANDLE=1
          UIClick>%hWndParent%,%tmp%
          Wait>0.2
          Let>i=0
          SetDialogObjectFocus>Dialog1,txtSearch
        EndIf
      EndIf
    Until>i=0
  Endif
END>SearchSelect

SRT>Selected
  GetDialogProperty>Dialog1,lstMyList,SelectedItems,itemsSel
END>Selected

SRT>Move
  SetDialogProperty>Dialog1,lstMyList1,Text,%itemsSel%
  SetDialogProperty>Dialog1,lstMyList1,SelectedIndex,0
END>Move

SRT>Exit
  Exit>
END>Exit
Loving MS's Capabilities!!!
zabros2020
Pro Scripter
 
Posts: 69
Joined: Sun May 03, 2009 11:49 pm
Location: AU

Re: Pre-select Items in a Multi-select Listbox

Postby kdtrog » Fri Mar 17, 2017 1:45 pm

:D Ah what a nice trick. This is exactly what I was looking for.
Code: Select all
 UIClick>%hWndParent%,%tmp%


Many thanks for your help. May I ask if you also know a solution if the listbox is defined with "ExtendedSelect=true" ?

Klaus
kdtrog
Junior Coder
 
Posts: 26
Joined: Wed Mar 28, 2007 9:36 am

Re: Pre-select Items in a Multi-select Listbox

Postby zabros2020 » Sun Mar 19, 2017 12:35 pm

kdtrog wrote::D Ah what a nice trick. This is exactly what I was looking for.
Code: Select all
 UIClick>%hWndParent%,%tmp%


Many thanks for your help. May I ask if you also know a solution if the listbox is defined with "ExtendedSelect=true" ?

Klaus


Hi Klaus,
I've never had to use ExtendedSelect. Judging by its behavior, it looks like it requires LDown instead of UIClick. It becomes more of a user action type of behaviour.
I've tried a couple of tests, but all fail to select the correct amount of items, especially when the items are located further down the list.
Here is what i have:
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 = 247
  Top = 96
  HelpContext = 5000
  BorderIcons = [biSystemMenu]
  Caption = 'CustomDialog'
  ClientHeight = 493
  ClientWidth = 604
  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 lstMyList: tMSListBox
    Left = 14
    Top = 32
    Width = 173
    Height = 377
    Style = lbOwnerDrawFixed
    ExtendedSelect = True
    ItemHeight = 13
    MultiSelect = True
    TabOrder = 0
    SelectedIndex = -1
  end
  object lstMyList1: tMSListBox
    Left = 348
    Top = 34
    Width = 173
    Height = 160
    Style = lbOwnerDrawFixed
    ExtendedSelect = True
    ItemHeight = 13
    MultiSelect = True
    TabOrder = 1
    SelectedIndex = -1
  end
  object btnMove: tMSButton
    Left = 232
    Top = 48
    Width = 75
    Height = 25
    Caption = 'Move'
    TabOrder = 2
    DoBrowse = False
    BrowseStyle = fbOpen
  end
  object btnExit: tMSButton
    Left = 233
    Top = 203
    Width = 75
    Height = 25
    Caption = 'Exit'
    TabOrder = 3
    DoBrowse = False
    BrowseStyle = fbOpen
  end
  object txtSearch: TEdit
    Left = 14
    Top = 6
    Width = 173
    Height = 21
    TabOrder = 4
  end
end
EndDialog>Dialog1

ArrayDim>arrLst,100
Let>i=0
Let>list=
Repeat>i
  Add>i,1
  Let>tmp=list%i%%CRLF%
  Let>arrLst_%i%=%tmp%
  ConCat>%list%,%tmp%
Until>i=100

SetDialogProperty>Dialog1,lstMyList,Text,%list%
SetDialogObjectFocus>Dialog1,txtSearch
GoSub>LoadHandlers

Show>Dialog1

'to preselect
Let>n=0
Label>Loop
  Add>n,1
  If>n=1
    GoSub>PreSelected
  EndIf
Goto>Loop

SRT>LoadHandlers
  AddDialogHandler>Dialog1,lstMyList,OnClick,Selected
  AddDialogHandler>Dialog1,txtSearch,OnChange,SearchSelect
  AddDialogHandler>Dialog1,btnMove,OnClick,Move
  AddDialogHandler>Dialog1,btnExit,OnClick,Exit
END>LoadHandlers

SRT>PreSelected
  Let>r=0
  Repeat>r
    Add>r,1
    Random>100,ran%r%
    Let>ran=ran%r%
    SetDialogProperty>Dialog1,lstMyList,SelectedIndex,%ran%
    Let>tmp=arrLst_%ran%
    Trim>%tmp%,tmp
    WaitReady>0
    GetWindowHandle>CustomDialog,hWndParent
    FindObject>hWndParent,tMSListBox,,2,hWnd,X1,Y1,X2,Y2,result
    Let>WIN_USEHANDLE=1
    UIPos>%hWnd%,%tmp%,pos
    Separate>pos,%COMMA%,XY
    MouseMove>%XY_1%,%XY_2%
    LDown
    Add>ran,10
    SetDialogProperty>Dialog1,lstMyList,SelectedIndex,%ran%
    Add>XY_2,100
    Wait>0.3
    MouseMove>%XY_1%,%XY_2%
    Wait>0.3
    LUp
    let>r=5
    Let>WIN_USEHANDLE=0
  Until>r=5
  SetDialogObjectFocus>Dialog1,txtSearch
END>PreSelected

SRT>SearchSelect
  GetDialogProperty>Dialog1,txtSearch,Text,tmpStr
  Length>tmpStr,chkLen
  If>chkLen>3
    'get exact match
    Let>i=0
    Repeat>i
      Add>i,1
      Let>tmp=arrLst_%i%
      Trim>%tmp%,tmp
      If>tmp=%tmpStr%
        Position>%tmp%,%itemsSel%,1,iPos
        If>iPos=0
          SetDialogProperty>Dialog1,lstMyList,SelectedIndex,%i%
          GetWindowHandle>CustomDialog,hWndParent
          Let>WIN_USEHANDLE=1
          'UIClick>%hWndParent%,%tmp%
          UISelect>%hWndParent%,%tmp%
          UISelect>%hWndParent%,%i%
          Wait>0.2
          Let>i=0
          SetDialogObjectFocus>Dialog1,txtSearch
          Let>WIN_USEHANDLE=0
          Release CTRL
        EndIf
      EndIf
    Until>i=0
  Endif
END>SearchSelect

SRT>Selected
  GetDialogProperty>Dialog1,lstMyList,SelectedItems,itemsSel
END>Selected

SRT>Move
  SetDialogProperty>Dialog1,lstMyList1,Text,%itemsSel%
  SetDialogProperty>Dialog1,lstMyList1,SelectedIndex,0
END>Move

SRT>Exit
  Exit>
END>Exit
Loving MS's Capabilities!!!
zabros2020
Pro Scripter
 
Posts: 69
Joined: Sun May 03, 2009 11:49 pm
Location: AU

Re: Pre-select Items in a Multi-select Listbox

Postby kdtrog » Mon Mar 20, 2017 2:53 pm

Hi Zabros. Thank you again for sharing your ideas. But you are right it is some kind of instability of results. So I meanwhile changed the logical design of my application,
I am now reloading the list field with the items from the query result and offer a button to switch back to the common list.
The users are happy with this solution. :wink:
Happy scripting

Klaus
kdtrog
Junior Coder
 
Posts: 26
Joined: Wed Mar 28, 2007 9:36 am


Return to Beginners

Who is online

Users browsing this forum: Majestic-12 [Bot] and 5 guests