从form_onload调用此函数。我基本上是在阅读注册表,确定哪些复选框已被勾选,然后在GUI中反映出来。
有没有办法压缩这个并编写更好的代码?如何使用CheckState属性?
感谢。
木质
private void checkExcelSettings()
{
// Read what the values are for the checkboxes first and assign them to a string.
string _excelEnable = Convert.ToString(Registry.GetValue(@"HKEY_CURRENT_USER\Software\Mask Data\", "ExcelEnableHash", "Unchecked"));
string _excelSSN = Convert.ToString(Registry.GetValue(@"HKEY_CURRENT_USER\Software\Mask Data\", "ExcelSSNHash", "Unchecked"));
string _excelCC = Convert.ToString(Registry.GetValue(@"HKEY_CURRENT_USER\Software\Mask Data\", "ExcelCCHash", "Unchecked"));
string _excelWells = Convert.ToString(Registry.GetValue(@"HKEY_CURRENT_USER\Software\Mask Data\", "ExcelWellsHash", "Unchecked"));
string s=@"t\""; //unimportant no-op to placate stackoverflow syntax highlighter.
// Now let's make sure we reflect what they are supposed to be in the GUI.
if (_excelEnable == "Checked")
{
chkbxExcelEnable.Checked = true;
}
else
{
chkbxExcelEnable.Checked = false;
}
if (_excelSSN == "Checked")
{
chkbxExcelSSN.Checked = true;
}
else
{
chkbxExcelSSN.Checked = false;
}
if (_excelCC == "Checked")
{
chkbxExcelCC.Checked = true;
}
else
{
chkbxExcelCC.Checked = false;
}
if (_excelWells == "Checked")
{
chkbxExcelWellsFargo.Checked = true;
}
else
{
chkbxExcelWellsFargo.Checked = false;
}
}
答案 0 :(得分:11)
嗯,你至少可以把它缩小到:
chkbxExcelCC.Checked = _excelCC.Equals("Checked");
这样就可以避免所有的if / else语句。
答案 1 :(得分:8)
您可以通过将条件与赋值内联放置来删除所有不必要的if / else条件。您还可以使主键路径保持不变。但是,要真正简化它,您可以采用重复的逻辑来查找密钥并与“已检查”进行比较并将其放入单独的方法中:
private void checkExcelSettings()
{
// Now let's make sure we reflect what they are supposed to be in the GUI.
chkbxExcelEnable.Checked = IsChecked("ExcelEnableHash");
chkbxExcelSSN.Checked = IsChecked("ExcelSSNHash");
chkbxExcelCC.Checked = IsChecked("ExcelCCHash");
chkbxExcelWellsFargo.Checked = IsChecked("ExcelWellsHash");
}
private static bool IsChecked(string regValue)
{
return Convert.ToString(
Registry.GetValue(
@"HKEY_CURRENT_USER\Software\Mask Data\",
regValue,
"Unchecked")) == "Checked";
}
答案 2 :(得分:3)
private void checkExcelSettings()
{
// Read what the values are for the checkboxes first and assign them to a string.
string _excelEnable = Convert.ToString(Registry.GetValue(@"HKEY_CURRENT_USER\Software\Mask Data\", "ExcelEnableHash", "Unchecked"));
string _excelSSN = Convert.ToString(Registry.GetValue(@"HKEY_CURRENT_USER\Software\Mask Data\", "ExcelSSNHash", "Unchecked"));
string _excelCC = Convert.ToString(Registry.GetValue(@"HKEY_CURRENT_USER\Software\Mask Data\", "ExcelCCHash", "Unchecked"));
string _excelWells = Convert.ToString(Registry.GetValue(@"HKEY_CURRENT_USER\Software\Mask Data\", "ExcelWellsHash", "Unchecked"));
// Now let's make sure we reflect what they are supposed to be in the GUI.
chkbxExcelEnable.Checked = (_excelEnable == "Checked");
chkbxExcelSSN.Checked = (_excelSSN == "Checked");
chkbxExcelCC.Checked = (_excelCC == "Checked");
chkbxExcelWellsFargo.Checked = (_excelWells == "Checked");
}
答案 3 :(得分:0)
您可以将所有复选框放在面板上,并将每个复选框的Tag属性设置为注册表中的相应键。然后你可以在表单的Load事件中使用这段代码:
foreach (CheckBox cb in panel1.Controls)
{
cb.Checked = ((string)Registry.GetValue(RegPath,
(string)cb.Tag, "Unchecked") == "Checked");
}
这样做的缺点是对于未来的维护程序员不会立即显而易见,因此我会在此处添加一份详细的评论,针对您的最终替换。
你也可以省去Tag,只需用相应的键命名每个CheckBox,然后用“cb.Name”代替“(string)cb.Tag”。无论如何,没有人喜欢匈牙利的乐谱。
答案 4 :(得分:0)
private void checkExcelSettings()
{
string key = @"HKEY_CURRENT_USER\Software\Mask Data\";
// Read what the values are for the checkboxes first and assign them to a string.
string _excelEnable = Convert.ToString(Registry.GetValue(key, "ExcelEnableHash", "Unchecked"));
string _excelSSN = Convert.ToString(Registry.GetValue(key, "ExcelSSNHash", "Unchecked"));
string _excelCC = Convert.ToString(Registry.GetValue(key, "ExcelCCHash", "Unchecked"));
string _excelWells = Convert.ToString(Registry.GetValue(key, "ExcelWellsHash", "Unchecked"));
string s=@"t\""; //unimportant no-op to placate stackoverflow syntax highlighter.
// Now let's make sure we reflect what they are supposed to be in the GUI.
chkbxExcelEnable.Checked = (_excelEnable == "Checked");
chkbxExcelSSN.Checked = (_excelSSN == "Checked");
chkbxExcelCC.Checked = (_excelCC == "Checked");
chkbxExcelWellsFargo.Checked = (_excelWells == "Checked");
}
答案 5 :(得分:-3)
对if else使用简写符号
chkbxExcelEnable.Checked = _excelEnable == "Checked" ? true: false;