简体   繁体   中英

automatically update textBox windows phone 8/C#

wassup guys i did a search and found a couple of post they helped out but for some reason it isnt working completely

ok here is my code:

 if (!string.IsNullOrEmpty(amountBox1.Text) && !string.IsNullOrEmpty(amountBox2.Text) &&       !string.IsNullOrEmpty(amountBox3.Text) && !string.IsNullOrEmpty(amountBox4.Text))
            totalBox.Text = (Convert.ToInt32(amountBox1.Text) + Convert.ToInt32(amountBox2.Text) + Convert.ToInt32(amountBox3.Text) + Convert.ToInt32(amountBox4.Text)).ToString();

(TotalBox isEnabled is set to false so it becomes read only)

now this in a way works but it doesn't update like i want. I would like totalbox to update as soon as amountBox1 has a value and then when amountBox2 has a value to update with the two boxes combined and so fourth.

the way its doing it is it wont update until there is something in each box specifically until amountBox4 has a value. Im sure your aware of the fact, what if the user only use two out of the four? help is much appreciated

Try:

var allAmounts = new List<int>();

if (!String.IsNullOrEmpty(amountBox1.Text))
    allAmounts.Add(Convert.ToInt32(amountBox1.Text));

if (!String.IsNullOrEmpty(amountBox2.Text))
    allAmounts.Add(Convert.ToInt32(amountBox2.Text));

if (!String.IsNullOrEmpty(amountBox3.Text))
    allAmounts.Add(Convert.ToInt32(amountBox3.Text));

if (!String.IsNullOrEmpty(amountBox4.Text))
    allAmounts.Add(Convert.ToInt32(amountBox4.Text));

totalBox.Text = allAmounts.Sum().ToString();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM