简体   繁体   中英

Visual C#, how can I set SetItemChecked property using Control?

I have a form which contains several CheckedListBox items, along with other controls. I'm trying to loop through each the controls and set its property values. Unfortunately, the SetItemChecked property is is not available in the Control class, so I can't figure out how to manipulate the Checked state of the control.

Here's what I have so far:

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;
  }
}

You can cast the Control into a CheckedListBox Like this:

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

I'm not sure of this one, but it might work also:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM