CheckedListBox.SetItemChecked 方法

将指定索引处的项的 CheckState 设置为 Checked

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

语法

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

instance.SetItemChecked(index, value)
public void SetItemChecked (
    int index,
    bool value
)
public:
void SetItemChecked (
    int index, 
    bool value
)
public void SetItemChecked (
    int index, 
    boolean value
)
public function SetItemChecked (
    index : int, 
    value : boolean
)

参数

  • index
    要为其设置复选状态的项的索引。
  • value
    若要将该项设置为选中,则为 true;否则为 false

异常

异常类型 条件

ArgumentException

指定的 index 小于零。

- 或 -

该索引大于列表中项的计数。

备注

当传递 true 值时,此方法将 CheckState 值设置为 Checkedfalse 的值将 CheckState 设置为 Unchecked

示例

下面的示例枚举 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 命名空间