Howto: Work With Files

This chapter shows you how to load and save files with TX Text Control.

The source code for this example is contained in the following directories:

  • %USERPROFILE%\My Documents\TX Text Control 29.0.NET for Windows Forms\Samples\WinForms\VB.NET\Files
  • %USERPROFILE%\My Documents\TX Text Control 29.0.NET for Windows Forms\Samples\WinForms\CSharp\Files

Used TX Text Control controls:

  • TXTextControl.TextControl
  • TXTextControl.ButtonBar
  • TXTextControl.RulerBar
  • TXTextControl.StatusBar

Relevant API Links

Using the Built-in Dialog Boxes

The easiest way to load and save files with Text Control is to use the Load and Save methods without a parameter, as shown in Creating a Simple Word Processor. This way, only a single line of code is required to open a file dialog box, fill its Files of type: combo box with supported file formats, and finally load and display a file the user has selected:

Image

You may have noticed, though, that Load and Save actually consist of 11 overloaded functions each, and thus enable you to customize TX Text Control's file handling for various types of application. You can, for instance, load data from a string instead of a file, or provide your own customized dialog box. The next sections shows you some of the options available.

Restricting File Formats

By default, the Load and Save methods will display all file formats Text Control supports. If you wish to support a subset of these formats, or if you have a version of Text Control that does not support all of the formats, you can specify the formats to be displayed as a parameter. Several members of Text Control's StreamType enumeration can be combined to, for instance, only support HTML and RTF:

Image

private void mnuFile_OpenFile_Click(object sender,
                                      System.EventArgs e)
{
    textControl1.Load(TXTextControl.StreamType.HTMLFormat |
                        TXTextControl.StreamType.RichTextFormat);
}
Private Sub mnuFile_OpenFile_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles mnuFile_OpenFile.Click
    TextControl1.Load(TXTextControl.StreamType.HTMLFormat + _
                        TXTextControl.StreamType.RichTextFormat)
End Sub

Displaying The File Name

It is convenient for the user to have the name of the file he or she is currently working with displayed as a window caption. One would normally use the FileName property of the file dialog box for this, but, as the dialog box is called from within Text Control, this option is not available.

This code sample shows how to get the filename by creating and passing a LoadSettings object as a parameter to the Load method.

private void mnuFile_OpenFile_Click(object sender, System.EventArgs e)
{
    TXTextControl.LoadSettings LoadSettings = new TXTextControl.LoadSettings();
    textControl1.Load(TXTextControl.StreamType.All, LoadSettings);
    this.Text = LoadSettings.LoadedFile;
}
Private Sub mnuFile_OpenFile_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles mnuFile_OpenFile.Click
    Dim LoadSettings As New TXTextControl.LoadSettings()
    TextControl1.Load(TXTextControl.StreamType.All, LoadSettings)
    Me.Text = LoadSettings.LoadedFile
End Sub

Save and Save As...

Word processing programs typically have seperate menu items for Save... and Save As.. operations. Unless a file has been created from scratch, it can be saved with its previously assigned filename and type by just hitting Save, and without a dialog box.

The sample program maintains two variables called DocumentFileName and DocumentStreamType in which it stores the name and type of the current file. The information is updated when a file is loaded, and when it is stored under a different name.

When the user clicks Save, the file is stored using this information. A dialog box is only displayed if the file has been created from scratch, i.e. if it does not yet have a name.

private void mnuFile_Save_Click(object sender, System.EventArgs e)
{
    TXTextControl.SaveSettings SaveSettings = new TXTextControl.SaveSettings();
    if (DocumentFileName != "" )
    {
        // save under same name and type
        textControl1.Save(DocumentFileName, DocumentStreamType);
    }
    else
    {
        // save as..
        textControl1.Save(TXTextControl.StreamType.All, SaveSettings);
        DocumentFileName = SaveSettings.SavedFile;
        DocumentStreamType = SaveSettings.SavedStreamType;
    }
}
Private Sub mnuFile_Save_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles mnuFile_Save.Click
    Dim SaveSettings As New TXTextControl.SaveSettings()
    If DocumentFileName <> "" Then
        ' save under same name and type
        TextControl1.Save(DocumentFileName, DocumentStreamType)
    Else
        ' save as..
        TextControl1.Save(TXTextControl.StreamType.All, SaveSettings)
        DocumentFileName = SaveSettings.SavedFile
        DocumentStreamType = SaveSettings.SavedStreamType
    End If
End Sub

Export to PDF and CSS

PDF and CSS formats require special attention, as they can be saved to, but not loaded from. The problem is that if you save a file as PDF, you cannot load it back in for further editing. CSS will not even save the text contained in a document, but only the formatting.

The best way to circumvent this is to create a seperate Export... menu item in addition to Save and Save As..., and keep the document in its original format when exporting it to a non-editable format. Note that, unlike Save As..., Export... does not change the file name and type.

private void mnuFile_Export_Click(object sender, System.EventArgs e)
{
    textControl1.Save(TXTextControl.StreamType.AdobePDF);
}
Private Sub mnuFile_Export_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles mnuFile_Export.Click
    TextControl1.Save(TXTextControl.StreamType.AdobePDF)
End Sub