简化C#代码,使其更易于管理

时间:2014-01-11 13:43:15

标签: c#

我如何简化以下代码,以便随着时间的推移更容易管理?

    private void btn1_Click(object sender, EventArgs e)
    {
        if(remainTxt.BackColor == Color.FromArgb(245, 244, 162))
        {
            remainTxt.AppendText("1");
        }
        else if (totalTxt.BackColor == Color.FromArgb(245, 244, 162))
        {
            totalTxt.AppendText("1");
        }
        else if (paidTxt.BackColor == Color.FromArgb(245, 244, 162))
        {
            paidTxt.AppendText("1");
        }
    }

    private void btn2_Click(object sender, EventArgs e)
    {
        if (remainTxt.BackColor == Color.FromArgb(245, 244, 162))
        {
            remainTxt.AppendText("2");
        }
        else if (totalTxt.BackColor == Color.FromArgb(245, 244, 162))
        {
            totalTxt.AppendText("2");
        }
        else if (paidTxt.BackColor == Color.FromArgb(245, 244, 162))
        {
            paidTxt.AppendText("2");
        }
    }

    private void btn3_Click(object sender, EventArgs e)
    {
        if (remainTxt.BackColor == Color.FromArgb(245, 244, 162))
        {
            remainTxt.AppendText("3");
        }
        else if (totalTxt.BackColor == Color.FromArgb(245, 244, 162))
        {
            totalTxt.AppendText("3");
        }
        else if (paidTxt.BackColor == Color.FromArgb(245, 244, 162))
        {
            paidTxt.AppendText("3");
        }
    }

    private void btn4_Click(object sender, EventArgs e)
    {
        if (remainTxt.BackColor == Color.FromArgb(245, 244, 162))
        {
            remainTxt.AppendText("4");
        }
        else if (totalTxt.BackColor == Color.FromArgb(245, 244, 162))
        {
            totalTxt.AppendText("4");
        }
        else if (paidTxt.BackColor == Color.FromArgb(245, 244, 162))
        {
            paidTxt.AppendText("4");
        }
    }

    private void btn5_Click(object sender, EventArgs e)
    {
        if (remainTxt.BackColor == Color.FromArgb(245, 244, 162))
        {
            remainTxt.AppendText("5");
        }
        else if (totalTxt.BackColor == Color.FromArgb(245, 244, 162))
        {
            totalTxt.AppendText("5");
        }
        else if (paidTxt.BackColor == Color.FromArgb(245, 244, 162))
        {
            paidTxt.AppendText("5");
        }
    }

    private void btn6_Click(object sender, EventArgs e)
    {
        if (remainTxt.BackColor == Color.FromArgb(245, 244, 162))
        {
            remainTxt.AppendText("6");
        }
        else if (totalTxt.BackColor == Color.FromArgb(245, 244, 162))
        {
            totalTxt.AppendText("6");
        }
        else if (paidTxt.BackColor == Color.FromArgb(245, 244, 162))
        {
            paidTxt.AppendText("6");
        }
    }

    private void btn7_Click(object sender, EventArgs e)
    {
        if (remainTxt.BackColor == Color.FromArgb(245, 244, 162))
        {
            remainTxt.AppendText("7");
        }
        else if (totalTxt.BackColor == Color.FromArgb(245, 244, 162))
        {
            totalTxt.AppendText("7");
        }
        else if (paidTxt.BackColor == Color.FromArgb(245, 244, 162))
        {
            paidTxt.AppendText("7");
        }
    }

    private void btn8_Click(object sender, EventArgs e)
    {
        if (remainTxt.BackColor == Color.FromArgb(245, 244, 162))
        {
            remainTxt.AppendText("8");
        }
        else if (totalTxt.BackColor == Color.FromArgb(245, 244, 162))
        {
            totalTxt.AppendText("8");
        }
        else if (paidTxt.BackColor == Color.FromArgb(245, 244, 162))
        {
            paidTxt.AppendText("8");
        }
    }

    private void btn9_Click(object sender, EventArgs e)
    {
        if (remainTxt.BackColor == Color.FromArgb(245, 244, 162))
        {
            remainTxt.AppendText("9");
        }
        else if (totalTxt.BackColor == Color.FromArgb(245, 244, 162))
        {
            totalTxt.AppendText("9");
        }
        else if (paidTxt.BackColor == Color.FromArgb(245, 244, 162))
        {
            paidTxt.AppendText("9");
        }
    }

    private void btn0_Click(object sender, EventArgs e)
    {
        if (remainTxt.BackColor == Color.FromArgb(245, 244, 162))
        {
            remainTxt.AppendText("0");
        }
        else if (totalTxt.BackColor == Color.FromArgb(245, 244, 162))
        {
            totalTxt.AppendText("0");
        }
        else if (paidTxt.BackColor == Color.FromArgb(245, 244, 162))
        {
            paidTxt.AppendText("0");
        }
    }

    private void btndot_Click(object sender, EventArgs e)
    {
        if (remainTxt.BackColor == Color.FromArgb(245, 244, 162))
        {
            remainTxt.AppendText(".");
        }
        else if (totalTxt.BackColor == Color.FromArgb(245, 244, 162))
        {
            totalTxt.AppendText(".");
        }
        else if (paidTxt.BackColor == Color.FromArgb(245, 244, 162))
        {
            paidTxt.AppendText(".");
        }
    }

    private void btnback_Click(object sender, EventArgs e)
    {
        if (remainTxt.BackColor == Color.FromArgb(245, 244, 162))
        {
            remainTxt.Text = remainTxt.Text.Remove(remainTxt.Text.Length - 1, 1);
        }
        else if (totalTxt.BackColor == Color.FromArgb(245, 244, 162))
        {
            totalTxt.Text = totalTxt.Text.Remove(totalTxt.Text.Length - 1, 1);
        }
        else if (paidTxt.BackColor == Color.FromArgb(245, 244, 162))
        {
            paidTxt.Text = paidTxt.Text.Remove(paidTxt.Text.Length - 1, 1);
        }
    }

    private void btnreset_Click(object sender, EventArgs e)
    {
        if (remainTxt.BackColor == Color.FromArgb(245, 244, 162))
        {
            remainTxt.Text = "";
        }
        else if (totalTxt.BackColor == Color.FromArgb(245, 244, 162))
        {
            totalTxt.Text = "";
        }
        else if (paidTxt.BackColor == Color.FromArgb(245, 244, 162))
        {
            paidTxt.Text = "";
        }
    }

7 个答案:

答案 0 :(得分:1)

您可以将逻辑提取到单独的方法中:

private void AppendValue(string valueToAppend)
{
    if(remainTxt.BackColor == Color.FromArgb(245, 244, 162))
    {
        remainTxt.AppendText(valueToAppend);
    }
    else if (totalTxt.BackColor == Color.FromArgb(245, 244, 162))
    {
        totalTxt.AppendText(valueToAppend);
    }
    else if (paidTxt.BackColor == Color.FromArgb(245, 244, 162))
    {
        paidTxt.AppendText(valueToAppend);
    }
}

然后从每个点击事件中调用它:

private void btn1_Click(object sender, EventArgs e)
{
    AppendValue("1");
}

这适用于除btnback_Click事件之外的所有情况,但您可以自行处理该方法而无需调用AppendValue

答案 1 :(得分:1)

正如Grant所写,将类似的逻辑转换为可重用的方法。您甚至可以使用单个事件处理程序并将值作为CommandArgument发送。

其次,永远不要编写依赖于某种形式的UI组件的业务规则,比如颜色。颜色很可能会随着时间而改变,代码会破裂。相反,添加一些其他字段以保持表单状态

答案 2 :(得分:1)

将动作类型定义为枚举,可以在以后进行扩展:

enum TextAction
{
    Clear,
    Append,
    RemoveLast
}

