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

IMPLEMENTING THE BUSINESS LOGIC

Chapter 33

779

 

 

 

 

The preceding code matches the value entered by the user to all the attributes in the Users.xml file If the value of the variable found is false, an error message is displayed. Finally, the reader object is closed using the Close() method of the

XmlTextReader class.

Adding Code to the Query Button in the frmSelectOption Form

When a user selects an option, View Pending Calls or Show Unattended Calls, and clicks on the Query button, the frmPending form or the frmUnattended form is displayed, respectively. Therefore, you first need to track the option selected by the user and display the corresponding form. To do this, add the following code to the Click event of the cmdLoad button.

if (lstOptions.Selection.Value==”viewPending”)

{

ActiveForm=frmPending;

}

else

{

ActiveForm=frmUnattended;

}

The preceding code uses the Value property of the MobileListItem class to find the option selected by the user and then to display the appropriate form. However, displaying the form requires reading data from the Calls.xml file. To display data from the Calls.xml file, you need to add the following code to the Click event of the cmdLoad button.

string lstItem;

XmlTextReader reader = new XmlTextReader(“C:\\Electronix\\Calls.xml”);

reader.MoveToContent();

while (reader.Read())

{

lstItem=””;

if (reader.HasAttributes)

780 Project 6 CREATING A MOBILE APPLICATION

{

reader.MoveToNextAttribute(); reader.MoveToNextAttribute(); if (reader.Value==”Unattended”)

{

reader.MoveToFirstAttribute(); lstItem=reader.Value + “: “; reader.MoveToElement(); lstItem=lstItem+ reader.ReadInnerXml(); lstUnattended.Items.Add(lstItem);

}

if (reader.Value==”Pending”)

{

reader.MoveToFirstAttribute(); lstItem=reader.Value + “: “; reader.MoveToElement(); lstItem=lstItem+ reader.ReadInnerXml(); lstPending.Items.Add(lstItem);

}

}

}

The preceding code creates a variable of the type string, lstItem, and initializes it to a null value. In addition, the code creates an instance, reader, of the XmlTextReader class and initializes it to the Calls.xml file. Next, the MoveToContent() method of the XmlReader class is used to move to the content node in the XML document.

The code uses the Read() method to read the data in the while loop. Inside the while loop, an if loop is used to check whether the current node has any attributes associated with it. If the current node has attributes associated with it, the reader object is moved to read the second attribute, status, in the Calls.xml file. Figure 33-9 displays the content of the Calls.xml file.

IMPLEMENTING THE BUSINESS LOGIC

Chapter 33

781

 

 

 

 

FIGURE 33-9 The content of the Calls.xml file

If the value of the status node is Unattended, the reader object is moved back to the first attribute of the content node, id. To move to the id attribute, the

MoveToFirstAttribute() method of the XmlTextReader class is used. Then, the

value in the id attribute is retrieved using the Value property and stored in the lstItem variable.

Next, the reader object is moved to the Call element that contains the id attribute. This would enable the reader object to read the entire content of the Call element with the value of the status node as Unattended. To read the entire content of the Call element as a string, you can use the ReadInnerXml() method of the XmlTextReader class. Then, the content of the Call element is stored in the lstItem variable and added to the lstUnattended SelectionList control by using

the Add() method of the MobileListItemCollection() class.

Similarly, if the user has selected the View Pending Calls option, the content of the Call element with the value of the status property as Pending is added to the lstPending SelectionList control and displayed in the frmPending form. Figure 33-10 shows the frmSelectOption form with the View Pending Calls option selected.

782 Project 6 CREATING A MOBILE APPLICATION

FIGURE 33-10 The frmSelectOption form with the View Pending Calls option selected

Adding Code to the Mark checked as complete Button in the frmPending Form

When a user checks the pending calls check box and clicks on the Mark checked as complete button, the status of the selected call is changed to Complete in the Calls.xml file. In addition, the entry of the call is removed from the lstPending SelectionList control in the frmPending form. To do this, you need to add the following code to the Click event of the cmdUpdate button.

private void cmdUpdate_Click(object sender, System.EventArgs e)

{

StreamReader strRead; string content, strText;

int index;

strRead= new StreamReader(“C:\\Electronix\\Calls.xml”); content=strRead.ReadToEnd();

strRead.Close();

for (int i=0; i<lstPending.Items.Count; i++)

{

if (lstPending.Items[i].Selected==true)

{

strText=lstPending.Items[i].Text;

IMPLEMENTING THE BUSINESS LOGIC

Chapter 33

 

783

 

 

 

 

 

 

strText=strText.Substring(0,4); index=content.IndexOf(strText); content=content.Remove(index+14,7); content=content.Insert(index+14, “Complete”);

}

}

StreamWriter strWrite;

strWrite = new StreamWriter(“C:\\Electronix\\Calls.xml”); strWrite.Write(content);

strWrite.Close(); lstPending.Items.Clear(); string lstItem; XmlTextReader reader;

reader = new XmlTextReader(“C:\\Electronix\\Calls.xml”); reader.MoveToContent();

while (reader.Read())

{

lstItem=””;

if (reader.HasAttributes)

{

reader.MoveToNextAttribute(); reader.MoveToNextAttribute(); if (reader.Value==”Pending”)

{

reader.MoveToFirstAttribute(); lstItem=reader.Value + “: “; reader.MoveToElement(); lstItem=lstItem+ reader.ReadInnerXml(); lstPending.Items.Add(lstItem);

}

}

}

reader.Close();

}

The preceding code creates an instance, strRead, of the StreamReader class and initializes it to the Calls.xml file in the Electronix folder. Next, a string type variable, content, is declared and initialized to the data in the strRead object.

784 Project 6 CREATING A MOBILE APPLICATION

However, to do this, you first need to use the ReadToEnd() method of the StreamReader class to read the entire content stored in the strRead object. Once the data in the strRead object is stored in the content variable, you can close the strRead object by using the Close() method. Next, a for loop is used to add the data in the content variable as list items to the lstPending SelectionList control.This data is then displayed as a list of calls in the frmPending form. Figure 33-11 shows the list of pending calls at run time.

FIGURE 33-11 The list of pending calls at run time

As you can see in Figure 33-11, the user has selected the C002 pending list check box. After selecting the check box, if the user clicks on the Mark checked as complete button, the status of the C002 call is changed to Complete. You have already added the code to do this in the Click event of the cmdAcceptCall button. I will now discuss the code.

To write the changes to the Calls.xml file, you would need an object of the StreamWriter class. Therefore, the code declares and initializes an object, strWrite, to the Calls.xml file. The strWrite object uses the Write() method of the StreamWriter class to write the changes to the Calls.xml file.The data to be written is passed as a parameter to the Write() method.After doing this, you can close