Select an item in ListBox
Moderators: JRL, Dorian (MJT support)
- Phil Pendlebury
- Automation Wizard
- Posts: 543
- Joined: Tue Jan 16, 2007 9:00 am
- Contact:
Select an item in ListBox
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.
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
- Grovkillen
- Automation Wizard
- Posts: 1132
- Joined: Fri Aug 10, 2012 2:38 pm
- Location: Bräcke, Sweden
- Contact:
Re: Select an item in ListBox
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?
- Phil Pendlebury
- Automation Wizard
- Posts: 543
- Joined: Tue Jan 16, 2007 9:00 am
- Contact:
Re: Select an item in ListBox
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.
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
Re: Select an item in ListBox
Have you tried using:
with the last item (here 0) picking item to be selected, 0 for the first item, 1 for the second aso.
Code: Select all
SetDialogProperty>Dialog1,MSListBox1,SelectedIndex,0
- Phil Pendlebury
- Automation Wizard
- Posts: 543
- Joined: Tue Jan 16, 2007 9:00 am
- Contact:
Re: Select an item in ListBox
Yes that was the first thing I tried, I doesn't work.
Phil Pendlebury - Linktree
Re: Select an item in ListBox
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
- Phil Pendlebury
- Automation Wizard
- Posts: 543
- Joined: Tue Jan 16, 2007 9:00 am
- Contact:
Re: Select an item in ListBox
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:-)
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
- Phil Pendlebury
- Automation Wizard
- Posts: 543
- Joined: Tue Jan 16, 2007 9:00 am
- Contact:
Re: Select an item in ListBox
Just for future ref:
Selects the item index %xselectx% in Dialog1 MSListBox2
Code: Select all
SetDialogProperty>Dialog1,MSListBox2,SelectedIndex,%xselectx%
Phil Pendlebury - Linktree