
C# ПІДРУЧНИКИ / c# / Premier Press - C# Professional Projects
.pdf
This page intentionally left blank
|
|
Y |
|
L |
|
|
F |
|
|
M |
|
A |
|
|
E |
|
|
T |
|
|
Team-Fly®

Chapter 16
Implementing
the Programming
Logic

350 Project 3 CREATING A CREATIVE LEARNING PROJECT
In the preceding chapter, you looked at the design of the Creative Learning application. This project adds the programming logic to the Creative Learning
application.
Adding the Programming Logic to the Application
Before adding the programming logic to the Creative Learning application, you need to understand how the application works.
1.When the Creative Learning application is run, a user needs to specify the names of the source, destination, and processed file directories.
The application, by default, specifies the names of the source, destination, and processed file directories as D:\Creative\Source,
D:\Creative\Destination, and D:\Creative\Processed. The user may
choose to retain the default directory structure or may change the directory structure as required.
2.When the user adds the names of the specified directories and clicks on the OK button, the application checks whether the entered directory structure is valid.
3.If the user enters an invalid directory structure, the application creates an error message in Error Provider and gives focus to the invalid directory.
4.If the user enters a valid directory structure, the application enables the directory watcher.
5.The application then hides the Creative Learning form and displays a notification icon in the status area of the taskbar.
6.While the application is running, it continuously checks whether the user has added a file to the source directory.
7.When the user adds a file to the source directory, the application disables the directory watcher and changes the notification icon in the status area.

IMPLEMENTING THE PROGRAMMING LOGIC |
Chapter 16 |
351 |
|
|
|
|
|
8.The application then validates the file format, and if the format of the file is not correct, the application generates an error entry in Event Viewer.
9.Alternatively, if the file format is correct, the application processes the file. Processing of the file includes extracting data, such as Cash Memo No. and Total amount payable from the cash memo document.
10.Once the file has been processed, the application saves the information from the cash memo document to an XML document, Summary.XML. This XML document is then saved in the destination directory as specified by the user.
11.After creating the Summary.XML document, the application changes the notification icon again and then enables the directory watcher so that the directory watcher can check the directory for any new file.
You have seen how the application works. You can now code to the application so that the application can be deployed at the client site. To start, add code to the form Load() method.
Adding Code to the Form Load() Method
When the form is loaded by using the form Load() method, the default values of the source, destination, and processed file directory are displayed in txtSource, txtDest, and txtProcessedFile text boxes, respectively. In addition, the optGenerateLog check box is checked by default. When the optGenerateLog check box is checked, any errors that occur while the application is running are logged in the Event Viewer. However, if desired, the user may choose to uncheck the optGenerateLog check box.
To load the form, specify the following code to the Load() method of the Creative Learning form.
private void frmCreative_Load(object sender, System.EventArgs e)
{
txtSource.Text=”D:\\Creative\\Source\\”; txtProcessedFile.Text=”D:\\Creative\\Processed\\”; txtDest.Text=”D:\\Creative\\Destination\\”; optGenerateLog.Checked=true;
}

352 Project 3 CREATING A CREATIVE LEARNING PROJECT
When the application is run, the Creative Learning form looks as shown in Figure 16-1 and Figure 16-2.
FIGURE 16-1 The tabSource page of the Creative Learning form at run time
FIGURE 16-2 The tabDest page of the Creative Learning form at run time

IMPLEMENTING THE PROGRAMMING LOGIC |
Chapter 16 |
353 |
|
|
|
|
|
Adding Code to the OK Button
After entering the required information, such as the names of the source, destination, and processed file directory, the user clicks on the OK button. The application then validates the directory structure that is specified by the user. If the directory structure is found to be incorrect, the application creates an error message in Error Provider and then gives the focus to the invalid directory. However, for it to do so, you first need to add an ErrorProvider control from the Windows Forms toolbox.
The ErrorProvider Control
The ErrorProvider control in Windows forms is used to validate data entered by the user in a control. If the data entered by the user is incorrect, the ErrorProvider control displays an icon adjacent to the control in which the user enters the data. You can change the Icon property of the ErrorProvider control to specify the icon that is displayed when an error occurs. By default, Visual Studio .NET displays the icon as shown in Figure 16-3.
FIGURE 16-3 The default icon of the ErrorProvider control
In addition, the ErrorProvider control displays an error message as a ToolTip when the user points to the icon next to the control. You can specify the error

