Quantcast
Channel: Programmers Heaven Forums RSS Feed
Viewing all articles
Browse latest Browse all 2703

Loading/Saving Form state to file C++ Builder XE2

$
0
0
I hope someone can help me, I am currently working on a personal project where I need to save the position and content of dynamically created Images and Memos to a file, so that I can reload them when I restart the application.

I have been getting by creating an output file for each Image and Memo, along with a secondary file holding the relative information, such as the name, position, height, width, etc. and reading it back at the start of the application.

I tried porting code from Delphi to save the entire form as well as all components to a single file with the following test code.

This code works perfectly to save the form and it's settings to a file.

//Save the Form to a file
if(SaveDialog1->Execute())
{
TFileStream * FileStream;
TMemoryStream * MemStream;
MemStream = NULL;
FileStream = new TFileStream(SaveDialog1->FileName,fmCreate);
MemStream = new TMemoryStream;
MemStream->WriteComponent(dynamic_cast<TForm2 *>(ActiveMDIChild));
MemStream->Position = 0;
ObjectBinaryToText(MemStream, FileStream);
MemStream->Free();
FileStream->Free();
}

The problem I have is when I use the following code to create a new form with the information, it works perfectly if the saved form only contains TImage components, but, if I try to load a TMemo component, then the form is blank and I am not receiving an error. I have literally searched for over a week for an answer but, so far I have failed.


if(OpenDialog1->Execute())
{
TForm2 * Form2 = new TForm2(Application);
TFileStream * FileStream;
TMemoryStream * MemStream;
MemStream = NULL;
FileStream = new TFileStream(OpenDialog1->FileName,fmOpenRead);
MemStream = new TMemoryStream;
ObjectTextToBinary(FileStream, MemStream);
MemStream->Position = 0;
MemStream->ReadComponent(Form2);
Application->InsertComponent(Form2);
MemStream->Free();
FileStream->Free();
Form2->Show();
}

Keep in mind, I am not employed as a programmer, this is just a hobby for me, so my terminology may be off. Also, If you need anymore information to help me, let me know.

Viewing all articles
Browse latest Browse all 2703

Trending Articles