1

初めての投稿です。Visual C# のチェックリスト ボックスで複数の合計を作成しようとしています。各行に1つずつ、108個の数字があり、チェックされたアイテムを残りの各アイテムと合計して、テキストボックスに出力しようとしています。

私はこれをしましたが、それは間違っていると思います。これは実際に合計を計算しますが、数値自体と全体の 108 回も計算します。

チェックボックスに残りの数字を入れてチェックした数字を追​​加したい。

private void button2_Click(object sender, EventArgs e)
{
   foreach(string checkednumber in checkedlistbox1.CheckedItems)
   {
      double x = Convert.ToDouble(checkednumber);

      double a = 0;
      for (double y = 0; y < checkedlistbox1.Items.Count; ++y)
      {
         foreach (string othernumbers in checkedlistbox1.Items)
         {
            double z = Convert.ToDouble(othernumbers);
            sum = x + z;
            string str = Convert.ToString(sum);
            listbox1.Items.Add(str);
         }
      }
   }  
}

助けてくれてありがとう。

4

2 に答える 2

0

また、linq を使用してそれを達成することもできます。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var result = from num in this.checkedListBox1.CheckedItems.OfType<string>()
                         select Convert.ToInt32(num);
            this.textBox1.Text = result.Sum().ToString();
        }
    }
}
于 2013-04-17T13:35:22.133 に答える