
C# ПІДРУЧНИКИ / c# / Premier Press - C# Professional Projects
.pdf
358 Project 3 CREATING A CREATIVE LEARNING PROJECT
Until now, you have not specified the path of the directory that the watchDir component will monitor. In this case, the watchDir component needs to monitor the source directory as entered by the user in the txtSource text box.
4.To specify the path of the directory to the watchDir component, add the following statement to the Click event of the OK button.
watchDir.Path=txtSource.Text; |
|
Y |
|
|
|
||
|
|
L |
|
|
|
F |
|
|
M |
|
|
A |
|
||
E |
|
|
|
T |
|
|
|
FIGURE 16-6 A FileSystemWatcher component in the Components toolbox
After enabling the watchDir component, you need to add a notification icon to the form that is displayed in the status area when the user runs the application. You can do this by adding the NotifyIcon control to your form.
The NotifyIcon Control
The NotifyIcon control in Visual Studio .NET is used to denote a process running in the background as an icon in the status area. For example, when you print a document, the printer icon is displayed in the status area of the taskbar. To specify the icon to be displayed, you need to change the Icons property of the control.
Team-Fly®

IMPLEMENTING THE PROGRAMMING LOGIC |
Chapter 16 |
359 |
|
|
|
|
|
To add the NotifyIcon control to your form, perform the following steps:
1.Drag the NotifyIcon control from the Windows Forms toolbox to the Creative Learning form.
A NotifyIcon control with the name notifyIcon1 gets added to the component tray. Figure 16-7 shows the NotifyIcon control in the Windows Forms toolbox.
2.Change the Name property of the NotifyIcon control to mnuNotify.
To display a notification icon, you need to add the icon file (Ready.ico file) to the bin folder of your application and then create an instance of the icon file.
3.To create an instance of the icon file, add the following statement to the
frmCreative class in the CreativeLearning namespace.
private System.Drawing.Icon m_Ready= new System.Drawing.Icon(“Ready.ICO”);
4.To display the notification icon, add the following code to the Click event of the OK button.
icoNotify.Icon=m_Ready;
icoNotify.Visible=true;
After you have specified the notification icon, the user does not need to see the application. Therefore, you can hide the application from the taskbar.
5.To hide the application, add the following code to the Click event of the OK button.
this.ShowInTaskbar=false;
this.Hide();
The entire code for the OK button is as follows:
private void btnOK_Click(object sender, System.EventArgs e)
{
if (!Directory.Exists(txtSource.Text))
{
errMessage.SetError(txtSource,”Invalid source directory”);

360 Project 3 CREATING A CREATIVE LEARNING PROJECT
FIGURE 16-7 The NotifyIcon control in the Windows Forms toolbox
txtSource.Focus(); tabControl1.SelectedTab=tabSource; return;
}
else errMessage.SetError(txtSource,””);
if (!Directory.Exists(txtDest.Text))
{
errMessage.SetError(txtDest,”Invalid destination directory”); txtDest.Focus();
tabControl1.SelectedTab=tabDest; return;
}
else errMessage.SetError(txtDest,””);
if (!Directory.Exists(txtProcessedFile.Text))
{
errMessage.SetError(txtProcessedFile,”Invalid processed file directory”); txtProcessedFile.Focus();

IMPLEMENTING THE PROGRAMMING LOGIC |
Chapter 16 |
361 |
|
|
|
|
|
tabControl1.SelectedTab=tabSource; return;
}
else errMessage.SetError(txtProcessedFile,””); watchDir.Path=txtSource.Text; watchDir.EnableRaisingEvents=true; icoNotify.Icon=m_Ready; icoNotify.Visible=true; this.ShowInTaskbar=false;
this.Hide();
}
Visual Studio .NET also allows you to add a context menu to the NotifyIcon control.The context menu is displayed when the user right-clicks on the notification icon in the status area of the taskbar.
The ContextMenu Control
The ContextMenu control in Visual Studio .NET allows you to create a menu that consists of frequently used commands. The menu that is created is called a context menu. A context menu is associated with a control in a Windows form. The user can access the context menu by right-clicking on the control in the Windows form.
To add a ContextMenu control to the NotifyIcon control, perform the following steps:
1.Drag a ContextMenu control from the Windows Forms control to the Creative Learning form.
A ContextMenu control with the name contextMenu1 is added to the component tray. In addition, a context menu is added to the top of the form.
2.Click on the text Context Menu on the top of the form to add menus to the ContextMenu control.
A menu item gets added to the ContextMenu control. You can change the text of the menu item by typing the text in the Type Here area.
3.Type the name of the menu item as Configure Application.

362Project 3 CREATING A CREATIVE LEARNING PROJECT
4.Change the Name property of the menu item to mnuConfigure.
To add another menu item to the ContextMenu control, type the name of the menu item in the Type Here area below the Configure Application menu item.
5.Type the name of the second menu item as Exit.
6.Change the Name property of the menu item to mnuExit.
Figure 16-8 displays the ContextMenu control in the design view.
FIGURE 16-8 The ContextMenu control in the design vie w
However, the menu items that you created do not contain the code so far.
7.To make the mnuConfigure menu item functional, add the following code to the Click event of the mnuConfigure menu item.
private void mnuConfigure_Click(object sender, System.EventArgs e)
{
icoNotify.Visible=false; this.ShowInTaskbar=true; this.Show();
}

