0

I'm working on this application that gets user's input and puts them into a ListBox and also finds the sum of those numbers(everything works fine). My issue is that every time the user enters a new number that number is being shown in the window(and I don't want that), instead I only want the current sum of all the ListBox numbers to be shown on the window.So if the users enter a new number a new sum should be shown on the window. Please help me. Thank you so much in advanced. Here's my code that runs just fine....

    private void ClickToAddMoreCoins(object sender, RoutedEventArgs e)
    {
        int sum = 0;
        //Hides InputBox and takes input text from user.
        InputBox.Visibility = System.Windows.Visibility.Collapsed;

        // Ensuring that input from user is a integer number
        String input = InputTextBox.Text;
        int result = 0;
        if (int.TryParse(input, out result))
        {
            //Adding number of coins to CoinListBox
            CoinListBox.Items.Add(result);
        }
        else
        {
            MessageBox.Show("Please enter a number of coins");
        }
        sum = CoinListBox.Items.Cast<object>().Sum(x => Convert.ToInt32(x));    
        if(sum > 30)
        {
            //Removing last coin in case number of coins exceeds 30
            CoinListBox.Items.RemoveAt(CoinListBox.Items.Count - 1);
            MessageBoxResult answer = MessageBox.Show("You cannot enter more than 30 coins. Do you want to end?", "Message", MessageBoxButton.YesNo, MessageBoxImage.Question);
            if (answer == MessageBoxResult.Yes)
            {
                Application.Current.Shutdown();
            }
        }

        // Resets InputBox.
        InputTextBox.Text = String.Empty;
    }

2 Answers 2

1

Your main problem was declaring the sum inside the method.

Simply use this:

     int sum = 0;
    private void ClickToAddMoreCoins(object sender, RoutedEventArgs e)
    {
        if (sum+(int) (InputTextBox.Text) > 30)
        {
            //Removing last coin in case number of coins exceeds 30
            CoinListBox.Items.RemoveAt(CoinListBox.Items.Count - 1);
            MessageBoxResult answer = MessageBox.Show("You cannot enter more than 30 coins. Do you want to end?", "Message", MessageBoxButton.YesNo, MessageBoxImage.Question);
            if (answer == MessageBoxResult.Yes)
            {
                Application.Current.Shutdown();
            }
        }     
        //Hides InputBox and takes input text from user.
        InputBox.Visibility = System.Windows.Visibility.Collapsed;

        // Ensuring that input from user is a integer number

        string number = InputTextBox.Text;
        int num;
        if(int.TryParse(number,out num))
        {
            sum += num;
          try { CoinListBox.Items.RemoveAt(0); 
              } catch
       {}
    CoinListBox.Items.Add(sum);
        }

        InputTextBox.Text = string.Empty;

    }

Goodluck.

Sign up to request clarification or add additional context in comments.

4 Comments

@Henry if i helped you please mark my answer :) thanks
your point of view was good, but still didn't solve my issue. I want ONLY one sum of the numbers to be shown NOT all the sums you get what I mean? basically if the user enters 4 and then 5, the sum should be 9, then if users enters 6, the the NEW sum should be 15 and 15 should be the ONLY SUM that needs to be in the window.
@Henry edited my answer. Please mark if I helped you ;)
your code erased the checking I had when the sum is over 30. Let's say the user entered 20, 4, 5 the total sum is 29 right? but then if the user enters 7 the 7 SHOULD NOT be added...and we should still see the number 29 on the screen...
1
  1. Slashy's answer is incomplete in that it allows negative number to be added.

  2. If you are setting InputTextBox's visibility to Collapsed, then how you are getting user input ? and what's the point of using InputTextBox ?

  3. If you want to show only sum, then you can hide the ListBox by setting its visibility to Collapsed. So, now your code should look like :

    ...
    CoinListBox.Visibility = System.Windows.Visibility.Collapsed;
    ...
    sum = CoinListBox.Items.Cast<object>().Sum(x => Convert.ToInt32(x));
                if (sum > 30)
                {
                    sum -= result; // removing excess coin
    
                    //Removing last coin in case number of coins exceeds 30
                    CoinListBox.Items.RemoveAt(CoinListBox.Items.Count - 1);
                    MessageBoxResult answer = MessageBox.Show("You cannot enter more than 30 coins. Do you want to end?", "Message", MessageBoxButton.YesNo, MessageBoxImage.Question);
                    if (answer == MessageBoxResult.Yes)
                    {
                        Application.Current.Shutdown();
                    }
                }
    
  4. A better version of your code which should check for negative numbers too would be like :

        List<int> coinList = new List<int>();
        private void ClickToAddMoreCoins2(object sender, RoutedEventArgs e)
        {
            int sum = 0;
    
    //Hides InputBox and takes input text from user.
    //InputTextBox.Visibility = System.Windows.Visibility.Collapsed;
    
    // Ensuring that input from user is a integer number
    String input = InputTextBox.Text;
    int result = 0;
    if (int.TryParse(input, out result) && result > 0)
    {
        //Adding number of coins to CoinListBox
        coinList.Add(result);
    }
    else
    {
        MessageBox.Show("Please enter a valid number of coins");
    }
    sum = coinList.Sum();
    if (sum > 30)
    {
        sum -= result;
        //Removing last coin in case number of coins exceeds 30
        coinList.RemoveAt(coinList.Count - 1);
        MessageBoxResult answer = MessageBox.Show("You cannot enter more than 30 coins. Do you want to end?", "Message", MessageBoxButton.YesNo, MessageBoxImage.Question);
        if (answer == MessageBoxResult.Yes)
        {
            Application.Current.Shutdown();
        }
    }
    
    tbSum.Text = "Sum = " + sum.ToString();
    
    // Resets InputBox.
    InputTextBox.Text = String.Empty;
    
    InputTextBox.Focus();
        }
    

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.