【问题标题】:Visual C#, how can I set SetItemChecked property using Control?Visual C#,如何使用 Control 设置 SetItemChecked 属性?
【发布时间】:2017-11-17 06:31:48
【问题描述】:

我有一个包含多个CheckedListBox 项目以及其他控件的表单。我正在尝试遍历每个控件并设置其属性值。不幸的是,SetItemChecked 属性在Control 类中不可用,所以我不知道如何操作控件的Checked 状态。

这是我目前所拥有的:

for (int i = 0; i < Controls.Count(); i++) {
  switch(Controls[i].GetType().ToString()) {
    case "System.Windows.Forms.TextBox":
    case "System.Windows.Forms.RichTextBox":
      Controls[i].Text=i.ToString();
        break;
    case "System.Windows.Forms.CheckedListBox":
      Controls[i].SetItemChecked(0,true);
      // ^^ This line doesn't work, because SetItemChecked is not available
      break;
    default:
      Controls[i].Tag=i;
      break;
  }
}

【问题讨论】:

  • 感谢 Alford 先生的编辑!

标签: c# forms controls checkedlistbox


【解决方案1】:

您可以像这样将 Control 转换为 CheckedListBox:

(Controls[i] as CheckedListBox).SetItemChecked(0,true);

我不确定这个,但它也可能有效:

CheckedListBox myCbList= (ChecekdListBox) Controls[i];
myCbList.SetItemChecked(0,true)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-02-11
    • 2011-04-06
    • 1970-01-01
    • 2010-10-18
    • 2010-10-13
    • 1970-01-01
    相关资源
    最近更新 更多