BillHung.Net


powered by FreeFind     sms

Lab 05 GUI Control and Event

15 Nov 2006

Output

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

//Lab 5
//Learn about GUI events, forms and controls

namespace Lab05
{
public partial class Form1 : Form
{
private System.IO.StreamReader reader = null;
private string currentCvsFile = null;

System.Collections.ArrayList filledSeats = new System.Collections.ArrayList();
bool lookingAtEmptySeats = false;

public Form1()
{
InitializeComponent();
currentCvsFile = "airline.csv";
ReadPassengers(currentCvsFile);
}

//read a file for input data and store the data into an ArrayList
// Show the content of the comma separated value (CSV) file
public void ReadPassengers(string currentCvsFile)
{
string delimitStr = ",\n";
string[] strLine = null;
char[] delimiter = delimitStr.ToCharArray();
filledSeats.Clear();

try
{ reader = new System.IO.StreamReader(currentCvsFile); }
catch
{ Console.WriteLine("open file {0} error\n", currentCvsFile); }

for (string line = reader.ReadLine(); line != null; line = reader.ReadLine())
{
Console.WriteLine(line);

strLine = line.Split(delimiter);
if (strLine.Length == 4)
{
ListViewItem lvi = new ListViewItem(strLine);
listView1.Items.Add(lvi);
filledSeats.Add(new Seat(strLine[2], strLine[3]));
}
}
}

//Display the empty seats of a flight
private void dispalyEmptySeats(object sender, EventArgs e)
{
ListViewItem lvi =null;
listView1.Items.Clear();
string[] strLine = { "row", "column" };
lookingAtEmptySeats = true;
System.Collections.ArrayList emptySeats = new System.Collections.ArrayList();
CompareSeat seatCompare = new CompareSeat();

for (char ch = 'a'; ch <= 'z'; ch++)
for (int i = 1; i < 7; i++)
emptySeats.Add(new Seat(ch.ToString(), i.ToString()));

foreach (Seat tempSeat in filledSeats)
{
int arrayIndex = emptySeats.BinarySearch(tempSeat, seatCompare);
if (arrayIndex > 0)
emptySeats.RemoveAt(arrayIndex);
}

foreach (Seat tempSeat in emptySeats)
{
strLine[0] = tempSeat.row;
strLine[1] = tempSeat.column;
lvi = new ListViewItem(strLine);
listView1.Items.Add(lvi);
}

}

//Display the filled seats of a flight
private void dispalyFilledSeats(object sender, EventArgs e)
{
lookingAtEmptySeats = false;
listView1.Items.Clear();
ReadPassengers(currentCvsFile);
}

//Exit the program
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
reader.Close();
Application.Exit(); 
}

//When another item of the listView is selected, change the text boxes
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{

if (listView1.SelectedIndices.Count > 0)
{ 
int index = listView1.SelectedIndices[0];
ListViewItem lvi = listView1.Items[index];

if (lookingAtEmptySeats == false)
{
textBox1.Text = lvi.SubItems[2].Text;
textBox2.Text = lvi.SubItems[3].Text;
}
else if (lvi.SubItems.Count==2)
{
textBox1.Text = lvi.SubItems[0].Text;
textBox2.Text = lvi.SubItems[1].Text;
}
}
}

//the open CVS file button in tool strip (tool bar) and menu strip (menu bar)
private void openCVS(object sender, EventArgs e)
{
listView1.Items.Clear();
openFileDialog1.ShowDialog();
currentCvsFile = openFileDialog1.FileName;
ReadPassengers(currentCvsFile);
}

//clear the content of the listView 
private void ClearListView(object sender, EventArgs e)
{
listView1.Items.Clear();
}
}
//The Seat Class
public class Seat
{
private String strRow;
private String strColumn;

public Seat() { }
public Seat(String row, String column)
{
strRow = row;
strColumn = column;
}

public String row
{ get { return strRow; } }
public String column
{ get { return strColumn; } }
}
//comparer for the binarysearch of the arraylist seats
class CompareSeat : System.Collections.IComparer
{
int System.Collections.IComparer.Compare(Object x, Object y)
{
Seat sx = (Seat)x;
Seat sy = (Seat)y;
return ((System.Convert.ToInt32(sx.column) - System.Convert.ToInt32(sy.column))
+
(System.Convert.ToChar(sx.row) * 100 - System.Convert.ToChar(sy.row) * 100));
}
}
}