【问题标题】:C# checkboxes - Can I clean this up?C# 复选框 - 我可以清理它吗?
【发布时间】:2009-09-12 01:26:27
【问题描述】:

这个函数是从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;
        }
    }

【问题讨论】:

    标签: c# checkbox properties


    【解决方案1】:

    嗯,你至少可以把范围缩小到:

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

    这样可以避免所有 if/else 语句。

    【讨论】:

      【解决方案2】:

      您可以通过将条件与赋值内联来删除所有不必要的 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";
      }
      

      【讨论】:

        【解决方案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");
        }
        

        【讨论】:

          【解决方案4】:

          您可以将所有复选框放在一个面板上,并将每个复选框的 Tag 属性设置为注册表中的相应键。然后您可以在表单的 Load 事件中使用此代码:

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

          这对未来的维护程序员来说不是很明显的缺点,所以我会在这里添加一个详细的评论,针对你最终的替换。

          您也可以省略标签,只需使用相应的键名命名每个复选框,然后使用“cb.Name”而不是“(string)cb.Tag”。反正没有人喜欢匈牙利符号了。

          【讨论】:

            【解决方案5】:
            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");
                }
            

            【讨论】:

              【解决方案6】:

              if else 使用简写符号

              chkbxExcelEnable.Checked = _excelEnable == "Checked" ? true: false;
              

              【讨论】:

              • 在您的 .aspx 页面上添加一个复选框。在 Load int i = 9 上添加以下代码; CheckBox1.Checked = i == 9 ?真假;并检查。
              • Neil: (_excelEnable == "Checked") 的结果是真还是假,那为什么还要对它做一个条件呢?您的 (i == 9) 示例也是如此。
              • @Jeff Yates,@Charles Bretana:是的,你是对的。感谢您提供信息。
              猜你喜欢
              • 2011-10-16
              • 2011-04-07
              • 1970-01-01
              • 2020-09-10
              • 2010-09-17
              • 1970-01-01
              • 2010-12-23
              • 1970-01-01
              • 2017-08-10
              相关资源
              最近更新 更多