Home

Record Navigation

 

Introduction

This sample program is made of a form equipped with many controls that hold information about employees and some buttons to navigate among the records. The application also allows you to add new records. This application was mainly created to apply the ArrayList.Item[] property.

Practical Learning Practical Learning: Creating the StoreItem Library

  1. Start Notepad and, in the empty file, type the following:
     
    using System;
    
    public class Employee
    {
    	public string   FullName;
    	public string   Department;
    	public DateTime DateHired;
    	public Double   Salary;
    
    	public Employee()
    	{
    	
    	}
    }
  2. Save it as Employee.cs
  3. Start another file and fill it up as follows:
     
    using System;
    using System.Drawing;
    using System.Collections;
    using System.Windows.Forms;
    
    namespace GCS
    {
    	
    	public class Exercise : System.Windows.Forms.Form
    	{
    		private System.Windows.Forms.Label label1;
    		private System.Windows.Forms.TextBox txtFullName;
    		private System.Windows.Forms.TextBox txtDepartment;
    		private System.Windows.Forms.Label label2;
    		private System.Windows.Forms.Label label3;
    		private System.Windows.Forms.DateTimePicker dtpDateHired;
    		private System.Windows.Forms.TextBox txtSalary;
    		private System.Windows.Forms.Label label4;
    		private System.Windows.Forms.Button btnAdd;
    		private System.Windows.Forms.Button btnFirst;
    		private System.Windows.Forms.Button btnPrevious;
    		private System.Windows.Forms.Button btnLast;
    		private System.Windows.Forms.Button btnNext;
    		private System.Windows.Forms.Button btnClose;
    		
    		private ArrayList arlEmployees;
    		int CurrentPosition;
    
    		public Exercise()
    		{
    			InitializeComponent();
    		}
    
    		private void InitializeComponent()
    		{
    			this.label1 = new System.Windows.Forms.Label();
    			this.txtFullName = new System.Windows.Forms.TextBox();
    			this.txtDepartment = new System.Windows.Forms.TextBox();
    			this.label2 = new System.Windows.Forms.Label();
    			this.label3 = new System.Windows.Forms.Label();
    			this.dtpDateHired = new System.Windows.Forms.DateTimePicker();
    			this.txtSalary = new System.Windows.Forms.TextBox();
    			this.label4 = new System.Windows.Forms.Label();
    			this.btnAdd = new System.Windows.Forms.Button();
    			this.btnFirst = new System.Windows.Forms.Button();
    			this.btnPrevious = new System.Windows.Forms.Button();
    			this.btnLast = new System.Windows.Forms.Button();
    			this.btnNext = new System.Windows.Forms.Button();
    			this.btnClose = new System.Windows.Forms.Button();
    			
    			// label1
    			this.label1.Location = new System.Drawing.Point(16, 18);
    			this.label1.Size = new System.Drawing.Size(72, 16);
    			this.label1.TabIndex = 0;
    			this.label1.Text = "Full Name:";
    		
    			// TextBox: Full Name
    			this.txtFullName.Location = new System.Drawing.Point(104, 16);
    			this.txtFullName.Size = new System.Drawing.Size(168, 20);
    			this.txtFullName.TabIndex = 1;
    			this.txtFullName.Text = "";
    			
    			// TextBox: Department
    			this.txtDepartment.Location = new System.Drawing.Point(104, 48);
    			this.txtDepartment.Size = new System.Drawing.Size(168, 20);
    			this.txtDepartment.TabIndex = 3;
    			this.txtDepartment.Text = "";
    			
    			// label2 
    			this.label2.Location = new System.Drawing.Point(16, 48);
    			this.label2.Size = new System.Drawing.Size(72, 16);
    			this.label2.TabIndex = 2;
    			this.label2.Text = "Department:";
    			
    			// label3
    			this.label3.Location = new System.Drawing.Point(16, 80);
    			this.label3.Size = new System.Drawing.Size(72, 16);
    			this.label3.TabIndex = 4;
    			this.label3.Text = "Date Hired:";
    			
    			// dtpDateHired
    			this.dtpDateHired.CustomFormat = "dddd dd MMM yyyy";
    			this.dtpDateHired.Format = System.Windows.Forms.DateTimePickerFormat.Custom;
    			this.dtpDateHired.Location = new System.Drawing.Point(104, 80);
    			this.dtpDateHired.Size = new System.Drawing.Size(168, 20);
    			this.dtpDateHired.TabIndex = 5;
    			
    			// txtSalary
    			this.txtSalary.Location = new System.Drawing.Point(104, 112);
    			this.txtSalary.Size = new System.Drawing.Size(168, 20);
    			this.txtSalary.TabIndex = 7;
    			this.txtSalary.Text = "";
    
    			// label4
    			
    			this.label4.Location = new System.Drawing.Point(16, 112);
    			this.label4.Size = new System.Drawing.Size(72, 16);
    			this.label4.TabIndex = 6;
    			this.label4.Text = "Salary:";
    			
    			// btnAdd
    			this.btnAdd.Location = new System.Drawing.Point(24, 144);
    			this.btnAdd.Size = new System.Drawing.Size(72, 23);
    			this.btnAdd.TabIndex = 8;
    			this.btnAdd.Text = "Add";
    			this.btnAdd.Click += new System.EventHandler(this.btnAdd_Click);
    			
    			// btnFirst
    			this.btnFirst.Location = new System.Drawing.Point(104, 144);
    			this.btnFirst.Size = new System.Drawing.Size(40, 23);
    			this.btnFirst.TabIndex = 9;
    			this.btnFirst.Text = "| <";
    			this.btnFirst.Click += new System.EventHandler(this.btnFirst_Click);
    			
    			// btnPrevious
    			this.btnPrevious.Location = new System.Drawing.Point(144, 144);
    			this.btnPrevious.Size = new System.Drawing.Size(40, 23);
    			this.btnPrevious.TabIndex = 10;
    			this.btnPrevious.Text = "<<";
    			this.btnPrevious.Click += new System.EventHandler(this.btnPrevious_Click);
    			
    			// btnLast
    			this.btnLast.Location = new System.Drawing.Point(232, 144);
    			this.btnLast.Size = new System.Drawing.Size(40, 23);
    			this.btnLast.TabIndex = 12;
    			this.btnLast.Text = "> |";
    			this.btnLast.Click += new System.EventHandler(this.btnLast_Click);
    			
    			// btnNext
    			this.btnNext.Location = new System.Drawing.Point(192, 144);
    			this.btnNext.Size = new System.Drawing.Size(40, 23);
    			this.btnNext.TabIndex = 11;
    			this.btnNext.Text = ">>";
    			this.btnNext.Click += new System.EventHandler(this.btnNext_Click);
    			
    			// btnClose
    			this.btnClose.Location = new System.Drawing.Point(280, 144);
    			this.btnClose.Name = "btnClose";
    			this.btnClose.Size = new System.Drawing.Size(72, 23);
    			this.btnClose.TabIndex = 13;
    			this.btnClose.Text = "Close";
    			this.btnClose.Click += new System.EventHandler(this.btnClose_Click);
    		
    			// Form: Exercise
    			this.ClientSize = new System.Drawing.Size(368, 182);
    			this.Controls.Add(this.btnClose);
    			this.Controls.Add(this.btnLast);
    			this.Controls.Add(this.btnNext);
    			this.Controls.Add(this.btnPrevious);
    			this.Controls.Add(this.btnFirst);
    			this.Controls.Add(this.btnAdd);
    			this.Controls.Add(this.txtSalary);
    			this.Controls.Add(this.label4);
    			this.Controls.Add(this.dtpDateHired);
    			this.Controls.Add(this.label3);
    			this.Controls.Add(this.txtDepartment);
    			this.Controls.Add(this.label2);
    			this.Controls.Add(this.txtFullName);
    			this.Controls.Add(this.label1);
    			this.MaximizeBox = false;
    			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
    			this.Text = "Employees Record";
    			this.Load += new System.EventHandler(this.Form1_Load);
    		}
    		
    		static void Main() 
    		{
    			Application.Run(new Exercise());
    		}
    
    		private void Form1_Load(object sender, System.EventArgs e)
    		{
    			arlEmployees = new ArrayList();
    		}
    
    		private void btnAdd_Click(object sender, System.EventArgs e)
    		{
    			if( this.btnAdd.Text.Equals("Add") )
    	 {
    		 this.txtFullName.Text    = "";
    		 this.txtDepartment.Text  = "";
    		 this.dtpDateHired.Text  = (DateTime.Now).ToString();
    		 this.txtSalary.Text      = "";
    		 this.btnAdd.Text = "Update";
    		 this.txtFullName.Focus();
    	 }
    	 else
    	 {
    		 Employee Empl = new Employee();
    
    		 try {
    			 Empl.FullName   = this.txtFullName.Text;
    			 Empl.Department = this.txtDepartment.Text;
    			 Empl.DateHired  = this.dtpDateHired.Value;
    			 Empl.Salary     = Double.Parse(this.txtSalary.Text);
    
    			 arlEmployees.Add(Empl);
    		 }
    		 catch(NotSupportedException)
    		 {
    			 MessageBox.Show("The item could not be added");
    		 }
    		 catch(Exception ex)
    		 {
    			 MessageBox.Show("The item could not be added to the list\n" +
    					ex.Message);
    		 }
    				this.txtFullName.Text    = "";
    				this.txtDepartment.Text  = "";
    				this.dtpDateHired.Text  = (DateTime.Now).ToString();
    				this.txtSalary.Text      = "";
    		 this.btnAdd.Text = "Add";
    				this.txtFullName.Focus();
    	 }
    		}
    
    		private void btnFirst_Click(object sender, System.EventArgs e)
    		{
    			CurrentPosition = 0;
    	 Employee Empl = new Employee();
    
    	 try {
    		 Empl = (Employee)arlEmployees[CurrentPosition];
    
    		 this.txtFullName.Text   = Empl.FullName;
    		 this.txtDepartment.Text = Empl.Department;
    		 this.dtpDateHired.Text  = Empl.DateHired.ToString();
    		 this.txtSalary.Text     = Empl.Salary.ToString();
    	 }
    	 catch(ArgumentOutOfRangeException)
    	 {
    		 MessageBox.Show("The item could not be retrieved");
    	 }
    	 catch(Exception)
    	 {
    		 MessageBox.Show("There was a problem retrieving the item");
    	 }
    		}
    
    		private void btnPrevious_Click(object sender, System.EventArgs e)
    		{
    			Employee Empl = new Employee();
    
    	 if( CurrentPosition == 0 )
    		 return;
    	 else
    	 {
    		 CurrentPosition = CurrentPosition - 1;
    
    		 try {
    			 Empl = (Employee)arlEmployees[CurrentPosition];
    
    			 this.txtFullName.Text   = Empl.FullName;
    			 this.txtDepartment.Text = Empl.Department;
    			 this.dtpDateHired.Text  = Empl.DateHired.ToString();
    			 this.txtSalary.Text     = Empl.Salary.ToString();
    		 }
    		 catch(ArgumentOutOfRangeException)
    		 {
    			 MessageBox.Show("The item could not be retrieved");
    		 }
    		 catch(Exception)
    		 {
    			 MessageBox.Show("There was a problem retrieving the item");
    		 }
    	 }
    		}
    
    		private void btnNext_Click(object sender, System.EventArgs e)
    		{
    			Employee Empl = new Employee();
    
    	 if( CurrentPosition == (arlEmployees.Count - 1) )
    		 return;
    	 else
    	 {
    		 CurrentPosition = CurrentPosition + 1;
    				 
    		 try {
    			 Empl = (Employee)arlEmployees[CurrentPosition];
    
    			 this.txtFullName.Text   = Empl.FullName;
    			 this.txtDepartment.Text = Empl.Department;
    			 this.dtpDateHired.Text  = Empl.DateHired.ToString();
    			 this.txtSalary.Text     = Empl.Salary.ToString();
    		 }
    		 catch(ArgumentOutOfRangeException)
    		 {
    			 MessageBox.Show("The item could not be retrieved");
    		 }
    		 catch(Exception)
    		 {
    			 MessageBox.Show("There was a problem retrieving the item");
    		 }
    	 }
    		}
    
    		private void btnLast_Click(object sender, System.EventArgs e)
    		{
    			CurrentPosition = arlEmployees.Count - 1;					  
                Employee Empl = new Employee();
    
    	try {
    		Empl = (Employee)arlEmployees[CurrentPosition];
    
    		  this.txtFullName.Text   = Empl.FullName;
    		  this.txtDepartment.Text = Empl.Department;
    		  this.dtpDateHired.Text  = Empl.DateHired.ToString();
    		  this.txtSalary.Text     = Empl.Salary.ToString();
    	 }
    	 catch(ArgumentOutOfRangeException)
    	 {
    		  MessageBox.Show("The item could not be retrieved");
    	 }
    	 catch(Exception)
    	 {
    		  MessageBox.Show("There was a problem retrieving the item");
    	 }
    		}
    
    		private void btnClose_Click(object sender, System.EventArgs e)
    		{
    			Close();
    		}
    	}
    }
  4. Save it as RecordNav.cs in the same folder as the above Employee.cs file
  5. Open the Command Prompt and switch to the above folder
  6. To compiler the application, type csc Employee.cs RecordNav.cs and press Enter
  7. To execute the application, type Exercise
  8. Close Notepad
 

Home Copyright 2004 FunctionX, Inc.