private void FormatText(TextAction action, string value = null)
{
    var textBox = DetermineTextBox();

    if (textBox != null)
    {
        switch (action)
        {
            case TextAction.Append :
                textBox.Text = value;
                break;
            case TextAction.Clear:
                textBox.Text = "";
                break;
            case TextAction.RemoveLast:
                textBox.Text = textBox.Text.Remove(textBox.Text.Length - 1, 1);
                break;

            default:
                break;
        }
    }
}

private TextBox DetermineTextBox()
{
    if (remainTxt.BackColor == Color.FromArgb(245, 244, 162))
    {
        return remainTxt;
    }
    else if (totalTxt.BackColor == Color.FromArgb(245, 244, 162))
    {
        return totalTxt;
    }
    else if (paidTxt.BackColor == Color.FromArgb(245, 244, 162))
    {
        return paidTxt;
    }

    return null;
}

private void btn1_Click(object sender, EventArgs e)
{
    FormatText(TextAction.Append, "1");
}

private void btn2_Click(object sender, EventArgs e)
{
    FormatText(TextAction.Append, "2");
}

private void btn3_Click(object sender, EventArgs e)
{
    FormatText(TextAction.Append, "3");
}

private void btn4_Click(object sender, EventArgs e)
{
    FormatText(TextAction.Append, "4");
}

private void btn5_Click(object sender, EventArgs e)
{
    FormatText(TextAction.Append, "5");
}

private void btn6_Click(object sender, EventArgs e)
{
    FormatText(TextAction.Append, "6");
}

private void btn7_Click(object sender, EventArgs e)
{
    FormatText(TextAction.Append, "7");
}

private void btn8_Click(object sender, EventArgs e)
{
    FormatText(TextAction.Append, "8");
}

private void btn9_Click(object sender, EventArgs e)
{
    FormatText(TextAction.Append, "9");
}

private void btn0_Click(object sender, EventArgs e)
{
    FormatText(TextAction.Append, "0");
}

private void btndot_Click(object sender, EventArgs e)
{
    FormatText(TextAction.Append, ".");
}

private void btnback_Click(object sender, EventArgs e)
{
    FormatText(TextAction.RemoveLast);
}

private void btnreset_Click(object sender, EventArgs e)
{
    FormatText(TextAction.Clear);
}

答案 3 :(得分:0)

您可以尝试使用Switch语句,它们会使它更清晰......

http://msdn.microsoft.com/en-us/library/06tc147t.aspx

答案 4 :(得分:0)

private var conditionColor = Color.FromArgb(245, 244, 162);

private void btn1_Click(object sender, EventArgs e)
{
    CheckAndUpdateTextFields("1");
}

private void btn2_Click(object sender, EventArgs e)
{
    CheckAndUpdateTextFields("2");
}

private void btn3_Click(object sender, EventArgs e)
{
    CheckAndUpdateTextFields("3");
}

private void btn4_Click(object sender, EventArgs e)
{
    CheckAndUpdateTextFields("4");
}

private void btn5_Click(object sender, EventArgs e)
{
    CheckAndUpdateTextFields("5");
}

private void btn6_Click(object sender, EventArgs e)
{
    CheckAndUpdateTextFields("6");
}

private void btn7_Click(object sender, EventArgs e)
{
    CheckAndUpdateTextFields("7");
}

private void btn8_Click(object sender, EventArgs e)
{
    CheckAndUpdateTextFields("8");
}

private void btn9_Click(object sender, EventArgs e)
{
    CheckAndUpdateTextFields("9");
}

private void btn0_Click(object sender, EventArgs e)
{
    CheckAndUpdateTextFields("0");
}

private void btndot_Click(object sender, EventArgs e)
{
    CheckAndUpdateTextFields(".");
}

