Select an item in ListBox

Technical support and scripting issues

Moderators: Dorian (MJT support), JRL

Post Reply
User avatar
Phil Pendlebury
Automation Wizard
Posts: 538
Joined: Tue Jan 16, 2007 9:00 am
Contact:

Select an item in ListBox

Post by Phil Pendlebury » Sat Nov 03, 2018 9:44 pm

Greetings,

I searched the forum and cannot find the answer to this. It feels though it should be possible but I cannot find it.

I have List box populated.
On selection of an item in the list box you are allowed to move that item up or down in the list. All that is working fine. (It was tricky to code but it is done).
So here is an example list with Beer selected:

Apples
Oranges
Pears
Bananas
Beer

I select Beer and move it up one space (using up/down buttons in the dialog) so that the list becomes:

Apples
Oranges
Pears
Beer
Bananas

The problem is, I want Beer to still be selected so I can instantly move it again. This is how most applications work in this context. The selected item, once moved, stays selected. Like this:

Apples
Oranges
Pears
Beer
Bananas

I can find no way to code "select this item in this dialog in this list".

I realise I could have simply asked: "Is there a way (using code) to select an item in a list box without using the mouse" but I thought some context may be useful / interesting.

Any tips appreciated.

Thanks,

P.
Last edited by Phil Pendlebury on Sun Nov 04, 2018 9:26 pm, edited 1 time in total.
Phil Pendlebury - Linktree

User avatar
Grovkillen
Automation Wizard
Posts: 1009
Joined: Fri Aug 10, 2012 2:38 pm
Location: Bräcke, Sweden
Contact:

Re: Select an item in ListBox

Post by Grovkillen » Sun Nov 04, 2018 8:17 pm

You know the first list selection number so you just subtract (or add of you move down) one from that and you have the new selection value?
Let>ME=%Script%

Running: 15.0.24
version history

User avatar
Phil Pendlebury
Automation Wizard
Posts: 538
Joined: Tue Jan 16, 2007 9:00 am
Contact:

Re: Select an item in ListBox

Post by Phil Pendlebury » Sun Nov 04, 2018 9:21 pm

Thanks but I think you misread my question:

I already finished work on the code, which is fine. (It is actually not quite so simple as just adding or removing a number the item has to be swapped. But anyway that is working fine.)

The question (simplified) was: How to make an item selected without using the mouse.
Phil Pendlebury - Linktree

hagchr
Automation Wizard
Posts: 327
Joined: Mon Jul 05, 2010 7:53 am
Location: Stockholm, Sweden

Re: Select an item in ListBox

Post by hagchr » Mon Nov 05, 2018 8:43 am

Have you tried using:

Code: Select all

SetDialogProperty>Dialog1,MSListBox1,SelectedIndex,0
with the last item (here 0) picking item to be selected, 0 for the first item, 1 for the second aso.

User avatar
Phil Pendlebury
Automation Wizard
Posts: 538
Joined: Tue Jan 16, 2007 9:00 am
Contact:

Re: Select an item in ListBox

Post by Phil Pendlebury » Mon Nov 05, 2018 10:36 am

Yes that was the first thing I tried, I doesn't work.
Phil Pendlebury - Linktree

hagchr
Automation Wizard
Posts: 327
Joined: Mon Jul 05, 2010 7:53 am
Location: Stockholm, Sweden

Re: Select an item in ListBox

Post by hagchr » Mon Nov 05, 2018 10:42 am

Hi, this works for me, maybe I misunderstood the question.

Code: Select all

Dialog>Dialog1
object Dialog1: TForm
  Left = 247
  Top = 97
  HelpContext = 5000
  BorderIcons = [biSystemMenu]
  Caption = 'CustomDialog'
  ClientHeight = 211
  ClientWidth = 476
  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 = 154
    Top = 38
    Width = 121
    Height = 97
    ItemHeight = 13
    Items.Strings = (
      'Apple'
      'Banana'
      'Pear')
    TabOrder = 0
    Text = 'Apple'#13#10'Banana'#13#10'Pear'#13#10
    SelectedIndex = -1
  end
end
EndDialog>Dialog1

Show>Dialog1

SetDialogProperty>Dialog1,MSListBox1,SelectedIndex,0
Wait>1
SetDialogProperty>Dialog1,MSListBox1,SelectedIndex,1
Wait>1
SetDialogProperty>Dialog1,MSListBox1,SelectedIndex,2
Wait>1

User avatar
Phil Pendlebury
Automation Wizard
Posts: 538
Joined: Tue Jan 16, 2007 9:00 am
Contact:

Re: Select an item in ListBox

Post by Phil Pendlebury » Mon Nov 05, 2018 11:11 am

Hahah thanks man, I had a typo in my code. (Wrong number in the dialog).
My one and only ListBox is labelled as MSListBox2, I was using MSListBox1 (yes it was late at night).

SelectedIndex is indeed what was needed.

Great stuff. Thanks again:-)
Phil Pendlebury - Linktree

User avatar
Phil Pendlebury
Automation Wizard
Posts: 538
Joined: Tue Jan 16, 2007 9:00 am
Contact:

Re: Select an item in ListBox

Post by Phil Pendlebury » Sat Nov 17, 2018 8:55 am

Just for future ref:

Code: Select all

SetDialogProperty>Dialog1,MSListBox2,SelectedIndex,%xselectx%
Selects the item index %xselectx% in Dialog1 MSListBox2
Phil Pendlebury - Linktree

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