Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Microsoft C# Professional Projects - Premier Press.pdf
Скачиваний:
177
Добавлен:
24.05.2014
Размер:
14.65 Mб
Скачать

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;

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();

}