Winform窗体学习笔记 第十篇 CheckedListBox类

1. 显示一个 ListBox,其中在每项的左边显示一个复选框。

2. CheckedListBox 属性:

  • CheckOnClick:true时,点击即选中,false,要点两下才选中。
  • CheckedIndices:选中索引的集合,int集合。
  • CheckedItems:选中项的集合,string集合。

3. CheckedListBox 方法:

  • <bool> GetItemChecked(int index):返回指示指定项是否选中的值。
  • <CheckState> GetItemCheckState(int index):返回指示指定项是否选中的值。(checked,unchecked,Indeterminate)
  • SetItemChecked(int index, bool check):指定项是否选中。
  • SetItemCheckState(int index, CheckState):指定项选中,三种状态。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

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

         // Add a tenth element to the CheckedListBox.
         this.checkedListBoxPossibleValue.Items.Add("Ten");
      }

      private void buttonMove_Click(object sender, EventArgs e)
      {
         // Check if there are any checked items in the CheckedListBox.
         if (this.checkedListBoxPossibleValue.CheckedItems.Count > 0)
         {
            // Clear the ListBox we'll move the selections to
            this.listBoxSelected.Items.Clear();

             foreach (int i in checkedListBoxPossibleValue.CheckedIndices)
             {
                 MessageBox.Show(i.ToString());
             }

            // Loop through the CheckedItems collection of the CheckedListBox
            // and add the items in the Selected ListBox
            foreach (string item in this.checkedListBoxPossibleValue.CheckedItems)
            {
               this.listBoxSelected.Items.Add(item.ToString());
            }

            // Clear all the checks in the CheckedListBox
            for (int i = 0; i < this.checkedListBoxPossibleValue.Items.Count; i++)
               this.checkedListBoxPossibleValue.SetItemChecked(i, false);
         }
      }
   }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值