 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Cenk Guest
|
Posted: Thu Apr 06, 2006 3:03 pm Post subject: How to Exchange values in a LISTBOX |
|
|
Hi,
i have a ListBox and i wanna change some elements of it.
Eg.
ListBox1 ListBox1
1 1
2 -----> 19
3 3
4 4
5 5 |
|
| Back to top |
|
 |
Vladimir Stefanovic Guest
|
Posted: Thu Apr 06, 2006 3:03 pm Post subject: Re: How to Exchange values in a LISTBOX |
|
|
| Quote: | Eg.
ListBox1 ListBox1
1 1
2 -----> 19
3 3
4 4
5 5
|
// second element
ListBox1->Items->Strings[1] = 19;
--
Best Regards,
Vladimir Stefanovic |
|
| Back to top |
|
 |
JD Guest
|
Posted: Thu Apr 06, 2006 5:03 pm Post subject: Re: How to Exchange values in a LISTBOX |
|
|
"Cenk" <cenk1536 (AT) yahoo (DOT) com> wrote:
| Quote: |
i have a ListBox and i wanna change some elements of it.
|
Your subject says exchange. If that's what you *really* want,
given two indexs into it's Items:
//-------------------------------------------------------------
void __fastcall TForm1::ExchangeItems( TListBox *pBox, int x, int y )
{
if( x < 0 || y < 0 || x > (pBox->Count - 1) || y > (pBox->Count - 1) )
{
throw Exception( "TListBox::Exchange : Index out of bounds." );
}
if( x == y ) return;
int First, Last;
AnsiString S;
TObject *pObject;
if( x < y )
{
First = x;
Last = y;
}
else
{
First = y;
Last = x;
}
::SendMessage( pBox->Handle, WM_SETREDRAW, FALSE, 0 );
S = pBox->Items->Strings[ First ];
pObject = pBox->Items->Objects[ First ];
pBox->Items->Strings[ First ] = pBox->Items->Strings[ Last ];
pBox->Items->Objects[ First ] = pBox->Items->Objects[ Last ];
pBox->Items->Strings[ Last ] = S;
pBox->Items->Objects[ Last ] = pObject;
::SendMessage( pBox->Handle, WM_SETREDRAW, TRUE, 0 );
pBox->Invalidate();
}
//-------------------------------------------------------------
You could also do it with strings and using the Items::IndexOf
method in addition to using the Items::IndexOfObject method as
well.
~ JD |
|
| Back to top |
|
 |
JD Guest
|
Posted: Thu Apr 06, 2006 7:03 pm Post subject: Re: How to Exchange values in a LISTBOX |
|
|
"JD" <nospam (AT) nospam (DOT) com> wrote:
I was thinking of inserting and deleting but this wasn't
needed with a temporary but I didn't remove it from the code.
//-------------------------------------------------------------
void __fastcall TForm1::ExchangeItems( TListBox *pBox, int x, int y )
{
if( x < 0 || y < 0 || x > (pBox->Count - 1) || y > (pBox->Count - 1) )
{
throw Exception( "TListBox::Exchange : Index out of bounds." );
}
else if( x == y ) return;
AnsiString S;
TObject *pObject;
::SendMessage( pBox->Handle, WM_SETREDRAW, FALSE, 0 );
S = pBox->Items->Strings[ x ];
pObject = pBox->Items->Objects[ x ];
pBox->Items->Strings[ x ] = pBox->Items->Strings[ y ];
pBox->Items->Objects[ x ] = pBox->Items->Objects[ y ];
pBox->Items->Strings[ y ] = S;
pBox->Items->Objects[ y ] = pObject;
::SendMessage( pBox->Handle, WM_SETREDRAW, TRUE, 0 );
pBox->Invalidate();
}
//-------------------------------------------------------------
~ JD |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|