2

ここに画像の説明を入力

こんにちは、リストボックスのアイテムを取得する方法と、すべてのチェックリストボックスをクリックして数字を追加してテキストボックスに表示したいときに、まだ混乱しています。たとえば、テキストボックスに表示される 300 を含むチェックリストボックスのインデックス 1 をチェックします。次に、checkboxlist のインデックス 2 に 100 が含まれていることも確認し、400 を表示します。次に、checkboxlist のインデックス 3 に 200 が含まれていることを確認すると、チェックボックスに 600 が表示されます。

私のコード:

namespace ggg
{
public partial class Form1 : Form
{

    public Form1()
    {
        InitializeComponent();


    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    private void checkedListBox1_SelectedIndexChanged(object sender, EventArgs e)
    {

        listBox1.Items.Clear();
        listBox2.Items.Clear();
        textBox1.Clear();
        foreach (string s in checkedListBox1.CheckedItems)
        listBox1.Items.Add(s);
        foreach (int i in checkedListBox1.CheckedIndices)
        {
            if (i == 0)
            {
                listBox2.Items.Add(300);
                decimal total = 300;
                textBox1.Text += total;
            }
            if (i == 1)
            {
                listBox2.Items.Add(100);
                decimal total = 100;
                textBox1.Text += total;
            }
            if (i == 2)
            {
                listBox2.Items.Add(200);
                decimal total = 200;
                textBox1.Text += total;

            }
        }


    }



}
}
4

2 に答える 2

1

リストボックスの項目を this として合計できます。後、合計をテキストボックスに設定できます

        int total = 0;
        for (int i = 0; i < listBox2.Items.Count; i++)
        {
            total = total+ int.Parse(listBox2.Items[i].ToString());
        }
        textBox1.Text = total.ToString();
于 2017-09-11T08:52:25.860 に答える