This is really frustrating me. I’m new to C Sharp so looking for some assistance. My Save/Save As is totally fubar.
Two questions really:
How do I save changes to an existing file without popping a save dialog? If I click save it pops a dialog which is fine so I save it, then make some changes and click Save again it pops a dialog rather than just saving the file to the name already given.
How do I show the filename rather than the full path in the save as dialog? It shows as File Name: C:\Users\username\desktop\save\filename.xml
This is in MainForm.cs.
private void biFileSave_Click(object sender, EventArgs e)
{
// Save diagram
EditorForm editForm = this.ActiveDiagramForm;
if (editForm != null)
{
if (!editForm.HasFileName)
{
if (this.saveEditorDialog.ShowDialog(this) == DialogResult.OK)
{
this.ActiveDiagram.SaveSoap(this.saveEditorDialog.FileName);
}
}
else
{
this.ActiveDiagram.SaveSoap(this.saveEditorDialog.FileName);
}
}
private void biFileSaveAs_Click(object sender, EventArgs e)
{
// Save As diagram
EditorForm editForm = this.ActiveDiagramForm;
if (editForm != null)
{
if (editForm.HasFileName)
{
this.saveEditorDialog.FileName = editForm.FileName;
}
if (this.saveEditorDialog.ShowDialog(this) == DialogResult.OK)
{
this.ActiveDiagram.SaveSoap(this.saveEditorDialog.FileName);
string strFileName = this.saveEditorDialog.FileName;
}
}
}
This is in EditForm.cs
public string FileName
{
get
{
return this.fileName;
}
set
{
this.fileName = value;
this.Text = Path.GetFileNameWithoutExtension(this.fileName);
}
}
public bool HasFileName
{
get
{
return (this.fileName != null && this.fileName.Length > 0);
}
}
EDIT:
Thank you for helping me on this so quickly! My Save works as expected now, however it introduced a strange issue with Save As (code above).
If I open a file (test.xml) that I have saved, then click Save As (name it test2.xml) it saves to the new file. BUT, when I open that test.xml again and make changes and click Save it saves those changes to the test2.xml. Very strange…any ideas?
Where in code is FileName set? From the sample you’ve posted, I don’t see it being set anywhere, but perhaps it is elsewhere. This may work: