获取复选框列表文本

时间:2019-09-03 10:43:49

标签: c#

嗨,我想将Checked List Box值(文本)保存在变量中。我该怎么办?

     private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
    {
        int a = checkedListBox1.;
        checkedListBox1.Hide();
        label1.Text = a.ToString() ;

    }

1 个答案:

答案 0 :(得分:1)

让我们尝试下面的代码。

List<string> checkedItems = new List<string>();
        foreach (var item in checkedListBox1.CheckedItems)
            checkedItems.Add(item.ToString());

        if (e.NewValue == CheckState.Checked)
            checkedItems.Add(checkedListBox1.Items[e.Index].ToString());
        else
            checkedItems.Remove(checkedListBox1.Items[e.Index].ToString());

         StringBuilder builder = new StringBuilder();
        foreach (string item in checkedItems)
        {
             builder.Append(item).Append("|");
        }
       label1.Text = builder.ToString();