Hi Everyone,
I need some help in saving a file in the rich text edit control i have. Basically i have one button and richedit control on a form and what i need is the contents of the entire rich edit control to be passed to the saveas common dialog to be saved onto thee disk. One more thing to note is that my rich edit also contains pictures and text. Can anyone tell me how this is done or show me the codings of how this is done. So far this all that i have gotten so far.

Void CDialogsDlg::onSaveAs ()
{
CFileDialog m_ldFile(false);
if(m_ldFile.DoModal = = IDOK)
{
//This is the part i do not know how to save the entire contents
// of the rich edit
}
}

Someone please help me . My e-mail is <<snip>>

Thank You

Yours Sincerely

Richard West

Recommended Answers

All 3 Replies

Introduction
---------------
The Visual Component Library (VCL) provides various built-in classes to perform file processing. Most of the features are provided through the TFileStream class. To perform file streaming using this class, first declare its instance using its constructor whose syntax is:

__fastcall TFileStream(const AnsiString FileName, Word Mode);

The first argument of this constructor is the name (or path) of the file you are dealing with. If the file does not exist or it cannot be accessed (opened or saved) for any reason, the compiler would throw an error and stop the action.

The second action, Mode, specifies what you are trying to do with the file.

The TFileStream class is conceptually designed to deal with the contents of one or more controls. Therefore, instead of concerning yourself with the values of controls, TFileStream would consider the change that affect a control on behalf of the user, which mostly is its contents. When saving a file, TFileStream faithfully gets the contents of all controls as you wish and saves them in one file. If opening a file, TFileStream locates the content of each file and restores it. Based on this, TFileStream is appropriate for VCL objects and usually its files should not mixed with non-VCL controls.

Saving Controls Contents


In order to save the contents of controls, first declare a pointer to TFileStream using its constructor and specify where the file would be saved. You can specify this path directly in the constructor if you know exactly where the file should be located. This can be done if you are writing a program that stores the default file at a specific location and you know where the file should be saved. Otherwise, you can use the SaveDialog control to let the user specify where to save the file.

When saving a file, the Mode argument of the constructor can be passed as one of the following constants:


Write Mode Description
fmCreate If the user is saving a new file, this is used to save it as new
fmOpenWrite This is used to save a file as new. If the file was previously saved and reopened, this mode would erase its previous contents and fill it with the new data
fmOpenReadWrite If the file already existed, this can be used to save a file that has been modified. Otherwise it can be used to create a new file

When it is possible that other people or application would try accessing the same file at the same time, the following constants can be used to manage the sharing of files:

Share Mode Description
fmShareExclusive Only the current application can access the file
fmShareDenyWrite The file can be opened by other applications but they cannot modify its contents
fmShareDenyRead The file can be modified by other applications but they cannot open it
fmShareDenyNone There is no restriction on what the other applications can do with the current file


To combine these two mode for the Mode argument, you use the bitwise OR operator |

Imagine you create a form equipped with a Memo and an Edit controls:

Here is an example of saving the contents of both controls to a file:


//---------------------------------------------------------------------------

void __fastcall TForm1::btnSaveClick(TObject *Sender)
{
    // Decalre a pointer to TFileStream
    TFileStream *FStream;

    // Let the user call the Save Dialog
    if( SaveDialog1->Execute() )
    {
        // Use the constructor of the TFileStream to create a file
        try {
            FStream = new TFileStream(SaveDialog1->FileName, fmCreate);
            // In the pointer to FStream, add the contents of the Memo
            FStream->WriteComponent(Memo1);
            // and the content of the Edit controls
            FStream->WriteComponent(Edit1);
        }
        __finally
        {
            // Since the pointer was created, delete it,
            // whether it was used or not
            delete FStream;
        }
    }
}

//---------------------------------------------------------------------------
Loading Controls Contents


When saving the contents of controls using TFileStream, the file is arranged so the class can locate data for each object. Based on this, you can use TFileStream to open a file that was created with this class. To do this, once again, declare a pointer to TFileStream and initialize the file using the constructor. If you already know where the file is located, you can simply provide it to the constructor. Otherwise, you can use the Open dialog box and let the user select the file.

When opening a file, you can use one of the following modes as the Mode argument:


Read Mode Description
fmOpenRead This is used to open a file but the user cannot modify and then save it.
fmOpenReadWrite This allows opening an existing file, modifying, and saving it.

You can combine this mode with one of the above share modes using the bitwise OR operator. Here is an example from the same above form design:


//---------------------------------------------------------------------------

void __fastcall TForm1::btnOpenClick(TObject *Sender)
{
    TFileStream *FStream;

    if( OpenDialog1->Execute() )
    {
        try {
            FStream = new TFileStream(OpenDialog1->FileName,
                                      fmOpenRead | fmShareExclusive);
            FStream->ReadComponent(Memo1);
            FStream->ReadComponent(Edit1);
        }
        __finally
        {
            delete FStream;
        }
    }
}

//---------------------------------------------------------------------------

Hi eveyone,
i do not want to use the file stream methods but i want to use microsoft's common dialogs to save my files.

Thank You

Richard West

Everyone is familar is the Common Dialog - File Open/Save control in Windows, you use it everyday. This script shows you how to script the Common Dialog control and put it to work for you.
--------------------------------------------------------------------------------------
Note: This script will run on system's with the Microsoft Controls properly installed. If your machine has virtually any of Microsoft's development tools installed, you probably have these controls. I have yet to find an easy way to distribute these, I am open to suggestions.

// Create the Objectcd = new ActiveXObject("MSComDlg.CommonDialog");
// Set file filtercd.Filter = "All Files(*.*)|*.*|JScript Files(*.js)|*.js";cd.FilterIndex = 2;
// Must set MaxFileSize. Otherwise you will get an errorcd.MaxFileSize = 128;
// Show it to usercd.ShowOpen();
// Retrieve file + pathfile = cd.FileName;
// If the user does enter file exitif ( !file ) 
{   WScript.Echo("You must enter a file name");  
 WScript.Quit(0);
} 
else 
{  
 WScript.Echo("The user selected:\n" + file );
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.