Wednesday, March 28, 2012

simple addition problem

I am creating a programm

There is one Textbox

2 Buttons, 1 is "Display" Other is "Conintue" button.

User enters a number in the TextBox and click continue , when user clicks continue TextBox clears

and user can Enter another value in the TextBox. In the begening program doesnt know how many

times a user going to Enter the values in the TextBox. User could enter 2, 3, 4, .. values. which is not known

and program suppose to provide this facilitiy to the user to enter as many time as user like to add up all the numbers he/she enters.

For example . you going to enter 2 in the Textbox and click Continue and now the Textbox is clear

and you going to enter 4 and click on Continue button, and now enter 4 again. This timee You going to enter

"Display" Button and on the Label you going to see "10".

It is not must to use the ArrayList or any certain mechism. I am just doing it as practice

So there are two problems.

One is that I have to use the ArrayList to add those textbox interger in the Arraylist.

But how I make the progrom to get a response from the user to "continue"

If the user clicks on the "continue" It would be a click Event and would take the user out of the loop.

2nd one is if the user clicks on the "Display" button how I am going to add the values in the ArrayList

in the click event of Display button. ArrayList is the local in Continue Button Event.

What machnism there should be to come over with the problem.

ArrayList NumberToAdd =newArrayList();

NumberToAdd.Add(Convert.ToInt16(TextBox1.Text));

//now if i want to add another value to ArrayList

//I want to click the button Continue if i do that

//it is going to be another event.

//if i click on the display button. It is going to be

//another event and won't recogonise the NumberToAdd as a variable.

I might be doing everything wrong. Or coming up with the wrong idea to use the ArrayList. Even what i am trying to

achive it is not practical. So any suggestion or mechinism to over come the problem.

Hi,

Store your array list in a ViewState variable. This will help you to access your values / arraylist after post back. You can do this process on continue button.

ViewState["arrList"] = NumberToAdd;

Now on page load assign your values back to your array list variable like.

NumberToAdd = (ArrayList) ViewState["arrList"] ;

Don't forget to move your arraylist declaration to top so that you can access the same variable in all buttons.

Don't forget to Mark this as Answer if this post helps you.

try code below...

protected void btnDisplay_Click(object sender, EventArgs e)
{
if (Session["MyData"] == null)
Response.Write("you have not entered any values...");
else
{
ArrayList arr = (ArrayList)Session["MyData"];
double dblResult = 0.0;
for (int iCount = 0; iCount < arr.Count; iCount++)
dblResult += Convert.ToDouble(arr[iCount].ToString());
Response.Write("Total Result : " + dblResult.ToString());
Session.RemoveAll();
}
}
protected void btnContinue_Click(object sender, EventArgs e)
{
ArrayList arr = new ArrayList();
if (Session["MyData"] == null)
Session["MyData"] = arr;
arr = (ArrayList)Session["MyData"];
arr.Add(TextBox1.Text.Trim());
Session["MyData"] = arr;
TextBox1.Text = "";
}

hope it helps./.


Try with this:

protected void Button1_Click(object sender, EventArgs e)
{
ArrayList NumbersToAdd;
if (ViewState["Numbers"] != null)
{
NumbersToAdd = (ArrayList)ViewState["Numbers"];
}
else
{
NumbersToAdd = new ArrayList();
}
NumbersToAdd.Add(TextBox1.Text);
TextBox1.Text = "";
ViewState["Numbers"] = NumbersToAdd;
}
protected void Button2_Click(object sender, EventArgs e)
{
ArrayList NumbersToAdd = (ArrayList)ViewState["Numbers"];
double a = 0;
for (int i = 0; i < NumbersToAdd.Count; i++)
{
a += double.Parse(NumbersToAdd[i].ToString());
}
Label1.Text = a.ToString();
}


you may use hidden filed for this purpose istead of arraylist.

use the following fucntions for this

Private void Add()
{
if (hid_No.Value != "")
{
char[] splitter = { ',' };
string[] nos = hid_No.Value.Split(splitter);
double sum = 0;
for (int i = 0; i < nos.Length; i++)
{
sum = sum + Convert.ToDouble(nos[i]);
}
sum = sum + Convert.ToDouble(TextBox1.Text);
lbl_Sum.Text = sum.ToString();
hid_No.Value = "";
}
else
{
lbl_Sum.Text = TextBox1.Text;
}
}
private void conti(object sender, EventArgs e)
{
if (hid_No.Value != "")
{
hid_No.Value = hid_No.Value + "," + TextBox1.Text;
}
else
{
hid_No.Value = TextBox1.Text;
}
TextBox1.Text = "";
}

0 comments:

Post a Comment