Step 6 of 9: Implement the File Support Methods
The simple designer has three commands that support files: New, Open, and Save. NewFile clears the class variables, report list, preview and designer controls, and then updates the UI.
Add the following code to implement the NewFile method:
Private Sub NewFile()
_fileName = ""
_dirty = False
_list.Items.Clear()
_c1ppv.Document = Nothing
_c1rd.Report = Nothing
UpdateUI()
End Sub
• C#
private void NewFile()
{
_fileName = "";
_dirty = false;
_list.Items.Clear();
_c1ppv.Document = null;
_c1rd.Report = null;
UpdateUI();
}
OpenFile prompts the user for a report definition file to open, then uses the C1Report component to retrieve a list of report names in the selected file. Each report is loaded into a new C1Report component, which is added to the report list (_list control).
Instead of adding the C1Report components directly to the list box, the code uses a ReportHolder wrapper class. The only function of the ReportHolder class is to override the ToString method so the list box shows the report names.
Add the following code to implement the OpenFile method:
Public Sub OpenFile()
' get name of file to open
Dim dlg As New OpenFileDialog
dlg.FileName = "*.xml"
dlg.Title = "Open Report Definition File"
If dlg.ShowDialog() <> DialogResult.OK Then
Return
End If
' check selected file
Try
reports = _c1r.GetReportInfo(dlg.FileName)
Catch
If IsNothing(reports) OrElse reports.Length = 0 Then
MessageBox.Show("Invalid (or empty) report definition file")
Return
End If
End Try
' clear list
NewFile()
' load new file
Cursor = Cursors.WaitCursor
_fileName = dlg.FileName
Dim reportName As String
For Each reportName In reports
Dim rpt As New C1Report()
rpt.Load(_fileName, reportName)
_list.Items.Add(New ReportHolder(rpt))
Next
Cursor = Cursors.Default
' select first report
_list.SelectedIndex = 0
End Sub
' ReportHolder
' Helper class used to store reports in listboxes. The main thing
' it does is override the ToString() method to render the report name.
Public Class ReportHolder
Public Sub New(ByVal report As C1Report)
Me.Report = report
End Sub
Public Overrides Function ToString() As String
Dim text As String = Me.Report.ReportName
If text.Length = 0 Then text = "Unnamed Report"
Return text
End Function
Public ReadOnly Report As C1Report
End Class
• C#
public void OpenFile()
{
// get name of file to open
OpenFileDialog dlg = new OpenFileDialog();
dlg.FileName = "*.xml";
dlg.Title = "Open Report Definition File";
if (dlg.ShowDialog() != DialogResult.OK)
return;
// check selected file
string[] reports = null;
try
{
reports = _c1r.GetReportInfo(dlg.FileName);
}
catch {}
if (reports == null || reports.Length == 0)
{
MessageBox.Show("Invalid (or empty) report definition file");
return;
}
// clear list
NewFile();
// load new file
Cursor = Cursors.WaitCursor;
_fileName = dlg.FileName;
foreach (string reportName in reports)
{
C1Report rpt = new C1Report();
rpt.Load(_fileName, reportName);
_list.Items.Add(new ReportHolder(rpt));
}
Cursor = Cursors.Default;
// select first report
_list.SelectedIndex = 0;
}
// ReportHolder
// Helper class used to store reports in listboxes. The main thing
// it does is override the ToString() method to render the report name.
public class ReportHolder
{
public readonly C1Report Report;
public ReportHolder(C1Report report)
{
Report = report;
}
override public string ToString()
{
string s = Report.ReportName;
return (s != null && s.Length > 0)? s: "Unnamed Report";
}
}
Finally, the SaveFile method prompts the user for a file name and uses an XmlWriter to save each report into the new file using C1Report.Save method. Add the following code to implement the SaveFile method:
Public Sub SaveFile()
' get name of file to save
Dim dlg As New SaveFileDialog()
dlg.FileName = _fileName
dlg.Title = "Save Report Definition File"
If dlg.ShowDialog() <> Windows.Forms.DialogResult.OK Then Return
' save file
Dim w As New XmlTextWriter(dlg.FileName, System.Text.Encoding.Default)
w.Formatting = Formatting.Indented
w.Indentation = 2
w.WriteStartDocument()
' write all reports to it
Cursor = Cursors.WaitCursor
w.WriteStartElement("Reports")
Dim rh As ReportHolder
For Each rh In _list.Items
rh.Report.Save(w) 'rh.Report.ReportName
Next
w.WriteEndElement()
Cursor = Cursors.Default
' close the file
w.Close()
' and be done
_fileName = dlg.FileName
_dirty = False
UpdateUI()
End Sub
• C#
public void SaveFile()
{
// get name of file to save
SaveFileDialog dlg = new SaveFileDialog();
dlg.FileName = _fileName;
dlg.Title = "Save Report Definition File";
if (dlg.ShowDialog() != DialogResult.OK)
return;
// save file
XmlTextWriter w = new XmlTextWriter(dlg.FileName, System.Text.Encoding.Default);
w.Formatting = Formatting.Indented;
w.Indentation = 2;
w.WriteStartDocument();
// write all reports to it
Cursor = Cursors.WaitCursor;
w.WriteStartElement("Reports");
foreach (ReportHolder rh in _list.Items)
rh.Report.Save(w); //rh.Report.ReportName;
w.WriteEndElement();
Cursor = Cursors.Default;
// close the file
w.Close();
// and be done
_fileName = dlg.FileName;
_dirty = false;
UpdateUI();
}
|