Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
TIWListbox how to get Selected Values (Priority support)
#1
I am using Intraweb 15.2.69 with Rad Studio 10.2.3
How to get all the values or text selected in a TIWListBox when MultiSelect option is true?
Because TIWListBox->SelectedValue and TIWListBox->SelectedText are both UnicodeString instead of TStringList!
So only one value is returned instead of a list even when multiple values are selected at the same time!
Reply
#2
I am supposed to be a priority user!
Why I have no response up to today?
Reply
#3
SelectedValue and SelectedText are utility methods to retrieve a single selected item in all TIWCustomListCombo descendants (which include all IWComboBoxes and ListBoxes)

In order to retrieve a list with the selected items you can do (in C++):


Code:
void ListBoxGetSelectedValues(TIWListBox* listBox, TStringList* selectedValues)
{
    if (listBox->MultiSelect)
    {
        for (int i = 0; i < listBox->Items->Count; i++)
        {
            if (listBox->Items->Selected[i])
            {
                if (listBox->ItemsHaveValues)
                {
                    selectedValues->Add(listBox->Items->ValueFromIndex[i]);
                }
                else
                {
                    selectedValues->Add(listBox->Items->Strings[i]);
                }
            }
        }
    }
    else
    {
        selectedValues->Add(listBox->GetSelectedValue());
    }
}
Reply
#4
We are going to extend the TIWCustomListCombo.GetSelectedValue and GetSelectedText to include all elements in a single string (comma delimited) as it works with the TIWSelect control. This modification will be included in the next update
Reply
#5
Thanks
But why not to have the TIWListBox return a TStringList instead of a UnicodeString?
A TStringList will make it easier to retrieve the values.
At the contrary of a UnicodeString for which I will need to write a code to separate the values from the commas.
Reply
#6
>>A TStringList will make it easier to retrieve the values.
>>At the contrary of a UnicodeString for which I will need 
>>to write a code to separate the values from the commas.

TStringList contains the property DelimitedText.  Assign DelimitedText a string of comma (or any other delimiter) separated values to populate the TStringList, no need to write code to separate the values:

Code:
  auto_ptr<TStringList> s(new TStringList);
  s->DelimitedText = "1,2,3";
Reply
#7
I know!
I just wanted to avoid to manipulate this kind of staffs like additional variables, pointers specially when working with threads!
If the TStringList is a property of the component, it automatically goes with it!
Reply
#8
If I create a TStringList inside the method and return a instance to the calling code you will also have to deal with its destruction and call it inside a try..finally block which is basically an anti-pattern considering this specific scenario.

It is a much better and clean code if the calling code creates the TStringList and takes care of its destruction.
Reply
#9
(04-07-2023, 12:00 AM)Alexandre Machado Wrote: We are going to extend the TIWCustomListCombo.GetSelectedValue and GetSelectedText to include all elements in a single string (comma delimited) as it works with the TIWSelect control. This modification will be included in the next update

Is there a condition comma would be in the items?  Maybe a non printable, or definable, delimiter?

I don't have this problem, just looking considering future issues.

Dan
Reply
#10
If comma is in the items, that item will be enclosed in double quotes. Something like what TStringList does
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)