简体   繁体   English

C#中的清单框

[英]Check list Box in C#

I have written program below with checkbox. 我在下面用复选框编写了程序。 which works fine. 效果很好。 but i want to write it in one checklistbox but don't know how to check 1st checklistbox is checked or 2nd. 但我想将其写在一个清单中,但不知道如何检查第一个清单或第二个清单。

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

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

        private void button1_Click(object sender, EventArgs e)
        {
            if (checkBox1.Checked)
                MessageBox.Show("CheckBox1 is checked");
            if (checkBox2.Checked)
                MessageBox.Show("CheckBox2 is checked");
        }
    }
}

Edit:it is a windows form application. 编辑:这是Windows窗体应用程序。

Try This: 尝试这个:

foreach (var item in checkedListBox1.CheckedItems)
{
    /* iterate over all checked items from the checkedlistbox */
    MessageBox.Show(item.ToString());
}

EDIT: if you want to perform some operations based on the selected item Try This: 编辑:如果要基于所选项目执行某些操作,请尝试以下操作:

foreach (int item in checkedListBox1.CheckedIndices)
{
    switch (item)
    {
        case 0:/*first item selected here do something*/
            MessageBox.Show("1st item selected");
            break;

        case 1:/*first item selected here do something*/
            MessageBox.Show("2nd item selected");
            break;

        case 2:/*first item selected here do something*/
            MessageBox.Show("3rd item selected");
            break;
    }
}

Explanation: 说明:

From MSDN: CheckedListBox.CheckedIndices Property 从MSDN: CheckedListBox.CheckedIndices Property

Collection of checked indexes in this CheckedListBox. 此CheckedListBox中已检查索引的集合。

The collection of checked indexes is a subset of the indexes into the collection of all items in the CheckedListBox control 已检查索引的集合是CheckedListBox控件中所有项的集合的索引的子集

SelectedIndices returns you the selectedindex values from the Checkedlistbox . SelectedIndicesCheckedlistbox返回您selectedindexCheckedlistbox值。 for example if there are 10 items in checkedlistbox and if you select item 1,item5 and item7 then it returns their respective index values(index always start with 0). 例如,如果checkedlistbox有10个项目,并且选择了项目1,项目5和项目7,则它将返回它们各自的index值(索引始终以0开头)。

so it returns 0,4,6. 因此它返回0,4,6。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM