HenryDev
HenryDev

Reputation: 4953

How to bind input from user in WPF?

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;
    }

Upvotes: 0

Views: 98

Answers (2)

AnjumSKhan
AnjumSKhan

Reputation: 9827

  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();
        }
    

Upvotes: 1

Slashy
Slashy

Reputation: 1881

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.

Upvotes: 1

Related Questions