Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

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

.pdf
Скачиваний:
475
Добавлен:
12.02.2016
Размер:
14.7 Mб
Скачать

318 Project 2 CREATING THE EMPLOYEE RECORDS SYSTEM PROJECT

{

MessageBox.Show(“You clicked me!”);

}

//STAThreadAttribute indicates that Windows Forms uses the

//single-threaded apartment model.

[STAThreadAttribute]

 

 

public static void Main(string[] args)

Y

{

 

 

 

 

 

 

 

Application.Run(new EventSampleForm ());

 

}

 

 

F

 

 

 

 

}

 

 

M

 

 

 

 

 

The essential steps in an event handlingLapplication are as follows:

 

 

A

 

The source of an event is an instance of System.Windows.Forms.<control>

 

control.

E

 

 

 

T

 

 

The <control> raises an event.

 

The delegate for the event is EventHandler.

The form has an event handler called Control_Event.

The Control_Event is tied to the event.

Displaying Employee Details in the ListView Control

In the ERS application, the employee details need to be displayed in the ListView control at the click of an employee code in the TreeView control. Items can be added to ListView control using the ListView Collection Editor or programmatically. For this application, you need to add the items programmatically, because the items are dependent on an event, the click of an employee code node in the TreeView control.

However, before the list view control is populated, you need to create column headers for the ListView control. A column header is an item in a ListView control that contains heading text. I have put the code for displaying the column headers in the initializeListControl method, as given below.

protected void initializeListControl()

{

listView1.Clear();

listView1.Columns.Add(“Employee Name”,225,HorizontalAlignment.Left );

Team-Fly®

IMPLEMENTING THE BUSINESS LOGIC

Chapter 14

319

 

 

 

 

listView1.Columns.Add(“Date of Join”,70,HorizontalAlignment.Right );

listView1.Columns.Add(“Grade”,105,HorizontalAlignment.Left );

listView1.Columns.Add(“Salary”,105,HorizontalAlignment.Left );

}

The Columns property of the ListView class contains a collection of all the column headers that appear in the control. The Columns property returns a collection containing ColumnHeader objects that are displayed in the ListView control. The ColumnHeader objects define the text to be displayed for a column and is contained

in the ListView.ColumnHeaderCollection.

You can add a column header to the collection using the Add method. Alternatively, you can create an array of ColumnHeader objects and pass it to the AddRange method to add a number of column headers.

Table 14-4 explains some of the commonly used methods of the ListView.Column-

HeaderCollection.

Table 14-4 ListView.ColumnHeaderCollection Members

Method

Description

Add

This overloaded method adds a column header to the collection.

AddRange

This method adds an array of column headers to the collection.

Clear

This method removes all column headers fr om the collection.

Contains

This method determines whether the specified method is contained in the

 

collection.

Insert

This method inserts a column header into the collection at the specified

 

index.

Remove

This method removes the specified column header from the collection.

RemoveAt

This method removes the column header at the specified index from within

 

the collection.

 

 

The final task is to read the EmpRec.xml XML file and display the details of an employee whose employee code has been clicked in the TreeView control.

protected void PopulateListView(TreeNode currNode)

{

initializeListControl();

320 Project 2 CREATING THE EMPLOYEE RECORDS SYSTEM PROJECT

XmlTextReader listRead= new XmlTextReader(“E:\\BookProj\\EmpRec.xml”); listRead.MoveToElement();

while(listRead.Read())

{

string strNodename; string strNodePath; string name; string grade; string doj;

string sal;

string[] strItemsArr=new String [4]; listRead.MoveToFirstAttribute(); strNodename=listRead.Value; strNodePath=currNode.FullPath.Remove(0,17); if(strNodePath==strNodename)

{

ListViewItem lvi; listRead.MoveToNextAttribute(); name=listRead.Value; lvi=listView1.Items.Add(name); listRead.Read(); listRead.Read(); listRead.MoveToFirstAttribute(); doj=listRead.Value; lvi.SubItems.Add(doj); listRead.MoveToNextAttribute(); grade=listRead.Value; lvi.SubItems.Add(grade); listRead.MoveToNextAttribute(); sal=listRead.Value; lvi.SubItems.Add(sal); listRead.MoveToNextAttribute(); listRead.MoveToElement(); listRead.ReadString();

}

}

}

