C# Checkboxes- 정리 할 수 있습니까?
-
05-07-2019 - |
문제
이 기능은 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;
}
}
해결책
글쎄, 당신은 적어도 그것을 좁힐 수 있습니다.
chkbxExcelCC.Checked = _excelCC.Equals("Checked");
이렇게하면 모든 IF/Else 문을 피합니다.
다른 팁
할당과 함께 조건을 인라인으로 배치하여 모든 불필요한 IF/기타 조건을 제거 할 수 있습니다. 메인 키 경로를 일정하게 만들 수도 있습니다. 그러나 실제로 단순화하려면 키를 찾고 "확인 된"과 비교하는 반복적 인 논리를 가져 와서 별도의 방법으로 넣을 수 있습니다.
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";
}
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");
}
모든 확인란을 패널에 배치하고 각 확인란의 태그 속성을 레지스트리의 해당 키로 설정할 수 있습니다. 그런 다음 양식로드 이벤트 에서이 코드를 사용할 수 있습니다.
foreach (CheckBox cb in panel1.Controls)
{
cb.Checked = ((string)Registry.GetValue(RegPath,
(string)cb.Tag, "Unchecked") == "Checked");
}
이것은 향후 유지 보수 프로그래머에게 즉시 명백하지 않다는 단점이 있으므로 최종 교체품을 대상으로하는 장황한 의견을 추가 할 것입니다.
또한 태그를 분배하고 해당 키가 무엇이든간에 각 확인란의 이름을 지은 다음 "(문자열) CB.tag"대신 "CB.Name"을 사용 할 수 있습니다. 어쨌든 아무도 헝가리 표기법을 좋아하지 않습니다.
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");
}
다른 경우 짧은 손 표고를 사용하십시오
chkbxExcelEnable.Checked = _excelEnable == "Checked" ? true: false;
제휴하지 않습니다 StackOverflow