private void btnback_Click(object sender, EventArgs e)
{
    if (remainTxt.BackColor == conditionColor)
    {
        remainTxt.Text = remainTxt.Text.Remove(remainTxt.Text.Length - 1, 1);
    }
    else if (totalTxt.BackColor == conditionColor)
    {
        totalTxt.Text = totalTxt.Text.Remove(totalTxt.Text.Length - 1, 1);
    }
    else if (paidTxt.BackColor == conditionColor)
    {
        paidTxt.Text = paidTxt.Text.Remove(paidTxt.Text.Length - 1, 1);
    }
}

private void btnreset_Click(object sender, EventArgs e)
{
    CheckAndUpdateTextFields("");
}

private CheckAndUpdateTextFields(string textToAppend)
{
   if(remainTxt.BackColor == conditionColor)
   {
       remainTxt.AppendText(textToAppend);
   }
   else if (totalTxt.BackColor == conditionColor)
   {
       totalTxt.AppendText(textToAppend);
   }
   else if (paidTxt.BackColor == conditionColor )
   {
       paidTxt.AppendText(textToAppend);
   }
}

答案 5 :(得分:0)

首先定义一个方法,接受具有您要执行的操作的委托:

static void DoEventually(Action<T> action) where T: TextBox
{
    if(remainTxt.BackColor == Color.FromArgb(245, 244, 162))
    {
        action(remainTxt);
    }
    else if (totalTxt.BackColor == Color.FromArgb(245, 244, 162))
    {
        action(totalTxt);
    }
    else if (paidTxt.BackColor == Color.FromArgb(245, 244, 162))
    {
        action(paidTxt);
    } 
}

然后你可以按照这些方式调用它:

private void btn1_Click(object sender, EventArgs e)
{
    DoEventually(txt => txt.AppendText("1"));
}

答案 6 :(得分:0)

我会将用于将按钮索引和背景颜色映射到字符串的规则封装到单独的类(或者甚至是接口之间)中。这看起来像这样:

public class ColorRules
{
    private IEnumerable<ColorRuleEntry> _rules;

    public ColorRules()
    {
        _rules = InitialColorRules();
    }

    private IEnumerable<ColorRuleEntry> InitialColorRules()
    {
        yield return new ColorRuleEntry(1, Color.FromArgb(245, 244, 162), "1");
        yield return new ColorRuleEntry(2, Color.FromArgb(245, 244, 162), "2");
        yield return new ColorRuleEntry(3, Color.FromArgb(245, 244, 162), "3");
        yield return new ColorRuleEntry(4, Color.FromArgb(245, 244, 162), "4");
        // and so on
    }

    public bool TryGetResult(int button, Color backgroundColor, out String result)
    {
        var entry = _rules.FirstOrDefault(e => e.Button == button && e.Color.Equals(backgroundColor));
        if (entry == null)
        {
            result = null;
            return false;
        }

        result = entry.Result;
        return true;
    }

    protected class ColorRuleEntry
    {
        public ColorRuleEntry(int button, Color color, String result)
        {
            Button = button;
            Color = color;
            Result = result;
        }

        public Color Color { get; protected set; }
        public int Button { get; protected set; }
        public String Result { get; protected set; }
    }
}

这个类的用法如下:

private ColorRules _rules = new ColorRules();

private void updateTextFormBackgroundColors(int buttonNr)
{
    var result = String.Empty;

    if (_rules.TryGetResult(buttonNr, remainTxt.BackColor, out result))
       remainTxt.AppendText(result);

    if (_rules.TryGetResult(buttonNr, totalTxt.BackColor, out result))
       totalTxt.AppendText(result);

    if (_rules.TryGetResult(buttonNr, paidTxt.BackColor, out result))
       paidTxt.AppendText(result);
}

private void btn4_Click(object sender, EventArgs e)
{
    updateTextFormBackgroundColors(buttonNr: 4);
}