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.

[C#]
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;
    }
}
[Visual Basic]
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
[Delphi]
procedure Form1.mnuFile_Save_Click(sender: System.Object;
    e: System.EventArgs);
var
    SaveSettings : TXTextControl.SaveSettings;

begin
    SaveSettings := TXTextControl.SaveSettings.Create;
    if DocumentFileName <> '' then begin
        // save under same name and type
        TextControl1.Save(DocumentFileName, DocumentStreamType);
    end
    else begin
        // save as..
        TextControl1.Save(TXTextControl.StreamType.All, SaveSettings);
        DocumentFileName := SaveSettings.SavedFile;
        DocumentStreamType := SaveSettings.SavedStreamType;
        Self.Text := DocumentFileName;
    end;
end;

<< Previous Next >>