In This Topic
The NOV framework supplies a set of types representing common dialog boxes like Open File, Save File and Print dialogs. These types provide uniform interface to the native dialog boxes of the underlying platforms / operating systems. Naturally, native dialogs differ in appearance and functionality across different platforms, but they are used in the vast majority of applications, so the users are accustomed to them.
The base type for all common dialogs is called NCommonDialog. It contains the RequestShow method, which is the one and only common method for standard dialog boxes. The table below presents the hierarchy of types derived from NCommonDialog.
Type |
Description |
NCommonDialog |
Abstract base type for all common dialogs. |
|
NPrintDialog |
Represents a platform provided Print Dialog. |
|
NFileDialog |
Abstract base type for file dialogs. |
|
NOpenFileDialog |
Represents a platform provided Open File dialog. |
|
NSaveFileDialog |
Represents a platform provided Save File dialog. |
Open File Dialog
The following steps must be accomplished to display an Open File dialog box:
- Create an NOpenFileDialog instance.
- Configure its properties like file filter, initial directory and multiple file selection.
- Subscribe for the Closed event.
- Call the RequestShow method.
The code below creates and shows an Open File dialog box.
Creating an Open File Dialog |
Copy Code
|
NOpenFileDialog ofd = new NOpenFileDialog();
ofd.SelectedFilter = 1;
ofd.Filter = "Image Files|*.png;*.jpg;*.jpeg;*.bmp;*.gif|All Files|*.*";
ofd.InitialDirectory = @"D:\Images";
ofd.MultiSelect = false;
ofd.Closed += new Function<NOpenFileDialogResult>(ofd_Closed);
ofd.RequestShow();
|
The Closed event is raised when the user closes the dialog box. The event handler method receives a parameter of type NOpenFileDialogResult, which provides access to the dialog box result and the selected files:
Closed Event Handler |
Copy Code
|
void ofd_Closed(NOpenFileDialogResult arg)
{
if ((arg.Result == ENCommonDialogResult.OK) || (arg.Files.Length > 0))
{
FileInfo fileInfo = arg.Files[0];
// NOTE: To access the file data you can use fileInfo.FullName or fileInfo.OpenRead()
}
}
|
The NOpenFileDialogResult instance contains an array of System.IO.FileInfo objects, each of them representing one of the selected files.
In some environments (like Silverlight) the FullName of the files may not be accessible due to security reasons, but you should be able to read the file using the OpenRead method.
Save File Dialog
The following steps must be accomplished to display a Save File dialog box:
- Create an NSaveFileDialog instance.
- Configure its properties like file filter, default file name and default extension.
- Subscribe for the Closed event.
- Call the RequestShow method.
The code below creates and shows a Save File dialog box.
Creating a Save File Dialog |
Copy Code
|
NSaveFileDialog sfd = new NSaveFileDialog();
sfd.DefaultFileName = "Document1";
sfd.DefaultExtension = "rtf";
sfd.Filter = "RTF Files|*.rtf|All Files|*.*";
sfd.SelectedFilter = 1;
sfd.Closed += new Function<NSaveFileDialogResult>(sfd_Closed);
sfd.RequestShow();
|
The Closed event is raised when the user closes the dialog box. The event handler method receives an instance of type NSaveFileDialogResult, which provides access to the dialog box result and the selected file:
Closed Event Handler |
Copy Code
|
void sfd_Closed(NSaveFileDialogResult arg)
{
if (arg.Result == ENCommonDialogResult.OK)
{
FileInfo fileInfo = arg.File;
// ...
}
}
|
In some environments the fileInfo.FullName might not be accessible due to security reasons, but you should be able to save the file with the help of the fileInfo.OpenFile method.
Print Dialog and Printing
Printing in NOV is initiated by the user through a Print Dialog. Print dialogs are represented by the NPrintDialog type, which contains settings for the printer name, page range, number of copies and collation. The code bellow creates and configures a Print Dialog:
Creating a Print Dialog |
Copy Code
|
NPrintDialog printDialog = new NPrintDialog();
printDialog.EnableCustomPageRange = true;
printDialog.EnableCurrentPage = true;
printDialog.PrintRangeMode = ENPrintRangeMode.AllPages;
printDialog.CustomPageRange = new NRangeI(1, 100);
printDialog.NumberOfCopies = 1;
printDialog.Collate = false;
|
Similarly to the other common dialogs, in order to get a notification when the dialog is closed, you have to subscribe for the Closed event.
Subscribing for the Closed Event |
Copy Code
|
printDialog.Closed += new Function<NPrintDialogResult>(printDialog_Closed);
|
The content for printing is provided by a Print Document object. An instance of the NPrintDocument type must be assigned to the print dialog’s PrintDocument property:
Creating a Print Document |
Copy Code
|
NPrintDocument printDocument = new NPrintDocument();
printDocument.DocumentName = "Test Document";
printDialog.PrintDocument = printDocument;
|
The print document object raises notification events during printing. You can subscribe for several events that are raised at different stages of the printing process:
Subscribing for the Print Document's Events |
Copy Code
|
printDocument.BeginPrint += printDocument_BeginPrint;
printDocument.QueryPageSettings += printDocument_QueryPageSettings;
printDocument.PrintPage += printDocument_PrintPage;
printDocument.EndPrint += printDocument_EndPrint;
|
Finally to show the dialog you have to call the RequestShow method:
Showing the Print Dialog |
Copy Code
|
printDialog.RequestShow();
|
When the user clicks the “Print” button on the dialog, the printing process begins. First the print document’s BeginPrint event is raised, followed by a series of QueryPageSettings and PrintPage events. Both QueryPageSettings and PrintPage are raised once for each page. The handler of the QueryPageSettings event is the place where you can change the page size and orientation for the current page. In the PrintPage event handler you have to render the actual contents of the current page. The following example demonstrates how this can be done:
Rendering Pages for Printing |
Copy Code
|
void printDocument_PrintPage(NPrintDocument sender, NPrintPageEventArgs e)
{
NPaintVisitor visitor = new NPaintVisitor(e.Graphics, 300, NMatrix.Identity, null);
switch (m_PageIndex)
{
case 0:
visitor.SetFill(NColor.Red);
break;
case 1:
visitor.SetFill(NColor.Green);
break;
case 2:
visitor.SetFill(NColor.Blue);
break;
}
visitor.PaintEllipse(10, 10, 200, 150);
m_PageIndex++;
e.HasMorePages = (m_PageIndex < 3);
}
|
In order to draw a scene, you need an NPaintVisitor object – one can be created using the NGraphics2D object contained in the NPrintPageEventArgs argument. To signify that there are no more pages to print, you have to set the HasMorePages property of the NPrintPageEventArgs argument to false.
After all pages are printed, or if the printing is cancelled, the EndPrint event is raised.