
C# ПІДРУЧНИКИ / c# / Premier Press - C# Professional Projects
.pdf

INTERACTING WITH A MICROSOFT WORD DOCUMENT |
Chapter 17 |
389 |
|
|
|
|
|
private void btnOK_Click(object sender, System.EventArgs e)
{
if (!Directory.Exists(txtSource.Text))
{
errMessage.SetError(txtSource,”Invalid source directory”); 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(); 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();
}

390 Project 3 CREATING A CREATIVE LEARNING PROJECT
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;
}
private void txtProcessedFile_KeyUp(object sender, System.Windows.Forms
.KeyEventArgs e)
{
if (Directory.Exists(txtProcessedFile.Text)) txtProcessedFile.BackColor=Color.White;
else txtProcessedFile.BackColor=Color.Pink;
}
private void menuItem3_Click(object sender, System.EventArgs e)
{
Application.Exit();
}
private void mnuConfigure_Click(object sender, System.EventArgs e)
{
icoNotify.Visible=false; this.ShowInTaskbar=true; this.Show();
}
private void btnCancel_Click(object sender, System.EventArgs e)
{
Application.Exit();
}
private void icoNotify_DoubleClick(object sender, System.EventArgs e)
{
icoNotify.Visible=false; this.ShowInTaskbar=true;

INTERACTING WITH A MICROSOFT WORD DOCUMENT |
Chapter 17 |
391 |
|
|
|
|
|
this.Show();
}
private void watchDir_Created(object sender, System.IO.FileSystemEventArgs e)
{
watchDir.EnableRaisingEvents=false; icoNotify.Icon=m_Info; icoNotify.Text=”Processed: “+ e.Name;
Word.Application wdApp= new Word.ApplicationClass(); object optional=System.Reflection.Missing.Value; XmlTextWriter xmlWrite;
xmlWrite= new XmlTextWriter(txtDest.Text + “Summary.xml”,null); try
{
Word.Document Doc = new Word.DocumentClass(); object filename=e.FullPath;
Doc=wdApp.Documents.Open(ref filename, ref optional, ref optional, ref optional,ref optional,ref optional,ref optional,ref optional, ref optional,ref optional,ref optional,ref optional);
Word.Range wdRange; wdRange=Doc.Paragraphs.Item(2).Range; string strMemo, strAmount;
int intParacount; strMemo=wdRange.Text; strMemo=strMemo.Substring(15,4); intParacount=Doc.Paragraphs.Count; intParacount=intParacount-2;
wdRange=Doc.Paragraphs.Item(intParacount).Range; object count=”-1”;
object wdCharacter=”1”; wdRange.MoveEnd(ref wdCharacter,ref count); strAmount=wdRange.Text; strAmount=strAmount.Substring(23); xmlWrite.Formatting=Formatting.Indented;
xmlWrite.WriteDocType(“Sales”,null,null,null); xmlWrite.WriteComment(“Summary of sales at Creative Learning”); xmlWrite.WriteStartElement(“Sales”); xmlWrite.WriteStartElement(Convert.ToString(DateTime.Today));

392 Project 3 CREATING A CREATIVE LEARNING PROJECT
xmlWrite.WriteElementString(“Memo”,strMemo); xmlWrite.WriteElementString(“Amount”,strAmount); xmlWrite.WriteEndElement(); xmlWrite.WriteEndElement(); icoNotify.Icon=m_Ready;
}
catch (Exception catchException)
{
icoNotify.Icon=m_Error; icoNotify.Text=”Error in “ + e.Name; if (optGenerateLog.Checked==true)
eventLog.WriteEntry(e.Name + “: “ + catchException.Message);
}
finally
{
xmlWrite.Flush(); xmlWrite.Close();
wdApp.Quit(ref optional, ref optional, ref optional); wdApp=null;
watchDir.EnableRaisingEvents=true;
}
tryagain: try
{
File.Move(e.FullPath,txtProcessedFile.Text+e.Name);
}
catch
{
goto tryagain;
}
}

INTERACTING WITH A MICROSOFT WORD DOCUMENT |
Chapter 17 |
393 |
|
|
|
|
|
private void btnRefresh_Click(object sender, System.EventArgs e)
{
lstEvents.Items.Clear(); eventLog.Log=”Application”; eventLog.MachineName=”.”;
foreach (EventLogEntry logEntry in eventLog.Entries)
{
if (logEntry.Source==”CreativeLearning”)
{
lstEvents.Items.Add(logEntry.Message);
}
}
}
private void btnSummary_Click(object sender, System.EventArgs e)
{
StreamReader strRead; try
{
strRead= new StreamReader(txtDest.Text+”Summary.xml”); MessageBox.Show(strRead.ReadToEnd(),txtDest.Text+”Summary.xml”,
MessageBoxButtons.OK,MessageBoxIcon.Information); strRead.Close();
}
catch(Exception exc)
{
MessageBox.Show(“An error was returned: “ + exc.Message + “Please check the destination folder for summary”);
}
private void txtDest_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
{
if (Directory.Exists(txtDest.Text)) txtDest.BackColor=Color.White;
else

394 Project 3 CREATING A CREATIVE LEARNING PROJECT
txtDest.BackColor=Color.Pink;
}
}
}
Summary
In this chapter, you learned to add code that allows your application to interact with a Word document and Windows Event Viewer.

PARTVIProfessional Project 4
This page intentionally left blank

Project 4
Creating an Airline
Reservation Portal