# File Commands

The ToolStripMain class implements the usual New, Open, Save and Save As file commands in C1Editor control.

## Content



The **ToolStripMain** class implements the usual _New_, _Open_, _Save_ and _Save As_ file commands.

The **New** command is implemented as follows:

DOC-DETAILS-TAG-OPEN

DOC-SUMMARY-TAG-OPEN

To write code in C#

DOC-SUMMARY-TAG-CLOSE

```csharp
bool NewDocument()
{
    if (OKToDiscardChanges())
    {
        Editor.Document = new System.Xml.XmlDocument();
        _fileName = null;
        SetDirty(false);
        return true;
    }
    return false;
}
```

DOC-DETAILS-TAG-CLOSE

The method calls a helper **OkToDiscardChanges** method that checks whether the loaded document has any changes. If it does, the method prompts the user to verify that it is OK to discard the changes before creating a new document.

To actually create the new document, the method sets the **C1Editor**'s [Document](/componentone/api/win/online-richtexteditor/dotnet-framework-api/C1.Win.C1Editor.4.8/C1.Win.C1Editor.C1Editor.Document.html) property to a new instance of an **XmlDocument** object. The **XmlDocument** represents the content of the editor, and you may use the **XmlDocument** object model to modify the document.

This is one of the main strengths of the [C1Editor](/componentone/api/win/online-richtexteditor/dotnet-framework-api/C1.Win.C1Editor.4.8/C1.Win.C1Editor.C1Editor.html) control. It allows you to create and modify document content using the powerful and familiar **XmlDocument** object model. This will be demonstrated in more detail in later sections.

The **Open** command is implemented as follows:

DOC-DETAILS-TAG-OPEN

DOC-SUMMARY-TAG-OPEN

To write code in C#

DOC-SUMMARY-TAG-CLOSE

```csharp
bool LoadDocument()
{
    if (OKToDiscardChanges())
    {
        using (OpenFileDialog dlg = new OpenFileDialog())
        {
            // get file name
            dlg.Filter = Properties.Resources.FileFilter;
            dlg.DefaultExt = Properties.Resources.DefaultExt;
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                try
                {
                   // load document
                    Editor.LoadXml(dlg.FileName);
                    _fileName = dlg.FileName;
                    SetDirty(false);
                    return true;
                }
                catch (Exception x)
                {
                    MessageBox.Show(x.Message, "Error", 
                        MessageBoxButtons.OK, 
                        MessageBoxIcon.Error);
                    return false;
                }
            }
        }
    }
    // canceled...
    return false;
}
```

DOC-DETAILS-TAG-CLOSE

As before, the method starts by calling the **OKToDiscardChanges** method. It then uses an **OpenFileDialog** to allow the user to pick the document he wants to load. Finally, the method calls the **C1Editor**'s [LoadXml](/componentone/api/win/online-richtexteditor/dotnet-framework-api/C1.Win.C1Editor.4.8/C1.Win.C1Editor.C1Editor.LoadXml.html) method to load the document into the editor.

The **Save** method is implemented in a similar way:

DOC-DETAILS-TAG-OPEN

DOC-SUMMARY-TAG-OPEN

To write code in C#

DOC-SUMMARY-TAG-CLOSE

```csharp
bool SaveDocument()
{
    // no name? go get one...
    if (string.IsNullOrEmpty(_fileName))
    {
        return SaveDocumentAs();
    }
    // got the name, save the file
    try
    {
        Editor.SaveXml(_fileName);
        SetDirty(false);
        return true;
    }
    catch (Exception x)
    {
        MessageBox.Show(x.Message, "Error", 
            MessageBoxButtons.OK, 
            MessageBoxIcon.Error);
        return false;
    }
}
```

DOC-DETAILS-TAG-CLOSE

The method starts by checking whether the current document already has a file name associated with it. If it does not, then it calls the **SaveDocumentAs** method, which prompts the user for a file name and then returns to the **SaveDocument** method.

Once the document has a file name, the method uses the **C1Editor**'s [SaveXml](/componentone/api/win/online-richtexteditor/dotnet-framework-api/C1.Win.C1Editor.4.8/C1.Win.C1Editor.C1Editor.SaveXml.html) method to save the current document to the file.