IMPLEMENTING THE BUSINESS LOGIC

Chapter 14

321

 

 

 

 

Figure 14-2 displays the ERS application populated with the employee records.

FIGURE 14-2 The ERS application

The code for the entire application is given here.

using System;

using System.Drawing; using System.Collections;

using System.ComponentModel; using System.Windows.Forms; using System.Data;

using System.Xml;

using System.Diagnostics; using System.IO; namespace EmployeeRecords

{

///<summary>

///Summary description for Form1.

///</summary>

public class EmployeeRecordsForm : System.Windows.Forms.Form

{

private System.Windows.Forms.TreeView treeView1; private System.Windows.Forms.ListView listView1; private System.Windows.Forms.StatusBar statusBar1;

private System.Windows.Forms.StatusBarPanel statusBarPanel1;

322Project 2 CREATING THE EMPLOYEE RECORDS SYSTEM PROJECT

///<summary>

///Required designer variable.

///</summary>

private System.ComponentModel.Container components=null; private TreeNode tvRootNode;

public EmployeeRecordsForm()

{

//Required for Windows Form Designer support InitializeComponent();

//TODO: Add any constructor code after InitializeComponent call PopulateTreeView();

initializeListControl();

}

///<summary>

///Clean up any resources being used.

///</summary>

protected override void Dispose( bool disposing )

{

if( disposing )

{

if (components != null)

{

components.Dispose();

}

}

base.Dispose( disposing );

}

#region Windows Form Designer generated code

///<summary>

///Required method for Designer support - do not modify

///the contents of this method with the code editor.

///</summary>

private void InitializeComponent()

{

this.treeView1 = new System.Windows.Forms.TreeView(); this.listView1 = new System.Windows.Forms.ListView(); this.statusBar1 = new System.Windows.Forms.StatusBar(); this.statusBarPanel1 = new System.Windows.Forms.StatusBarPanel();

IMPLEMENTING THE BUSINESS LOGIC

Chapter 14

323

 

 

 

 

((System.ComponentModel.ISupportInitialize)(this.statusBarPanel1))

.BeginInit();

this.SuspendLayout(); this.treeView1.ImageIndex = -1; this.treeView1.Name = “treeView1”; this.treeView1.SelectedImageIndex = -1;

this.treeView1.Size = new System.Drawing.Size(240, 352); this.treeView1.TabIndex = 0;

this.treeView1.AfterSelect += new System.Windows.Forms.TreeViewEventHandler (this.treeView1_AfterSelect);

//

// listView1

//

this.listView1.Activation = System.Windows.Forms.ItemActivation.TwoClick; this.listView1.Location = new System.Drawing.Point(240, 0); this.listView1.Name = “listView1”;

this.listView1.Size = new System.Drawing.Size(480, 352); this.listView1.TabIndex = 1;

this.listView1.View = System.Windows.Forms.View.Details;

//

//statusBar1

this.statusBar1.Location = new System.Drawing.Point(0, 357); this.statusBar1.Name = “statusBar1”;

this.statusBar1.Panels.AddRange(new System.Windows.Forms.StatusBarPanel[] { this.statusBarPanel1});

this.statusBar1.ShowPanels = true;

this.statusBar1.Size = new System.Drawing.Size(720, 24); this.statusBar1.TabIndex = 2;

//statusBarPanel1

//

this.statusBarPanel1.Text = “Click the employee code to view details”;

this.statusBarPanel1.Width = 720;

//

// EmployeeRecordsForm

//

324 Project 2 CREATING THE EMPLOYEE RECORDS SYSTEM PROJECT

this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.AutoScroll = true;

this.ClientSize = new System.Drawing.Size(720, 381); this.Controls.AddRange(new System.Windows.Forms.Control[] { this.statusBar1,

this.listView1, this.treeView1}); this.MaximizeBox = false; this.MinimizeBox = false;

this.Name = “EmployeeRecordsForm”;

this.Text = “Employee Records Monitoring System”; ((System.ComponentModel.ISupportInitialize)(this.statusBarPanel1))

.EndInit(); this.ResumeLayout(false);

}

#endregion

///<summary>

///The main entry point for the application.

///</summary>

[STAThread] static void Main()

{

Application.Run(new EmployeeRecordsForm());

}

protected void PopulateTreeView()

{

statusBarPanel1.Text=”Refreshing Employee Codes. Please wait...”; this.Cursor = Cursors.WaitCursor;

treeView1.Nodes.Clear();

tvRootNode=new TreeNode(“Employee Records”); this.Cursor = Cursors.Default; treeView1.Nodes.Add(tvRootNode);

TreeNodeCollection nodeCollect = tvRootNode.Nodes; string strVal=””;

XmlTextReader reader= new XmlTextReader(“E:\\BookProj\\EmpRec.xml”); reader.MoveToElement();

IMPLEMENTING THE BUSINESS LOGIC

Chapter 14

325

 

 

 

 

try

{

while(reader.Read())

{

if(reader.HasAttributes && reader.NodeType==XmlNodeType.Element)

{

reader.MoveToElement(); reader.MoveToElement(); reader.MoveToAttribute(“Id”); strVal=reader.Value; reader.Read(); reader.Read(); if(reader.Name==”Dept”)

{

reader.Read();

}

//create the child nodes

TreeNode EcodeNode = new TreeNode(strVal);

//Add the Node

nodeCollect.Add(EcodeNode);

}

}

statusBarPanel1.Text=”Click on an employee code to see their record.”;

}

catch(XmlException e)

{

MessageBox.Show(“XML Exception :”+e.ToString());

}

}

protected void initializeListControl()

{

listView1.Clear();

listView1.Columns.Add(“Employee Name”,225,HorizontalAlignment.Left ); listView1.Columns.Add(“Date of Join”,70,HorizontalAlignment.Right ); listView1.Columns.Add(“Grade”,105,HorizontalAlignment.Left );

326 Project 2 CREATING THE EMPLOYEE RECORDS SYSTEM PROJECT

listView1.Columns.Add(“Salary”,105,HorizontalAlignment.Left );

}

protected void PopulateListView(TreeNode currNode)

{

initializeListControl();

XmlTextReader listRead= new XmlTextReader(“E:\\BookProj\\EmpRec.xml”); listRead.MoveToElement();

while(listRead.Read())

{

string strNodename; string strNodePath; string name; string grade; string doj;

string sal;

string[] strItemsArr=new String [4]; listRead.MoveToFirstAttribute(); strNodename=listRead.Value; strNodePath=currNode.FullPath.Remove(0,17); if(strNodePath==strNodename)

{

ListViewItem lvi; listRead.MoveToNextAttribute(); name=listRead.Value; lvi=listView1.Items.Add(name); listRead.Read(); listRead.Read(); listRead.MoveToFirstAttribute(); doj=listRead.Value; lvi.SubItems.Add(doj); listRead.MoveToNextAttribute(); grade=listRead.Value; lvi.SubItems.Add(grade); listRead.MoveToNextAttribute(); sal=listRead.Value; lvi.SubItems.Add(sal);

IMPLEMENTING THE BUSINESS LOGIC

Chapter 14

327

 

 

 

 

listRead.MoveToNextAttribute(); listRead.MoveToElement(); listRead.ReadString();

}

}

}

private void treeView1_AfterSelect(object sender, System.Windows.Forms

.TreeViewEventArgs e)

{

TreeNode currNode = e.Node; if (tvRootNode == currNode )

{

initializeListControl();

statusBarPanel1.Text=”Double click the Employee Records”; return;

}

else

{

statusBarPanel1.Text=”Click an employee code to view individual records”;

}

PopulateListView(currNode);

}

}

}

Summary

You have learned to develop a Windows application using TreeView, ListView, and StatusBar controls and to interact with a XML file.