BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

How to Exchange values in a LISTBOX

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (VCL Components Usage)
View previous topic :: View next topic  
Author Message
Cenk
Guest





PostPosted: Thu Apr 06, 2006 3:03 pm    Post subject: How to Exchange values in a LISTBOX Reply with quote



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





PostPosted: Thu Apr 06, 2006 3:03 pm    Post subject: Re: How to Exchange values in a LISTBOX Reply with quote



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





PostPosted: Thu Apr 06, 2006 5:03 pm    Post subject: Re: How to Exchange values in a LISTBOX Reply with quote



"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





PostPosted: Thu Apr 06, 2006 7:03 pm    Post subject: Re: How to Exchange values in a LISTBOX Reply with quote

"JD" <nospam (AT) nospam (DOT) com> wrote:
Quote:

int First, Last;

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
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (VCL Components Usage) All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.