Domanda

Questa funzione è chiamata da form_onload. Fondamentalmente sto leggendo il registro, determinando quali caselle di controllo sono contrassegnate e riflettendo che nella GUI.

C'è un modo per condensare questo e scrivere un codice migliore? Che ne dici di usare la proprietà CheckState?

Grazie.

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;
        }
    }
È stato utile?

Soluzione

Beh, potresti almeno restringerlo a:

chkbxExcelCC.Checked = _excelCC.Equals("Checked");

In questo modo si evitano tutte le istruzioni if ??/ else.

Altri suggerimenti

Puoi rimuovere tutte le condizioni if ??/ else non necessarie posizionando la condizione in linea con l'assegnazione. Puoi anche rendere costante il percorso chiave principale. Tuttavia, per semplificarlo davvero, puoi prendere la logica ripetuta di cercare una chiave e confrontarla con " Controllato " e inseriscilo in un metodo separato:

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");
}

È possibile posizionare tutte le caselle di controllo su un pannello e impostare la proprietà Tag di ciascuna casella di controllo sulla chiave corrispondente nel registro. Quindi è possibile utilizzare questo codice nell'evento Load del modulo:

foreach (CheckBox cb in panel1.Controls)
{
    cb.Checked = ((string)Registry.GetValue(RegPath, 
        (string)cb.Tag, "Unchecked") == "Checked");
}

Questo ha lo svantaggio di non essere immediatamente ovvio per un futuro programmatore di manutenzione, quindi aggiungerei un commento dettagliato qui mirato alla tua eventuale sostituzione.

Potresti anche rinunciare al Tag e nominare ogni CheckBox con qualunque sia la chiave corrispondente, quindi utilizzare " cb.Name " invece di " (stringa) cb.Tag " ;. A nessuno piace più la notazione ungherese, comunque.

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");
    }

Usa la notazione abbreviata per if else

chkbxExcelEnable.Checked = _excelEnable == "Checked" ? true: false;
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top