display stack on listbox

Posted by Mavic under C# on 3/15/2012 | Points: 10 | Views : 1976 | Status : [Member] | Replies : 2
I need to display the stack list on a listbox, when i push a button (push) the data will add to stack and display each on the listbox and every time user enter data(names, etc) its keep on adding to the stack and display on the list box..

Any help with an example will be helpful....

regards
Mavic

private void button1_Click(object sender, EventArgs e)
{
int result;
result = stack.push(txtLastName.Text + txtPayRate.Text);
if (result == 1)
{
MessageBox.Show("Employee Lastname: " + txtLastName.Text + "\n Employee Payrate: " + txtPayRate.Text + "");
}
else
MessageBox.Show("You can't add an empty value");
txtLastName.Clear();
txtPayRate.Clear();
}
private void button2_Click(object sender, EventArgs e)
{
string top;
top = stack.top();
if (top != null)
{
MessageBox.Show("The Employee on top is: " + top);
}
else
MessageBox.Show("Your stack is empty");
}
private void button3_Click(object sender, EventArgs e)
{
string pop;
pop = stack.pop();
if (pop != null)
{
MessageBox.Show("Employee is dismissed: " + pop);
lstStack.Items.Clear();
}
else
MessageBox.Show("Your stack is empty");
}
private void button4_Click(object sender, EventArgs e)
{
lstStack.Items.Add(txtLastName.Text);
lstStack.Items.Add(txtPayRate.Text);
}
}




Responses

Posted by: Patel28rajendra on: 3/15/2012 [Member] Starter | Points: 25

Up
0
Down
Hi

please see following links it will guide you to solve your problem



1. http://msdn.microsoft.com/en-us/library/ms379571%28v=vs.80%29.aspx

2.http://www.codeproject.com/Articles/17029/The-use-of-Stacks-in-C

Thanks

R D Patel

Mavic, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Hmanjarawala on: 3/15/2012 [Member] Bronze | Points: 25

Up
0
Down
Hi here is the solution:

private void AddToList()
{
listBox1.Items.Clear();
if (stack.Peek() != null)
{
var array = stack.ToArray();
for (int i = array.Length - 1; i > -1; i--)
{
var o = (object[])array[i];
listBox1.Items.Add(o[0].ToString() + " " + o[1].ToString());
}
}
}

private void button1_Click(object sender, EventArgs e)
{
stack.Push(new object[] { txtName.Text, txtRate.Text });
//if (result == 1)
//{
// MessageBox.Show("Employee Lastname: " + txtName.Text + "\n Employee Payrate: " + txtRate.Text + "");
//}
//else
// MessageBox.Show("You can't add an empty value");
AddToList();
txtName.Clear();
txtRate.Clear();
}

private void button2_Click(object sender, EventArgs e)
{
if (stack.Count > 0)
{
stack.Pop();
AddToList();
}
}


Himanshu Manjarawala
Sr. Software Engineer@AutomationAnywhere
http://fieredotnet.wordpress.com/

Mavic, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response