如何在 WPF 的 ListBox 中找到元素的总和
How can I find the sum of the elements within a ListBox in WPF
如何找到列表框中元素的总和。我只需要找到一种方法来存储 ListBox 的值之和,并在用户插入错误的输入后输出
private void theMethod(object sender, RoutedEventArgs e)
{
// YesButton Clicked! Let's hide our InputBox and handle the input text.
InputBox.Visibility = System.Windows.Visibility.Collapsed;
// Do something with the Input
String input = InputTextBox.Text;
int result = 0;
if (int.TryParse(input, out result))
{
MyListBox.Items.Add(result); // Add Input to our ListBox.
}
else {
String[]arr = new String[3];
// I just want to be able to output the sum of the elements of the ListBox (MyListBox)
for ( i = 0; i < MyListBox.Items.Count; i++)
{
//MyListBox.Items[i].ToString();
MyListBox.Items.Cast<ListBoxItem>().Sum(x => Convert.ToInt32(x)).ToString();
}
sum.ToString();
MessageBox.Show("Sum is: " +MyListBox.Items.Cast<ListBoxItem>().Sum(x => Convert.ToInt32(x)).ToString());
}
你的代码问题在这里:
MyListBox.Items.Cast<ListBoxItem>
要计算列表框中项目的总和,如果您确定它们是作为 int 或 string 添加的整数,您可以使用此代码段:
var sum= this.ListBox1.Items.Cast<object>()
.Select(x => Convert.ToInt32(x))
.Sum();
MessageBox.Show(sum.ToString());
以上代码假定您使用以下代码将项目添加到 ListBox:
var value= this.TextBox1.text;
//your logic for null checking and ...
this.ListBox1.Items.Add(value);
这是我根据你的代码测试的完整代码。
当您向列表框添加整数值时,我们不再需要 Cast<object>
和 Select(x=>Convert.ToInt32(x))
,它足以 Cast<int>
,如下所示:
String input = InputTextBox.Text;
int result = 0;
if (int.TryParse(input, out result))
{
MyListBox.Items.Add(result);
}
else
{
var sum = this.MyListBox.Items.Cast<int>().Sum();
MessageBox.Show(string.Format("Sum is: {0}", sum));
sum.ToString();
}
InputTextBox.Text = String.Empty;
这对我有用,试试这个:
private void YesButton_Click(object sender, RoutedEventArgs e)
{
int sum = 0;
int i = 0;
// YesButton Clicked! Let's hide our InputBox and handle the input text.
InputBox.Visibility = System.Windows.Visibility.Collapsed;
// Do something with the Input
String input = InputTextBox.Text;
int result = 0;
if (int.TryParse(input, out result))
{
MyListBox.Items.Add(result); // Add Input to our ListBox.
}
else
{
sum = MyListBox.Items.Cast<int>().Sum(x => Convert.ToInt32(x));
MessageBox.Show("Sum is: " +sum);
}
// Clear InputBox.
InputTextBox.Text = String.Empty;
}
如何找到列表框中元素的总和。我只需要找到一种方法来存储 ListBox 的值之和,并在用户插入错误的输入后输出
private void theMethod(object sender, RoutedEventArgs e)
{
// YesButton Clicked! Let's hide our InputBox and handle the input text.
InputBox.Visibility = System.Windows.Visibility.Collapsed;
// Do something with the Input
String input = InputTextBox.Text;
int result = 0;
if (int.TryParse(input, out result))
{
MyListBox.Items.Add(result); // Add Input to our ListBox.
}
else {
String[]arr = new String[3];
// I just want to be able to output the sum of the elements of the ListBox (MyListBox)
for ( i = 0; i < MyListBox.Items.Count; i++)
{
//MyListBox.Items[i].ToString();
MyListBox.Items.Cast<ListBoxItem>().Sum(x => Convert.ToInt32(x)).ToString();
}
sum.ToString();
MessageBox.Show("Sum is: " +MyListBox.Items.Cast<ListBoxItem>().Sum(x => Convert.ToInt32(x)).ToString());
}
你的代码问题在这里:
MyListBox.Items.Cast<ListBoxItem>
要计算列表框中项目的总和,如果您确定它们是作为 int 或 string 添加的整数,您可以使用此代码段:
var sum= this.ListBox1.Items.Cast<object>()
.Select(x => Convert.ToInt32(x))
.Sum();
MessageBox.Show(sum.ToString());
以上代码假定您使用以下代码将项目添加到 ListBox:
var value= this.TextBox1.text;
//your logic for null checking and ...
this.ListBox1.Items.Add(value);
这是我根据你的代码测试的完整代码。
当您向列表框添加整数值时,我们不再需要 Cast<object>
和 Select(x=>Convert.ToInt32(x))
,它足以 Cast<int>
,如下所示:
String input = InputTextBox.Text;
int result = 0;
if (int.TryParse(input, out result))
{
MyListBox.Items.Add(result);
}
else
{
var sum = this.MyListBox.Items.Cast<int>().Sum();
MessageBox.Show(string.Format("Sum is: {0}", sum));
sum.ToString();
}
InputTextBox.Text = String.Empty;
这对我有用,试试这个:
private void YesButton_Click(object sender, RoutedEventArgs e)
{
int sum = 0;
int i = 0;
// YesButton Clicked! Let's hide our InputBox and handle the input text.
InputBox.Visibility = System.Windows.Visibility.Collapsed;
// Do something with the Input
String input = InputTextBox.Text;
int result = 0;
if (int.TryParse(input, out result))
{
MyListBox.Items.Add(result); // Add Input to our ListBox.
}
else
{
sum = MyListBox.Items.Cast<int>().Sum(x => Convert.ToInt32(x));
MessageBox.Show("Sum is: " +sum);
}
// Clear InputBox.
InputTextBox.Text = String.Empty;
}