Editor for WinForms | ComponentOne
Creating an XHTML Editor in Code / The ToolStripMain class / File Commands
In This Topic
    File Commands
    In This Topic

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

    The New command is implemented as follows:

    To write code in C#

    C#
    Copy Code
    bool NewDocument()
    {
        if (OKToDiscardChanges())
        {
            Editor.Document = new System.Xml.XmlDocument();
            _fileName = null;
            SetDirty(false);
            return true;
        }
        return false;
    }
    

    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 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 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:

    To write code in C#

    C#
    Copy Code
    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;
    }
    

    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 method to load the document into the editor.

    The Save method is implemented in a similar way:

    To write code in C#

    C#
    Copy Code
    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;
        }
    }
    

    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 method to save the current document to the file.