【问题标题】:avoid rewriting the same codes c#避免重写相同的代码c#
【发布时间】:2021-07-13 14:13:47
【问题描述】:

我有一个使用 TableLayoutPanel 控件的 winform 项目。每个 TableLayoutPanel 单元格都有面板。这些面板有 1 个。文本框

我使用下面的代码来访问文本框。

例如;

1-) 要更改文本框的颜色,我创建了 makeColorofTexboxesYellow() 方法。

2-) 为了使文本框为空,我创建了 makeTexboxesEmpty() 方法。

如您所见;在到达文本框之前,方法中有相同的代码。

private void makeColorofTexboxesYellow()
{
 for (int i = start; i < end; i++)
     {
                      //tlp1 is name of tablelayoutpanel
     Panel pnl = (Panel)tlp1.GetControlFromPosition(i, textboxOwnerIndex + 1);
     foreach (Control t in pnl.Controls)
        {
        if (t is TextBox)
           {
           if ((t as TextBox).Tag == null)
              {
            // until here, all codes with same as following method (makeTexboxesEmpty())
                 ((t as TextBox).BackColor=Color.Yellow;
              }
           }
        }
      }
 }
private void makeTexboxesEmpty()
 {
 for (int i = start; i < end; i++)
     {
      // tlp is name of tablelayoutpanel
       Panel pnl = (Panel)tlp1.GetControlFromPosition(i, textboxOwnerIndex + 1);
       foreach (Control t in pnl.Controls)
          {
               if (t is TextBox)
                  {
                  if ((t as TextBox).Tag == null)
                     {    
                      ((t as TextBox).Text=""; 
                     }
                  }
          }
     }
}

如何避免重写这些代码。解决方案应该是这样的。

(我保持代码简短,以举例说明 for-ex ((t as TextBox).Text="";

private void commonMethot()
{
 for (int i = start; i < end; i++)
  {
   //tlp1 is name of tablelayoutpanel
     Panel pnl = (Panel)tlp1.GetControlFromPosition(i, textboxOwnerIndex + 1);
   foreach (Control t in pnl.Controls)
     {
       if (t is TextBox)
          {
            if ((t as TextBox).Tag == null)
               {
                 // call methot here
                makeTexboxesBackColorYellow(t as TextBox) ;
                 //or
                makeTexboxesEmty(t as TextBox);

                }
          }
      }
   }
}


private void makeTexboxesBackColorYellow(Textbox t)
   {
   t.BackColor=Color.Yellow;
   }

private void makeTexboxesEmty(Textbox t)
    {
    t.Text="";
    }

但我无法解决这个问题。请帮忙。

编辑:

对不起,这是我的错。我必须更具体。

t.BackColor=Color.Yellow;

只是一个例子。这个领域有很多动作。 我添加了图片以进行更多解释。请看。

【问题讨论】:

  • 你的最后一个标签似乎完全关闭了。 - 还请修复格式以使代码可读而无需 sceolling
  • commonMethod(Action&lt;TextBox&gt; action) ... action(t as Textbox) .... `commonMethod(t=&gt;t.BackColor=Color.Yellow);commonMethod(t=&gt;t.Text=string.Empty); ... 或类似版本的代理
  • 与代表:delegate void DoSomthingWithTextBox(TextBox t); ... commonMethod(DoSomthingWithTextBox action) ... action(t as Textbox) 然后commonMethod(t=&gt;t.BackColor=Color.Yellow); 甚至commonMethod(makeTexboxesBackColorYellow);
  • 为什么不直接继承Textbox,把你的条件放进去,然后在你需要的地方使用它呢?恕我直言,这是一个简单任务不需要的所有膨胀。触发这些例程以更改文本框背景颜色的条件是什么?
  • 在commin方法中添加参数:commonMethot(Color color)

标签: c# methods


【解决方案1】:

可能最简单的方法是创建一个方法,该方法包含循环所有文本框的代码并接受一个参数,该参数将更改该方法是清空文本框还是将背景更改为黄色。例如:

private void loopThroughTextBoxes(string action)
{
    for (int i = start; i < end; i++)
    {
        //tlp1 is name of tablelayoutpanel
        Panel pnl = (Panel)tlp1.GetControlFromPosition(i, textboxOwnerIndex + 1);
        foreach (Control t in pnl.Controls)
        {
            if (t is TextBox)
            {
                if ((t as TextBox).Tag == null)
                {
                    if(action == "yellow")
                    {
                        (t as TextBox).BackColor=Color.Yellow;
                    }
                    else if(action == "empty")
                    {
                        (t as TextBox).Text = "";
                    }
                }
            }
        }
    }
}

现在您可以像这样调用该方法: loopThroughTextBoxes("yellow") 或者 loopThroughTextBoxes("empty") 并且行为会根据您传递的值而有所不同。

您还可以探索方法重载或可选参数来做类似的事情。

【讨论】:

    【解决方案2】:
    private void commonMethod<T>(Action<T> action) where T : Control
    {
        for (int i = start; i < end; i++)
        {
            Panel pnl = (Panel)tlp1.GetControlFromPosition(i, textboxOwnerIndex + 1);
            foreach (Control c in pnl.Controls)
            {
                if (c is T t)
                {
                    if (t.Tag == null)
                    {
                       // call method here
                       action(t);
                    }
                }
            }
        }
    }
    

    像这样使用它:

    commonMethod((TextBox t) => t.BackColor = Color.Yellow);
    
    commonMethod((TextBox t) => t.Text = "");
    

    或者,如果更复杂,将代码移动到方法中:

    commonMethod<TextBox>(complicatedStuff);
    

    然后

    private void complicatedStuff(TextBox t)
    {
        ...
    }
    

    您还可以将TextBox 替换为其他控件类型,例如ButtonLabelRadioButton 等。

    【讨论】:

      猜你喜欢
      • 2019-02-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多