Click here to Skip to main content
15,889,874 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am writing a small program to manage store and price based on the item quantity. In the desire functionality I want the Grand Total of all items cost to be displayed in the label. This label updates as i change the values in qty text boxes.
So far i have done writing this code and it is working perfectly and even the values are exactly the same in variables as i want but the problem is label doesn't update even i have null values in the qty text boxes. It keep shows the previous total until i change the values, and it seems get stuck on the last updated total.

C#
namespace wpfTest
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        List<int> myList = new List<int>();
        int val1, val2;
        int total;

        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            qtyTxt1.IsEnabled = false;
            priceTxt1.IsEnabled = false;

            qtyTxt2.IsEnabled = false;
            priceTxt2.IsEnabled = false;
        }

        private void chk1_Click(object sender, RoutedEventArgs e)
        {
            if (chk1.IsChecked == true)
            {
                qtyTxt1.IsEnabled = true;
                priceTxt1.IsEnabled = true;
            }
            else
            {
                qtyTxt1.IsEnabled = false;
                priceTxt1.IsEnabled = false;

                qtyTxt1.Clear();
                priceTxt1.Text = "@50";
            }
        }

        private void chk2_Click(object sender, RoutedEventArgs e)
        {
            if (chk2.IsChecked == true)
            {
                qtyTxt2.IsEnabled = true;
                priceTxt2.IsEnabled = true;
            }
            else
            {
                qtyTxt2.IsEnabled = false;
                priceTxt2.IsEnabled = false;

                qtyTxt2.Clear();
                priceTxt2.Text = "@100";
            }
        }

        private void qtyTxt1_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (qtyTxt1.Text=="")
            {
                priceTxt1.Text = "@50";
                val1 = 0;
            }
            if (qtyTxt1.Text.Length > 0)
            {
                priceTxt1.Text = (Convert.ToInt32(qtyTxt1.Text) * 50).ToString();
                val1 = Convert.ToInt32(priceTxt1.Text);
                updateTotal();
            }
        }

        private void qtyTxt2_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (qtyTxt2.Text.Length == 0)
            {
                priceTxt2.Text = "@100";
                val2 = 0;
            }
            if (qtyTxt2.Text.Length > 0)
            {
                priceTxt2.Text = (Convert.ToInt32(qtyTxt2.Text) * 100).ToString();
                val2 = Convert.ToInt32(priceTxt2.Text);
                updateTotal();
            }
        }

        private void updateTotal()
        {
            total = 0;
            total = val1 + val2;
            totalTxt.Clear();
            totalTxt.Text = (val1 + val2).ToString();
            
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("Total : " + total + "\n" + "Val1 : " + val1 + "\n" + "Val2 : " + val2);
        }
      
    }
}


I have also uploaded screen shot at my Microsoft onedrive account because i have limitation here to upload photograph.

[^]

Thanks
Posted
Comments
Laiju k 24-Apr-14 23:41pm    
where is the label here

1 solution

try to find the issue by your self. do you know how to debug?
mastering debugging in VS[^]
if you debug and check, you will find some values not set as you expected.

for example here you will not get expected value for Convert.ToInt32 why!
go and check the documentation for that method. you will find the reason
http://msdn.microsoft.com/en-us/library/sf1aw27b.aspx[^]

you will get format exception for values like “@50”
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900