0

I have been trying to stop adding numbers once it hits 30 or goes over 30 (max capacity). My code runs and adds numbers just fine. My issue is how to stop it from getting more numbers once it hits 30 or goes more than 30.

     private void ClickToAddMoreCoins(object sender, RoutedEventArgs e)
    {

        //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;
        var result = 0;
        if (int.TryParse(input, out result))
        {
            //Adding number of coins to CoinListBox
            //CoinListBox.Items.Add(result);

            sum += result;
            try
            {
                CoinListBox.Items.RemoveAt(0);
            }
            catch
            { }
            CoinListBox.Items.Add(sum);
        }
        else
        {
            MessageBox.Show("Please enter a number of coins");
        }
        //sum = CoinListBox.Items.Cast<object>().Sum(x => Convert.ToInt32(x));    
        if(sum > 30)
        { 
            //CoinListBox.Items.Add
            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;
    }
    //This method hides InputBox.

 }
halfer
  • 19,824
  • 17
  • 99
  • 186
Devmix
  • 1,599
  • 5
  • 36
  • 73

1 Answers1

0

I changed your code to make it work like you need.
But before using the code, consider writing clean and readable codes by removing unnecessary comments and lines.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WPF_Demo
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        //This method handles the "Click to add new coins button"
        private void AddNewCoins(object sender, RoutedEventArgs e)
        {
            InputBox.Visibility = System.Windows.Visibility.Visible;
        }
        //This method handles input text entered by user
        int sum = 0;
        private void ClickToAddMoreCoins(object sender, RoutedEventArgs e)
        {

            //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;
            var result = 0;
            if (int.TryParse(input, out result))
            {
                //Adding number of coins to CoinListBox
                //CoinListBox.Items.Add(result);
                //////////////////////////////////////////////////////////////////

                //sum = CoinListBox.Items.Cast<object>().Sum(x => Convert.ToInt32(x));    
                if (sum + result > 30)
                {
                    //CoinListBox.Items.Add
                    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();
                    }
                }
                else
                {
                    // Resets InputBox.
                    sum += result;
                    try
                    {
                        CoinListBox.Items.RemoveAt(0);
                    }
                    catch
                    { }
                    CoinListBox.Items.Add(sum);
                    //////////////////////////////////////////////////////////////////
                }

            }
            else
            {
                MessageBox.Show("Please enter a number of coins");
            }
            InputTextBox.Text = String.Empty;
        }
        //This method hides InputBox.
        private void Cancel_Button_Click(object sender, RoutedEventArgs e)
        {
            //Hides InputBox.
            InputBox.Visibility = System.Windows.Visibility.Collapsed;

            // Resets InputBox.
            InputTextBox.Text = String.Empty;
        }
    }
}
Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
  • your code works, but let's say: I add 4 and then when I click on the "click to add coins" button I see the last input value (4 )in the text input.. why is that?? I want the text input value to be clean (nothing)... How can I fix that? – Devmix Sep 13 '15 at 00:20