c# 如何获取我的列表框项并在我按下检查列表框时快速添加它

标签 c# forms listbox checklistbox

enter image description here

你好,我仍然对此感到困惑,我如何才能获得我的列表框项目以及当我单击每个检查列表框时我想添加数字并显示在文本框中。例如,我检查了包含 300 的检查列表框索引 1,它显示在文本框中。然后我还检查我的复选框列表的索引 2 包含 100,然后它显示 400。然后如果我检查我的复选框列表的索引 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;

            }
        }


    }



}
}

最佳答案

您可以像这样对列表框项目求和。之后,你可以将总数设置到文本框

        int total = 0;
        for (int i = 0; i < listBox2.Items.Count; i++)
        {
            total = total+ int.Parse(listBox2.Items[i].ToString());
        }
        textBox1.Text = total.ToString();

关于c# 如何获取我的列表框项并在我按下检查列表框时快速添加它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46151239/

相关文章:

c# - 如何从 double 转换为 Int64

c# - 通过键入带有 '%' 字符的子字符串来过滤字符串集合

c# - 排序字典时出错

Angular 表单验证如何在提交时显示错误消息

jquery - ToggleClass 基于复选框状态

c# - 滚动到列表框 wp7 的底部

c# - MSMQ 中的多个队列(性能下降)

javascript - 同一页面上的多个 Recaptcha 在提交失败时消失?

wpf - 如何从DataTemplate确定当前ListBox项目的索引?

c# - 将焦点从一个列表框移动到另一个列表框