当前位置: 首页>>代码示例>>C#>>正文


C# CheckedListBox.GetItemCheckState方法代码示例

本文整理汇总了C#中System.Windows.Forms.CheckedListBox.GetItemCheckState方法的典型用法代码示例。如果您正苦于以下问题:C# CheckedListBox.GetItemCheckState方法的具体用法?C# CheckedListBox.GetItemCheckState怎么用?C# CheckedListBox.GetItemCheckState使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Windows.Forms.CheckedListBox的用法示例。


在下文中一共展示了CheckedListBox.GetItemCheckState方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: WhatIsChecked_Click

private void WhatIsChecked_Click(object sender, System.EventArgs e) {
    // Display in a message box all the items that are checked.

    // First show the index and check state of all selected items.
    foreach(int indexChecked in checkedListBox1.CheckedIndices) {
        // The indexChecked variable contains the index of the item.
        MessageBox.Show("Index#: " + indexChecked.ToString() + ", is checked. Checked state is:" +
                        checkedListBox1.GetItemCheckState(indexChecked).ToString() + ".");
    }

    // Next show the object title and check state for each item selected.
    foreach(object itemChecked in checkedListBox1.CheckedItems) {

        // Use the IndexOf method to get the index of an item.
        MessageBox.Show("Item with title: \"" + itemChecked.ToString() + 
                        "\", is checked. Checked state is: " + 
                        checkedListBox1.GetItemCheckState(checkedListBox1.Items.IndexOf(itemChecked)).ToString() + ".");
    }
}
开发者ID:.NET开发者,项目名称:System.Windows.Forms,代码行数:19,代码来源:CheckedListBox.GetItemCheckState

示例2: CheckedListBoxes

//引入命名空间
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;

public class CheckedListBoxes : Form
{
  CheckedListBox clb;
  Button btnToggle;
  Button btnClear;
  String str;

  public CheckedListBoxes()
  {
    Size = new Size(300,400);
    this.Load += new EventHandler(this_Load);

    clb = new CheckedListBox();
    clb.Parent = this;
    clb.Location = new Point(10,10);
    clb.Size = new Size(ClientSize.Width - 20, Height - 240);
    clb.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Bottom;
    clb.BorderStyle = BorderStyle.Fixed3D;
    clb.MultiColumn = true;
    clb.ScrollAlwaysVisible = true;
    clb.ThreeDCheckBoxes = true;  
    clb.CheckOnClick = true;
    clb.ItemCheck += new ItemCheckEventHandler(clb_ItemCheck);

    btnToggle = new Button();
    btnToggle.Parent = this;
    btnToggle.Text = "Toggle Indeterminate";
    btnToggle.Size = new Size((int)(Font.Height * .75) * btnToggle.Text.Length, Font.Height + 10);
    btnToggle.Location = new Point( clb.Left, clb.Bottom + 10);
    btnToggle.Anchor = AnchorStyles.Left | AnchorStyles.Bottom;
    btnToggle.Click += new System.EventHandler(btnToggle_Click);

    btnClear = new Button();
    btnClear.Parent = this;
    btnClear.Text = "Clear All";
    btnClear.Size = new Size((int)(Font.Height * .75) * btnClear.Text.Length, Font.Height + 10);
    btnClear.Location = new Point(btnToggle.Left, btnToggle.Bottom + 10);
    btnClear.Anchor = AnchorStyles.Left | AnchorStyles.Bottom;
    btnClear.Click += new System.EventHandler(btnClear_Click);

    clb.Items.Add("12345");
      clb.Items.Add("67890");      
      clb.Items.Add("7890");      
      clb.Items.Add("890");            
  }  

  static void Main() 
  {
    Application.Run(new CheckedListBoxes());
  }

  private void btnToggle_Click(object sender, EventArgs e)
  {
    for (int i = 0; i <= (clb.Items.Count - 1); i++)
    {
      if (clb.GetItemCheckState(i) == CheckState.Checked)
      {
        clb.SetItemCheckState(i, CheckState.Indeterminate);  
      }
      else if (clb.GetItemCheckState(i) == CheckState.Indeterminate)
      {
        clb.SetItemCheckState(i, CheckState.Checked);  
      }
    }  
  }

  private void btnClear_Click(object sender, EventArgs e)
  {
    clb.ClearSelected();

    for (int i = 0; i <= (clb.Items.Count - 1); i++)
    {
      clb.SetItemChecked(i, false);  
    }
  }

  private void clb_ItemCheck(object sender, ItemCheckEventArgs e)
  {
    Console.WriteLine("Current Item:" + clb.GetItemText(clb.Items[e.Index]));
    Console.WriteLine("Current Index:" + e.Index.ToString() );
    Console.WriteLine("Current Value:" + e.CurrentValue.ToString() );
    Console.WriteLine("New Value:" + e.NewValue.ToString() );
  }

  private void clb_SelectedIndexChanged(object sender, EventArgs e)
  {
    Console.WriteLine("Selected Items:" + clb.SelectedItems.Count.ToString() );
    Console.WriteLine("Checked Items:" + clb.CheckedItems.Count.ToString() );
  }

  private void this_Load(object sender, EventArgs e)
  {
    clb.SelectedIndexChanged += new EventHandler(clb_SelectedIndexChanged);
  }
}
开发者ID:C#程序员,项目名称:System.Windows.Forms,代码行数:102,代码来源:CheckedListBox.GetItemCheckState


注:本文中的System.Windows.Forms.CheckedListBox.GetItemCheckState方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。