dynamic_cast<TRichEdit->Font...> for all available TRichEdits...?

dynamic_cast<TRichEdit->Font...> for all available TRichEdits...?

Post by Oren Halva » Sun, 29 Feb 2004 09:49:40


hi dear builders,

I'm storing Font attributes of a TRichEdit in the registry...
now..for reading and setting the font attributes I use
the below function...

my question now is,I got 20 forms with over 35 TRichEdits..
how can I cast them all to use the same font attributes ...??

how do I use: dynamic_cast<> exactly for this...?

Oren

/*************************************************/
void Set_MemoFont()
{
/*
something like....

TRichEdit *re = dynamic_cast<TRichEdit*>(TRichEdit1->Font->Size);
if(re)...

*/

TRegistry* reg = new TRegistry;
try
{
reg->OpenKey(RegistryKey, true);

if(reg->ValueExists("Font_Name")) {TRichEdit1->Font->Name =
reg->ReadString("Font_Name");}
if(reg->ValueExists("Font_Size")) {TRichEdit1->Font->Size =
StrToInt(reg->ReadString("Font_Size"));}

reg->CloseKey();
}

__finally
{
delete reg;
}
}
 
 
 

dynamic_cast<TRichEdit->Font...> for all available TRichEdits...?

Post by Team » Sun, 29 Feb 2004 11:22:32

"Oren Halvani" < XXXX@XXXXX.COM > wrote in message
news: XXXX@XXXXX.COM ...


You are going about it the wrong way. See further below for an explanation.


You don't, because that is not the correct way to approach the issue in the
first place.


The Font->Size is not a TRichEdit to begin with. You cannot cast it to one,
or vice versa.

You would be better off just moving your Registry code into a separate
function of its own that takes a TFont* as a parameter. For example:

const AnsiString MyFontsKey = "\\Software\\MyKey\\Fonts\\";

bool __fastcall SaveFontToRegistry(const AnsiString &Name, TFont *Font)
{
TRegistry* reg = NULL;
bool Success = false;
try
{
reg = new TRegistry;
try
{
reg->RootKey = whatever;
if( reg->OpenKey(MyFontsKey + "\\" + Name, true) )
{
reg->WriteString("Font_Name", Font->Name);
reg->WriteInteger("Font_Size", Font->Size);
//...
reg->CloseKey();
Success = true;
}
}
__finally {
delete reg;
}
}
catch(const Exception &) {}
return Success;
}

bool __fastcall ReadFontFromRegistry(const AnsiString &Name, TFont
*Font)
{
TRegistry* reg = NULL;
bool Success = false;
try
{
reg = new TRegistry;
try
{
reg->RootKey = whatever;
if( reg->OpenKeyReadOnly(MyFontsKey + "\\" + Name) )
{
Font->Name = reg->ReadString("Font_Name");
Font->Size = reg->ReadInteger("Font_Size");
//...
reg->CloseKey();
Success = true;
}
}
__finally {
delete reg;
}
}
catch(const Exception &) {}
return Success;
}

Then you can read/write your RichEdits as needed, ie:

for(int x = 0; x < Screen->Forms->Count; ++x)
{
TForm *pForm = Screen->Forms[x];
for(int y = 0; y < pForm->ComponentCount; ++y)
{
TRichEdit *pRichEdit =
dynamic_cast<TRichEdit*>(pForm->Components[x]);
if( pRichEdit )
{
if( !SaveFontToRegistry(pForm->Name + "|" + pRichEdit->Name,
pRichEdit->Font) )
// error saving the Font...
}
}
}

//...

for(int x = 0; x < Screen->Forms->Count; ++x)
{
TForm *pForm = Screen->Forms[x];
for(int y = 0; y < pForm->ComponentCount; ++y)
{
TRichEdit *pRichEdit =
dynamic_cast<TRichEdit*>(pForm->Components[x]);
if( pRichEdit )
{
if( !ReadFontFromRegistry(pForm->Name + "|" +
pRichEdit->Name, pRichEdit->Font) )
// error reading the Font...
}
}
}


Gambit



 
 
 

dynamic_cast<TRichEdit->Font...> for all available TRichEdits...?

Post by Oren Halva » Tue, 02 Mar 2004 04:19:32

thank you very much Remy !!

Oren