jonalodev
jonalodev

Reputation: 89

C# Windows Forms: OpenFileDialog Strange Issues

This is kinda strange, let me try to explain it as best as possible:

When I create a new file and Save it, it saves correctly (test.xml). When I make changes to this file and Save it, it saves correctly (to test.xml) When I make changes again to this file or just choose Save As, it works correctly (newtest.xml)

However, when I do a file open, make changes to a file (test.xml) and click Save it is saving to (newtest.xml).

This is in my MainForm.cs

            if (this.openEditorDialog1.ShowDialog(this) == DialogResult.OK && editForm != null)
        {


            editForm.Close();                
            editForm = new EditorForm(this);
            editForm.OpenFile(this.openEditorDialog1.FileName);
            editForm.Closing += new CancelEventHandler(EditorForm_Closing);
            editForm.MdiParent = this;
            editForm.Show();
        }

private void biFileSave_Click(object sender, EventArgs e)
{
if (!editForm.HasFileName)
            {
                if (this.saveEditorDialog1.ShowDialog(this) == DialogResult.OK)
                {
                    this.ActiveDiagram.SaveSoap(this.saveEditorDialog1.FileName);
                    editForm.FileName = this.saveEditorDialog1.FileName;
                }
            }
            else
            {
                this.ActiveDiagram.SaveSoap(this.saveEditorDialog1.FileName);
            }

This is in my EditorForm.cs

  public void OpenFile(string strFileName)
    {

        diagramComponent.LoadSoap(mainForm.openEditorDialog1.FileName);
        this.FileName = mainForm.openEditorDialog1.FileName;
        this.tabControl1.SelectedTab = DiagramTab;

    }

I'm sure it has to do with the what I'm doing in the EditoForm but I can't seem to figure it out.

Upvotes: 1

Views: 537

Answers (2)

Windows programmer
Windows programmer

Reputation: 8065

else
{
    this.ActiveDiagram.SaveSoap(this.saveEditorDialog1.FileName);

It looks like you want:

    this.ActiveDiagram.SaveSoap(editForm.FileName);

Upvotes: 2

agent-j
agent-j

Reputation: 27913

It must have to do with mainForm.openEditorDialog1.FileName. Use a FileName property of the form that does the saving. When you open the file, set the fileName to mainForm.openEditorDialog1.FileName. When you SaveAs, set the FileName property there, too. This way, whenever the current file, changes you set the FileName property appropriately. Then, when it comes time to save the file, you always have the correct filename.

In summary, only use the .FileName property of the SaveAs dialog or the FileOpen dialog right after you use them.

Upvotes: 1

Related Questions