using System;
using System.Collections;
using System.Windows.Forms;
using System.Drawing;
class QueueDemo : Form
{
static Queue simpleQueue;
Button btnEnQueue,btnPeek,btnDeQueue;
TextBox txtItem;
Label lblItem,lblQueue;
ListBox lstQueueUI;
public QueueDemo()
{
simpleQueue = new Queue(32);
lblItem = new Label();
lblItem.Text = "Item To Add";
lblItem.Location = new Point(16,16);
lblItem.AutoSize = true;
txtItem = new TextBox();
txtItem.Location = new Point(lblItem.Right + 8 ,lblItem.Top);
txtItem.Size = new Size(256,27);
btnEnQueue = new Button();
btnEnQueue.Size = btnEnQueue.Size;
btnEnQueue.Location = new Point(txtItem.Right + 8,lblItem.Top);
btnEnQueue.Text = "EnQueue";
btnEnQueue.Click += new EventHandler(btnEnQueue_Click);
btnPeek = new Button();
btnPeek.Size = btnEnQueue.Size;
btnPeek.Location = new Point(btnEnQueue.Right + 8,lblItem.Top);
btnPeek.Text = "Peek";
btnPeek.Click += new EventHandler(btnPeek_Click);
btnDeQueue = new Button();
btnDeQueue.Size = btnEnQueue.Size;
btnDeQueue.Location = new Point(btnPeek.Right + 8,lblItem.Top);
btnDeQueue.Text = "DeQueue";
btnDeQueue.Click += new EventHandler(btnDeQueue_Click);
lblQueue = new Label();
lblQueue.Location = new Point(lblItem.Left,lblItem.Bottom + 32);
lblQueue.Text = "Visual Queue";
lstQueueUI = new ListBox();
lstQueueUI.Location = new Point(lblItem.Left,lblQueue.Bottom + 16);
lstQueueUI.Size = new Size(512,256);
this.Text = "Queue Demo";
this.Controls.AddRange( new Control[]
{
lblItem,txtItem,
btnEnQueue,btnPeek,btnDeQueue,
lblQueue,lstQueueUI
});
}
void btnEnQueue_Click(object sender,EventArgs e)
{
if(txtItem.Text.Trim() != String.Empty)
{
simpleQueue.Enqueue(txtItem.Text.Trim());
lstQueueUI.Items.Insert(lstQueueUI.Items.Count,
"Added Element: " + txtItem.Text.Trim() + " At " + DateTime.Now.ToString());
}
else
{
MessageBox.Show("Empty Value Cannot be Added","QueueDemo",
MessageBoxButtons.OK,MessageBoxIcon.Information);
}
}
void btnPeek_Click(object sender,EventArgs e)
{
try
{
MessageBox.Show("Peek Element: "
+ simpleQueue.Peek().ToString());
}
catch(Exception ex)
{
MessageBox.Show("Error: " + ex.Message,"QueueDemo",
MessageBoxButtons.OK,MessageBoxIcon.Error);
}
}
void btnDeQueue_Click(object sender,EventArgs e)
{
try
{
MessageBox.Show("Removed Element: " +
simpleQueue.Dequeue().ToString());
lstQueueUI.Items.RemoveAt(0);
}
catch(Exception ex)
{
MessageBox.Show("Error: " + ex.Message,"QueueDemo",
MessageBoxButtons.OK,MessageBoxIcon.Error);
}
}
static void Main()
{
Application.Run(new QueueDemo());
}
}
|