如何在文本框中的检查表中获取检查项目的数量

本文关键字:检查 项目 获取 检查表 文本 | 更新日期: 2023-09-27 18:27:34

我不想添加检查表框中文本框中选中的项目。但文本框中没有显示任何内容。

   private void button1_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < chklst_scrips.Items.Count; i++)
        {
            if (chklst_scrips.GetItemCheckState(i) == CheckState.Checked)
            {
                for (int j = 0; ;j++ )
                {
                    textBox1.Text = Convert.ToString(j);
                }
            }
        }
    }

如何在文本框中的检查表中获取检查项目的数量

GetItemChecked方法对于从CheckboxList中查找已检查项非常有用。

 for (int i = 0; i < checkedListBox1.Items.Count; i++)
            {
                if (checkedListBox1.GetItemChecked(i))
                {
                    string str = (string)checkedListBox1.Items[i];
                    textBox1.Text += str;
                }
            }

只需创建一个计数器并给它一个初始值0

int counter = 0;

然后,每次选中复选框时递增计数器,如图所示,例如,如果您有一个名为checkBox1:的复选框

private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
    counter++
    //Your code here
}

你需要在你有的所有复选框中增加计数器

如果您正在使用一个名为checkedListBox1的checkedListBox,那么您可以只使用checkedListBox1.CheckedItems.Count并获取选中项目的数量。