Discussion:
List items selected from a ListBox
(too old to reply)
Wayne Smith
2008-07-28 14:04:19 UTC
Permalink
I have allowed MultiSelect on a ListBox. However, I am not able to read all
of the items selected from the ListBox. My code only gives me the last one
selected. Any input would be greatly appreciated.

AnsiString temp = " ''";
for(int x = 0; x <= lbBdx->Items->Count; x++)
{
if(lbBdx->ItemIndex == x)
temp = temp + ", '" + lbBdx->Items->Strings[lbBdx->ItemIndex] + "'";
}

Thank you in advance!!!!!
Wayne
Vladimir Stefanovic
2008-07-28 15:40:54 UTC
Permalink
Post by Wayne Smith
for(int x = 0; x <= lbBdx->Items->Count; x++)
Not <=.
Just <.
Post by Wayne Smith
if(lbBdx->ItemIndex == x)
Not ItemIndex.
There is Selected[] property.
Post by Wayne Smith
temp = temp + ", '" + lbBdx->Items->Strings[lbBdx->ItemIndex] + "'";
Not ItemIndex.
Use loop variable (x).

Try this instead /untested/:

AnsiString temp;
for(int x = 0; x < lbBdx->Items->Count; x++)
{
if ( lbBdx->Selected[x] )
temp = temp + lbBdx->Items->Strings[x] + ";";
}
ShowMessage( temp );

Also, refer the Help for the TListBox::SelCount property, it may be
useful.
--
Best Regards,
Vladimir Stefanovic
Wayne Smith
2008-07-29 15:14:08 UTC
Permalink
Vladimir,
This works great!!!!!

Thank you very much!!!!!!
Wayne
Post by Vladimir Stefanovic
Post by Wayne Smith
for(int x = 0; x <= lbBdx->Items->Count; x++)
Not <=.
Just <.
Post by Wayne Smith
if(lbBdx->ItemIndex == x)
Not ItemIndex.
There is Selected[] property.
Post by Wayne Smith
temp = temp + ", '" + lbBdx->Items->Strings[lbBdx->ItemIndex] + "'";
Not ItemIndex.
Use loop variable (x).
AnsiString temp;
for(int x = 0; x < lbBdx->Items->Count; x++)
{
if ( lbBdx->Selected[x] )
temp = temp + lbBdx->Items->Strings[x] + ";";
}
ShowMessage( temp );
Also, refer the Help for the TListBox::SelCount property, it may be
useful.
--
Best Regards,
Vladimir Stefanovic
autoreject
2008-07-28 16:38:45 UTC
Permalink
Post by Wayne Smith
I have allowed MultiSelect on a ListBox. However, I am not able to read all
of the items selected from the ListBox. My code only gives me the last one
selected. Any input would be greatly appreciated.
AnsiString temp = " ''";
for(int x = 0; x <= lbBdx->Items->Count; x++)
{
if(lbBdx->ItemIndex == x)
temp = temp + ", '" + lbBdx->Items->Strings[lbBdx->ItemIndex] + "'";
}
Thank you in advance!!!!!
Wayne
Maybe something like this:

AnsiString temp = " ''";
int x=0;
while( x<ListBox1->Items->Count)
{
if(ListBox1->Selected[x])
{
temp = temp + ", '" +
ListBox1->Items->Strings[ListBox1->ItemIndex] + "'";
}
x++;
};
--
John Kettle
autoreject
2008-07-28 16:49:26 UTC
Permalink
Post by Wayne Smith
I have allowed MultiSelect on a ListBox. However, I am not able to read all
of the items selected from the ListBox. My code only gives me the last one
selected. Any input would be greatly appreciated.
AnsiString temp = " ''";
for(int x = 0; x <= lbBdx->Items->Count; x++)
{
if(lbBdx->ItemIndex == x)
temp = temp + ", '" + lbBdx->Items->Strings[lbBdx->ItemIndex] + "'";
}
Thank you in advance!!!!!
Wayne
Sorry cut and paste went wrong but see you have solution in anycase
--
John Kettle
Loading...