IMPLEMENTING THE PROGRAMMING LOGIC |
Chapter 16 |
363 |
|
|
|
|
|
When the user clicks the mnuConfigure menu item in the context menu, the notify icon disappears and the Creative Learning form is displayed. In addition, the application appears in the taskbar.
8.To make the mnuExit menu item functional, add the following code to the Click event of the mnuExit menu item.
private void menuItem3_Click(object sender, System.EventArgs e)
{
Application.Exit();
}
When the user clicks the mnuExit menu item, the application terminates. In addition, the user can exit the application by clicking on the Exit button. You will learn to add code to the Exit button later in this chapter.
In addition to adding menu items to the ContextMenu control, you can add code that displays the application when the user double-clicks on the notification icon in the status area of the taskbar.
9.To display the application when the user double-clicks on the notification icon, add the following code to the DoubleClick event of the NotifyIcon control.
private void icoNotify_DoubleClick(object sender, System.EventArgs e)
{
icoNotify.Visible=false; this.ShowInTaskbar=true; this.Show();
}
Adding Code to the Exit Button
Add the following code to the Click event of the Exit button. This code will enable the application to terminate when the user clicks on the Exit button.
private void btnCancel_Click(object sender, System.EventArgs e)
{
Application.Exit();
}

364 Project 3 CREATING A CREATIVE LEARNING PROJECT
Summary
In this chapter, you added functionality to the Creative Learning form. While adding code to the form, you learned about a few new Windows forms controls, such as the ErrorProvider, FileSystemWatcher, NotifyIcon, and ContextMenu controls.

Chapter 17
Interacting with a
Microsoft Word
Document and
Event Viewer

366 Project 3 CREATING A CREATIVE LEARNING PROJECT
In the preceding chapter, you looked at how the Creative Learning application works. However, the way that the Creative Learning application works also involves accessing and processing a Microsoft Word document. In addition, the errors generated during the processing of the Microsoft Word document are trapped in Windows Event Viewer. Therefore, your application needs to interact with both the Microsoft Word document and Windows Event Viewer. This project covers the process of interacting with the Word document and Windows
Event Viewer.
Interacting with a
Microsoft Word Document
In the preceding chapter, you learned how to add a FileSystemWatcher component to your application. This component monitors a specified directory for any changes, such as adding or deleting a file. In addition, you changed the Filter property of the FileSystemWatcher component to *.doc. Doing this restricts the function of a FileSystemWatcher component to only monitoring the Word document. Therefore, when a user adds a Word file to the source directory, the FileSystemWatcher component generates a Created event.
The Created Event
The Created event is a public event of the FileSystemWatcher class.The FileSystemWatcher class generates the Created event when a new file is added to a directory specified in the FullPath property of the event.
To handle the Created event, the FileSystemWatcher class contains a delegate
FileSystemEventHandler. The FileSystemEventHandler delegate takes two para-
meters, the object that causes the event and an argument of the type FileSystemEventArgs that contains information about the event. This information includes the name and path of the directory that is monitored by the FileSystemWatcher component.

INTERACTING WITH A MICROSOFT WORD DOCUMENT |
Chapter 17 |
367 |
|
|
|
|
|
In this case, each time a new cash memo is added to the source directory, the FileSystemWatcher component raises a Created event. Therefore, to access and process the cash memo, you need to add programming logic to the Created event
of the FileSystemWatcher class.
Adding Code to the Created Event
When a file is added to the source directory, your application needs to process the cash memo file. File processing involves extracting data from the cash memo, such as Cash Memo No. and Total amount payable. This information is then written to an XML document as a chronological summary of the sales recorded at the bookstores of Creative Learning.
Displaying a Notification Icon with a ToolTip in the Status Area
While the application processes one file, you need to disable the FileSystemWatcher component so that the component does not monitor the source directory for that time. In addition, you can change the notification icon in the status area of the taskbar to denote that the application is processing a file. You can also change the text that is displayed when the user points to the notification icon in the status area. You will now add code to the Created event to perform the activities mentioned earlier in the chapter.
watchDir.EnableRaisingEvents=false; icoNotify.Icon=m_Info; icoNotify.Text=”Processed: “+ e.Name;
The preceding code sets the EnableRaisingEvents property of the FileSystemWatcher class to false. Doing this disables the watchDir component for the time the value of the EnableRaisingEvents property is set to true. Next, the Icon property of the NotifyIcon control is changed to display a different icon in the status area. However, before doing this, you need to add the Info.ico file to the bin folder of your application and then create an instance of the Info.ico file by using the following statement:
private System.Drawing.Icon m_Info= new System.Drawing.Icon(“Info.ICO”);