Jump to content

Setting List Box Selection

Recommended Posts

How do I set the selection of elements in a List Box?

The set up dialog creates a list box with

CreateListBoxN( dialog, kLabelLegendList, 35, 10, TRUE );


I have a button in the dialog that wants to select all the elements of the list box; however,


                    FOR index := 1 TO NumLLs DO
                        SelectChoice(dialog, kLabelLegendList, index, TRUE);
selects nothing in the list box.


                    SetSelectionRange(dialog, kLabelLegendList, 0, NumLLs);

selects nothing in the list box.


I have checked to be sure that I am entering the kSelectAllBtn case in the Case statement that polls items in the dialog handler.



Link to comment


   For starters, the ListBox is 0-based, so to select all items you will need to count from 0 to n-1. 


   SelectChoice() seems to only select one item at a time, so only the item with the last value of index will be selected. In your example, item NumLLs is outside the range of choices. Use NumLLs-1 to select the last item.


    I am not sure how SetSelectionRange() is supposed to work. I get the same results as you. If there is a VS command to set multiple selections, @_c_ is probably your best bet for a quick answer.




Link to comment

I can confirm all that's said so far.


Below the old example from Jeff Geraci adapted: the index is 0-based, SelectChoice selects only the last used index in the loop, unregarded the fact that the List Box accepts USER multiple-selection. It indeed doesn't seem to translate into scripts.


Dev states about List Boxes

Scripted API functions for retrieving and managing pulldown menu control options also work with list box controls. 

So it is entirely possible that since Pull-down menues don't support multiple selection by script, the functionality is missing here too (whereby I did once succeed into having multiple  selection in pull down menus).

Caro Sam, I would definitely file it as bug, it does feel totally wrong.

{ draws a muliple selection list box 25 characters wide and 7 rows high }
CreateListBoxN(2,10,25,7, true);
{ Jeff Geraci, Vlist, Re: List Box, 10 Jun 2005 }
PROCEDURE MultiSelListBox;
    kChooseOK = 1;
    kChooseCancel = 2;
    kListBox = 5;
    kButton = 6;
    dlogID, result : LONGINT;
    selCh, i   : INTEGER;
    sel1Str   : STRING;
    sel2Str   : STRING;
    sel1, sel2  : INTEGER;

        CASE item OF
                AddListBoxTabStop(dlogID, kListBox, 6);
                AddListBoxTabStop(dlogID, kListBox, 13);
                AddChoice(dlogID, kListBox, '1a 2abcd 3-123', 0);
                AddChoice(dlogID, kListBox, '1ab 2abc 3-8653', 1);
                AddChoice(dlogID, kListBox, '1abc 2ab 3-54754', 2);
                AddChoice(dlogID, kListBox, '1abcd 2a 3-5432', 3);
                FOR i:= 0 TO 3 DO
                    SelectChoice(dlogID, kListBox, i, true);
                { SetSelectionRange(dlogID, kListBox, 0, 3); }
                { doesn't work }
                { Get first two selections }
                GetSelectedChoiceInfo(dlogID, kListBox, 0, sel1, sel1Str);
                GetSelectedChoiceInfo(dlogID, kListBox, sel1+1, sel2, sel2Str);

    dlogID := CreateLayout('List Box', false, 'OK', 'Cancel');
    CreateListBoxN(dlogID, kListBox, 40, 10, true);
    SetFirstLayoutItem(dlogID, kListBox);
    result := RunLayoutDialog(dlogID, DialogProc);
    Message('Selections: ', sel1, ' & ', sel2);
The last parameter of CreateListBoxN indicates whether the list box
should be a multi-selection list box.  A list box cannot be changed from
single-sel to multi-sel (or vice-versa) at runtime.  To retrieve the
values, use GetSelectedChoiceInfo in a loop.
Pass 0 for atChoice to retrieve the first selection, and choiceNumber
will contain the index of that item. To retrieve the next one, pass
choiceNumber + 1 for atChoice. Repeat this process until -1 is returned
for choiceNumber. Also, if no items are selected in the list box, -1
will be returned for choiceNumber.
Jeff Geraci
Nemetschek North America



Edited by _c_
Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...