C# Caixas de seleção - Posso limpar isso?
-
05-07-2019 - |
Pergunta
Esta função é chamada do form_onload. Estou basicamente lendo o registro, determinando quais caixas de seleção são marcadas com marcas de seleção e, em seguida, refletindo isso na GUI.
Existe alguma maneira de condensar isso e escrever um código melhor? Que tal usar a propriedade CheckState?
Obrigado.
Woody
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;
}
}
Solução
Bem, você poderia pelo menos reduzi -lo a:
chkbxExcelCC.Checked = _excelCC.Equals("Checked");
Dessa forma, você evita todas as instruções IF/else.
Outras dicas
Você pode remover todas as condições desnecessárias se/else, colocando a condição em linha com a atribuição. Você também pode tornar o caminho principal principal uma constante. No entanto, para realmente simplificá -lo, você pode pegar a lógica repetida de procurar uma chave e comparar com "checked" e colocá -la em um método separado:
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");
}
Você pode colocar todas as suas caixas de seleção em um painel e definir a propriedade TAG de cada caixa de seleção para a tecla correspondente no registro. Então você pode usar este código no evento de carga do formulário:
foreach (CheckBox cb in panel1.Controls)
{
cb.Checked = ((string)Registry.GetValue(RegPath,
(string)cb.Tag, "Unchecked") == "Checked");
}
Isso tem a desvantagem de não ser imediatamente óbvio para um futuro programador de manutenção, então eu adicionaria um comentário detalhado aqui direcionado à sua eventual substituição.
Você também pode dispensar a tag e nomear cada caixa de seleção com qualquer tecla correspondente e, em seguida, usar "cb.name" em vez de "(string) cb.tag". Ninguém gosta mais de notação húngara.
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");
}
Use uma notação de mão curta para se mais
chkbxExcelEnable.Checked = _excelEnable == "Checked" ? true: false;