// Inside some loop creating a lot of these forms and matching checkboxes
Form frmTemp = frmTestPanels[i]; // New form in array
CheckBox chkSelectPanel; // New checkbox that selects this panel
chkSelectPanel = new CheckBox();
chkSelectPanel.Text = SomeName; // Give checkbox a label
chkSelectPanel.Click += (ss, ee) => // When clicked
{
label1.Text = SomeName; // Update a label
if (chkSelectPanel.Checked) // Show or hide the form
{
frmTemp.Show();
}
else
{
frmTemp.Hide();
}
};
frmTemp.VisibleChanged += (ss, ee) => // When form visibility changes
{
chkSelectPanel.Checked = frmTemp.Visible; // Reflect change to checkbox
ConfigurationFileChanged = true; // Update config file later
};
frmTemp.FormClosing += (ss, ee) => // When the form closes
{ // We're only pretending to close the form - it'll sit around until needed
chkSelectPanel.Checked = false; // Update the checkbox
frmTemp.Hide(); // Hide the form
ee.Cancel = true; // Cancel the close
};
flpSelectGroup.Controls.Add(chkSelectPanel); // Add checkbox to flow layout panel
// End of loop creating a bunch of these matching forms/checkboxes
当然,我遇到了转换错误:
我真的很喜欢动态创建所有内容,然后让对象自己处理的能力——我不需要添加任何特殊函数来找出哪个表单发出关闭事件,这样它就可以搜索正确的复选框并更新复选框——它只是有效的(TM)。