如何查找ListBox中元素的总和。我只需要找到一种方法来存储列表框和输出的值的总和,一旦用户插入错误的输入如何在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());
}
你是什么意思是“当用户输入任何内容,但数字应用程序显示目前输入的项目的总和” –
是不是像我已经添加文字124E,它应该显示我7? –
你在这里得到错误吗? –