354 Project 3 CREATING A CREATIVE LEARNING PROJECT
message by using the SetError() method of the ErrorProvider class. You will learn to specify an error message by using the SetError() method later in this chapter.
You can include an ErrorProvider control in the Creative Learning form by performing the following steps:
1.Drag the ErrorProvider control from the Windows Forms toolbox to the form.
Figure 16-4 shows an ErrorProvider control in the Windows Forms toolbox.
The ErrorProvider control gets added to the component tray of the form.
2.Change the Name property of the ErrorProvider control to errMessage.
After adding the ErrorProvider control to the form, you need to associate the ErrorProvider control with the control whose value is to be validated. You can do this by passing the name of the control as a parameter to the SetError() method of the ErrorProvider class. In your project, you need to validate the name of the source, destination, and processed file directories specified in the txtSource, txtDest, and txtProcessedFile text boxes, respectively.
3.To validate the names of the directories entered by the user, add the following code to the Click event of the OK button.
if (!Directory.Exists(txtSource.Text))
{
errMessage.SetError(txtSource,”Invalid source directory”); txtSource.Focus();
tabControl1.SelectedTab=tabSource; return;
}
else errMessage.SetError(txtSource,””);

IMPLEMENTING THE PROGRAMMING LOGIC |
Chapter 16 |
355 |
|
|
|
|
|
FIGURE 16-4 An ErrorProvider control in the Windows Forms toolbox
The preceding code uses an if loop to validate the directory structure.The if loop uses the Exists() method of the Directory class to check whether the path of the directory specified in the txtSource text box exists on the hard disk. If the path of the directory specified in the txtSource text box does not exist, the ErrorProvider control is used to display an error message.
The SetError() method is used to display an error message. This method takes the name of the control whose value is to be validated, txtSource, and the error message to be displayed as a ToolTip, Invalid source directory, as parameters. The txtSource.Focus(); command is used to set the focus of the application to the txtSource text box.Then, the SelectedTab property of the TabControl control is used to set the tabSource tabbed page as the selected page. However, if the directory structure specified by the user is correct, no text is displayed in the ErrorProvider control. Figure 16-5 shows an icon next to the txtSource text box.
Similarly, you can add code to validate the directory structure in the txtDest and txtProcessedFile text boxes.

356 Project 3 CREATING A CREATIVE LEARNING PROJECT
FIGURE 16-5 An error icon next to the txtSource text box
Another important aspect of the application is when a user specifies an invalid directory structure, the color of the text box changes to pink. To do this, add the following code to the KeyUp event of the txtSource text box.
private void txtSource_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
{
if (Directory.Exists(txtSource.Text)) txtSource.BackColor=Color.White;
else txtSource.BackColor=Color.Pink;
}
Similarly, you can add code to the txtDest and txtProcessedFile text boxes.
When the directory structure is validated, you need to enable the directory watcher and display the notification icon in the status area. In addition, you need to hide the Creative Learning form from the taskbar. This will enable your application to appear minimized in the system tray. Before doing that, add a FileS ystemWatcher component to the Creative Learning form.

IMPLEMENTING THE PROGRAMMING LOGIC |
Chapter 16 |
357 |
|
|
|
|
|
The FileSystemWatcher Component
The FileSystemWatcher component in Visual Studio .NET is used to monitor the contents of a directory. This implies that you can monitor the contents of a directory by using the FileSystemWatcher component and perform custom actions when the contents of the directory are changed. For example, you can use the FileSystemWatcher component to find any modifications made to the content of the entire directory or one or more files in the specified directory. When a user makes some change to the files in a specified directory, such as adding, deleting, or modifying a file, the FileS ystemWatcher component generates an event. For example, when a user adds a file to the specified directory, the Created event is generated. You will learn about the Created event in Chapter 17, “Interacting with a Microsoft Word Document and Event Viewer.”
To add a FileSystemWatcher component to the Creative Learning form, perform the following steps:
1.Drag a FileSystemWatcher component from the Components toolbox.
The FileSystemWatcher component gets added to the component tray. Figure 16-6 shows the FileSystemWatcher component in the Components toolbox.
2.Change the following properties of the FileSystemWatcher component.
Name: watchDir
Filter: *.doc
Specifying the value of the Filter property to *.doc restricts the watchDir component to monitoring only Microsoft Word documents.
TIP
By default, the EnableRaisingEvent property of the FileSystemWatcher component is set to true. Therefore, as soon as the FileSystemWatcher component is enabled, it starts monitoring the specified directory and raises an event when a change occurs.
3.To enable the watchDir component, add the following statement to the Click event of the OK button.
watchDir.EnableRaisingEvents=true;