110,533
社区成员
发帖
与我相关
我的任务
分享
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public Form1(SelectedValues selectedValues) : this()
{
this.cb_test.Checked = selectedValues.isTest;
foreach (Control c in this.Controls)
{
if (c is CheckBox checkBox)
{
foreach (var prop in typeof(SelectedValues).GetProperties())
{
if (checkBox.Name == "cb_" + prop.Name)
{
checkBox.Checked = (bool)prop.GetValue(selectedValues);
break;
}
}
}
}
}
}
public class SelectedValues
{
public bool isTest { get; set; } = false;
}
public enum CKName_e
{
GuidelinesSingle,
FunctionCheck,
Test,
Breakfast,
}
public class SelectPrintType
{
public SelectPrintType(Dictionary<CKName_e, bool> dictCkChecked)
{
this._dictCkChecked = dictCkChecked;
}
Dictionary<CKName_e, bool> _dictCkChecked = new Dictionary<CKName_e, bool>();
}
窗体里面:
Dictionary<CKName_e, bool> _dictCkChecked = new Dictionary<CKName_e, bool>();
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
CheckBox ck = sender as CheckBox;
string enumName = ck.Name.Substring(3);
CKName_e cKName;
if (Enum.TryParse(enumName, out cKName))
{
_dictCkChecked[cKName] = ck.Checked;
}
}
Dictionary<string, bool> initialValues = new Dictionary<string, bool>();