CheckedListBox.SetItemCheckState 方法

设置指定索引处项的复选状态。

**命名空间:**System.Windows.Forms
**程序集:**System.Windows.Forms(在 system.windows.forms.dll 中)

语法

声明
Public Sub SetItemCheckState ( _
    index As Integer, _
    value As CheckState _
)
用法
Dim instance As CheckedListBox
Dim index As Integer
Dim value As CheckState

instance.SetItemCheckState(index, value)
public void SetItemCheckState (
    int index,
    CheckState value
)
public:
void SetItemCheckState (
    int index, 
    CheckState value
)
public void SetItemCheckState (
    int index, 
    CheckState value
)
public function SetItemCheckState (
    index : int, 
    value : CheckState
)

参数

  • index
    要为其设置状态的项的索引。

异常

异常类型 条件

ArgumentOutOfRangeException

指定的 index 小于零。

- 或 -

index 大于或等于列表中的项的计数。

InvalidEnumArgumentException

value 不是 CheckState 值中的一个。

备注

SetItemCheckState 方法引发 ItemCheck 事件。

CheckState 被设置为 Indeterminate 的项在显示时复选框中会带有选中标记,但该框会是灰色的,以指示被选中项的不确定状态。

示例

下面的示例枚举 CheckedListBox 中的项并选中列表中的所有其他项。本示例演示了使用 SetItemCheckStateSetItemChecked 方法设置项的复选状态。对于要选中的所有其他项,调用 SetItemCheckStateCheckState 设置为 Indeterminate,同时对另一项调用 SetItemChecked,将复选状态设置为 Checked

本示例还演示了如何使用 Items 属性获取 CheckedListBox.ObjectCollection,从而获取项目的 Count

Private Sub CheckEveryOther_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckEveryOther.Click
    ' Cycle through every item and check every other.
    Dim i As Integer

    ' Set flag to true to know when this code is being executed. Used in the ItemCheck
    ' event handler.
    insideCheckEveryOther = True

    For i = 0 To CheckedListBox1.Items.Count - 1
        ' For every other item in the list, set as checked.

        If ((i Mod 2) = 0) Then
            ' But for each other item that is to be checked, set as being in an
            ' indeterminate checked state.

            If ((i Mod 4) = 0) Then
                CheckedListBox1.SetItemCheckState(i, CheckState.Indeterminate)
            Else
                CheckedListBox1.SetItemChecked(i, True)
            End If
        End If
    Next

    insideCheckEveryOther = False

End Sub
private void CheckEveryOther_Click(object sender, System.EventArgs e) {
    // Cycle through every item and check every other.

    // Set flag to true to know when this code is being executed. Used in the ItemCheck
    // event handler.
    insideCheckEveryOther = true;

    for (int i = 0; i < checkedListBox1.Items.Count; i++) {
        // For every other item in the list, set as checked.
        if ((i % 2) == 0) {
            // But for each other item that is to be checked, set as being in an
            // indeterminate checked state.
            if ((i % 4) == 0)
                checkedListBox1.SetItemCheckState(i, CheckState.Indeterminate);
            else
                checkedListBox1.SetItemChecked(i, true);
        }
    }        

    insideCheckEveryOther = false;
}
void CheckEveryOther_Click( Object^ /*sender*/, System::EventArgs^ /*e*/ )
{
   
   // Cycle through every item and check every other.
   // Set flag to true to know when this code is being executed. Used in the ItemCheck
   // event handler.
   insideCheckEveryOther = true;
   for ( int i = 0; i < checkedListBox1->Items->Count; i++ )
   {
      
      // For every other item in the list, set as checked.
      if ( (i % 2) == 0 )
      {
         
         // But for each other item that is to be checked, set as being in an
         // indeterminate checked state.
         if ( (i % 4) == 0 )
                     checkedListBox1->SetItemCheckState( i, CheckState::Indeterminate );
         else
                     checkedListBox1->SetItemChecked( i, true );
      }

   }
   insideCheckEveryOther = false;
}

private void checkEveryOther_Click(Object sender, System.EventArgs e)
{
    // Cycle through every item and check every other.
    // Set flag to true to know when this code is being executed. 
    // Used in the ItemCheck event handler.
    insidecheckEveryOther = true;
    for (int i = 0; i < checkedListBox1.get_Items().get_Count(); i++) {
        // For every other item in the list, set as checked.
        if (i % 2 == 0) {
            // But for each other item that is to be checked, set as being 
            // in an indeterminate checked state.
            if (i % 4 == 0) {
                checkedListBox1.SetItemCheckState(i, 
                    CheckState.Indeterminate);
            }
            else {
                checkedListBox1.SetItemChecked(i, true);
            }
        }
    }
    insidecheckEveryOther = false;
} //checkEveryOther_Click

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

请参见

参考

CheckedListBox 类
CheckedListBox 成员
System.Windows.Forms 命名空间
CheckState