I have a checkedListBox and a panel. They are both created by code. Now I want to make an event that by checking and Unchecking an item of the checkedListBox items this panel gets enabled or disable. I have the following code but It doesn't work and throws exception on run time.
CheckedListBox chlb = new CheckedListBox();
for (int i = 0; i < dr.Count(); i++)
{
chlb.Items.Add( dr[i]["Value_Name"].ToString());
if ((bool)dr[i]["HasText"] == true)
{
Panel pnltxt = new Panel();
pnltxt.Size = new Size(630, 30);
chlb.SelectedIndexChanged += (s, argx) => pnltxt.Enabled =
(chlb.GetItemCheckState(i).ToString().Trim() == "Unchecked" ? false : true);
}
}
Error message is:
Additional information: InvalidArgument=Value of '7' is not valid for 'index'. + chlb.GetItemCheckState(i) 'chlb.GetItemCheckState(i)' threw an exception of type 'System.ArgumentOutOfRangeException' System.Windows.Forms.CheckState {System.ArgumentOutOfRangeException}
Can you please help me how to write the correct code for this event.
dr.Count()is7?chlb.GetItemCheckState(i).ToString().Trim() == "Unchecked" ? false : truetochlb.GetItemCheckState(i) != CheckState.Unchecked