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 

Reseting component properties

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





PostPosted: Mon Mar 26, 2007 12:24 am    Post subject: Reseting component properties Reply with quote



Hello,

To simplify the action of a Cancel button in a form wich modifies an object
inherited from TComponent I use the following strategy.

__fastcall TFormA::TFormA(TComponent *Owner, TMyComponent *pMyComponent) :
TForm(Owner)
{
myComponent=pMyComponent;
}

void __fastcall TFormA::FormShow(TObject *Sender)
{
//Update form controls from myComponent properties
myComponent->Backup();
}

void __fastcall TFormA::btCancelClick(TObject *Sender)
{
myComponent->Restore();
Close();
}

void __fastcall TFormA::btOkClick(TObject *Sender)
{
//myComponent properties updated on the fly, so nothing to do here
Close();
}

void __fastcall TMyComponent::Backup()
{
memStream->Seek(0,soFromBeginning);
memStream->WriteComponent(this);
}

void __fastcall TMyComponent::Restore()
{
memStream->Seek(0,soFromBeginning);
memStream->ReadComponent(this);
}

What i'm doing is making a backup of the component properties into a memory
stream before anything is changed in the dialog and if the user decides to
discard the changes, then I restore the original state of the component
reading the component from the stream.
The problem is that it only works if the properties are different to the
default values after saving the component in the stream. That is, the first
time the form is shown, lets say myProperty shows the default value of 0.
The user changes it to 1 (myComponent->myProperty=1) in some part of the
form, but then decides to cancel it pressing Cancel button. This will call
myComponent->Restore() to get back to the initial values. As at the begining
myProperty had the default value, it wasn't saved to the stream because it
had the default value, and when the component is read from the stream,
myProperty is not modified because it wan't saved to the stream and
therefore not readed.

Is there any way to force writing the property to a stream? I tried nodefaul
modifier but does not work. Setting the modifier default to a value probably
will work but it is not very elegant and can introduce coding errors.

I know there are plenty of different approaches to the "Cancel behaviour"
but my approach is very useful (if it works, of course) because I have
plenty of TMyComponent derived classes and this simplifies a lot the
development.

Thanks

Josep
Back to top
Remy Lebeau (TeamB)
Guest





PostPosted: Mon Mar 26, 2007 4:12 am    Post subject: Re: Reseting component properties Reply with quote



"Josep Seto" <remove-jseto (AT) vodafone (DOT) es> wrote in message
news:4606cc9e (AT) newsgroups (DOT) borland.com...

Quote:
What i'm doing is making a backup of the component properties into
a memory stream before anything is changed in the dialog and if the
user decides to discard the changes, then I restore the original
state
of the component reading the component from the stream.

Alternatively, you could simply create a second instance of the
original component, modify that copy as needed, and then assign its
values back to the original component only if accepted by the user.

Quote:
The problem is that it only works if the properties are different
to the default values after saving the component in the stream.

That is what happens whn you use the DFM streaming system like you
are. It is specifically designed to only stream non-default values.

Quote:
Is there any way to force writing the property to a stream?

Only if you are writing the component(s) in question. You can specify
"stored=true" in the property declaration.

If you do not have access to change the property declarations, then
you will have to use the component RTTI to manually loop through and
write each property to the stream instead. You can then write the
current values without regard to their default values. You would
essentially be re-writing the whole DFM streaming system, though.


Gambit
Back to top
Horst Reichert
Guest





PostPosted: Mon Mar 26, 2007 8:11 am    Post subject: Re: Reseting component properties Reply with quote



Quote:
Is there any way to force writing the property to a stream?

Only if you are writing the component(s) in question. You can specify
"stored=true" in the property declaration.
Even this will not work with floating point and string properties.


Regards Horst
Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (VCL Components Development) 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.