【问题标题】:How to stop adding elements to ListBox in WPF?如何停止向 WPF 中的 ListBox 添加元素?
【发布时间】:2015-12-09 06:44:50
【问题描述】:

一旦数字达到 30 或超过 30(最大容量),我一直在尝试停止添加数字。我的代码运行并添加数字就好了。我的问题是如何阻止它在达到 30 或超过 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.

 }

【问题讨论】:

  • Bind 你的ListBox 到像 List 这样的集合,并根据你的需要限制集合。只需验证您的列表即可。
  • @AbinMathew 怎么样?我对 WPF 真的很陌生 :(...
  • 找到这个Link

标签: c# wpf


【解决方案1】:

我更改了您的代码以使其按您的需要运行。
但在使用代码之前,请考虑通过删除不必要的 cmets 和行来编写干净易读的代码。

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

【讨论】:

  • 您的代码有效,但假设:我添加 4,然后当我单击“单击添加硬币”按钮时,我在文本输入中看到最后一个输入值 (4).. 为什么那??我希望文本输入值是干净的(什么都没有)......我该如何解决这个问题?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-05-25
  • 2017-07-26
  • 1970-01-01
  • 1970-01-01
  • 2012-04-14
  • 1970-01-01
  • 2018-06-06
相关资源
最近更新 更多