none
这段拖沓冗长的代码,能优化吗? RRS feed

  • 问题

  •     这是界面中的部分TextBox控件,我要让选择的这部分TextBox的内容为“”,我只会简单的每个TextBox.Text=“”,能优化么?

    TextBox_OppositeSide.Text = "";
                    TextBox_ManageProject.Text = "";
                    TextBox_SubjectMatter.Text = "";
                    TextBox_EscapeLeaseDateFrom.Text = "";
                    TextBox_EscapeLeaseDateTo.Text = "";
                    TextBox_LeaseDateSign.Text = "";
                    TextBox_LeaseDtaeFrom.Text = "";
                    TextBox_LeaseDateTo.Text = "";
                    TextBox_MonthRent.Text = "";
                    TextBox_SumRent.Text = "";
                    TextBox_YearRent.Text = "";
                    TextBox_RateOfRise.Text = "";
                    TextBox_LeaseAssureMoney.Text = "";
                    TextBox_PenalSum.Text = "";
                    TextBox_ReferencePrice.Text = "";
                    TextBox_ReferencePriceRemarks.Text = "";
                    TextBox_ContractRemarks.Text = "";
                    TextBox_PropertyManageMoney.Text = "";
                    TextBox_ContractChange.Text = "";


    C# 菜鸟中的雏鸟!提的问题也许很幼稚,但我是认真的。希望看在党国的面子上拉兄弟一把!

    2013年6月18日 1:27

答案

  • TextBox_OppositeSide.Text = "";
    TextBox_ManageProject.Text = "";
    TextBox_SubjectMatter.Text = "";
    TextBox_EscapeLeaseDateFrom.Text = "";
    TextBox_EscapeLeaseDateTo.Text = "";
    TextBox_LeaseDateSign.Text = "";
    TextBox_LeaseDtaeFrom.Text = "";
    TextBox_LeaseDateTo.Text = "";
    TextBox_MonthRent.Text = "";
    TextBox_SumRent.Text = "";
    TextBox_YearRent.Text = "";
    TextBox_RateOfRise.Text = "";
    TextBox_LeaseAssureMoney.Text = "";
    TextBox_PenalSum.Text = "";
    TextBox_ReferencePrice.Text = "";
    TextBox_ReferencePriceRemarks.Text = "";
    TextBox_ContractRemarks.Text = "";
    TextBox_PropertyManageMoney.Text = "";
    TextBox_ContractChange.Text = "";
    //=======================================================================
    //更改后如下:
    public void ClearTextBox(params TextBox txt)
    {
        foreach(var t in txt)
        {
            t.Text = string.Empty;
        }
    }
    
    ClearTextBox(
        TextBox_OppositeSide,
        TextBox_ManageProject,
        TextBox_SubjectMatter,
        ...
        TextBox_ContractChange
    );
    这个思路希望能给你提供参考帮助!

    博客园: dotNetDR_ 的博客 - 天杀下来的BUG,不怕有我来挡!

    博客园介绍: 博客园创立于2004年1月,是面向软件开发者的高品质IT技术社区,聚集了很多优秀的软件开发者和IT技术学习者。

    2013年6月19日 4:12

全部回复

  • 如果你的页面(WebForm)上有若干TextBox,可以:

     foreach (var item in   Controls.OfType<TextBox>())
                {
                    item.Text = "";
                }

    If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer

    Help by clicking:
    Click here to donate your rice to the poor
    Click to Donate
    Click to feed Dogs & Cats


    Found any spamming-senders? Please report at: Spam Report

    2013年6月18日 5:32
  •     可我并不是每个TextBox都要“”啊,我只要指定的TextBox为“”,是不是要做个数列什么之类的啊?

    C# 菜鸟中的雏鸟!提的问题也许很幼稚,但我是认真的。希望看在党国的面子上拉兄弟一把!

    2013年6月18日 9:25
  • 你可以把所有需要设置为空的TextBox名字放入一个List<string>中(静态全局类变量),

    然后:

    foreach (var item in   Controls.OfType<TextBox>())
                {
                    if(你的定义变量.IndexOf(item.ID)>=0)
                    {
                        item.Text = string.Empty;
                    }
                }

    If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer

    Help by clicking:
    Click here to donate your rice to the poor
    Click to Donate
    Click to feed Dogs & Cats


    Found any spamming-senders? Please report at: Spam Report

    2013年6月18日 9:28
  • TextBox_OppositeSide.Text = "";
    TextBox_ManageProject.Text = "";
    TextBox_SubjectMatter.Text = "";
    TextBox_EscapeLeaseDateFrom.Text = "";
    TextBox_EscapeLeaseDateTo.Text = "";
    TextBox_LeaseDateSign.Text = "";
    TextBox_LeaseDtaeFrom.Text = "";
    TextBox_LeaseDateTo.Text = "";
    TextBox_MonthRent.Text = "";
    TextBox_SumRent.Text = "";
    TextBox_YearRent.Text = "";
    TextBox_RateOfRise.Text = "";
    TextBox_LeaseAssureMoney.Text = "";
    TextBox_PenalSum.Text = "";
    TextBox_ReferencePrice.Text = "";
    TextBox_ReferencePriceRemarks.Text = "";
    TextBox_ContractRemarks.Text = "";
    TextBox_PropertyManageMoney.Text = "";
    TextBox_ContractChange.Text = "";
    //=======================================================================
    //更改后如下:
    public void ClearTextBox(params TextBox txt)
    {
        foreach(var t in txt)
        {
            t.Text = string.Empty;
        }
    }
    
    ClearTextBox(
        TextBox_OppositeSide,
        TextBox_ManageProject,
        TextBox_SubjectMatter,
        ...
        TextBox_ContractChange
    );
    这个思路希望能给你提供参考帮助!

    博客园: dotNetDR_ 的博客 - 天杀下来的BUG,不怕有我来挡!

    博客园介绍: 博客园创立于2004年1月,是面向软件开发者的高品质IT技术社区,聚集了很多优秀的软件开发者和IT技术学习者。

    2013年6月19日 4:12