简体   繁体   English

如何验证四个文本框的值是否为空字符串,以及如何使用最少的代码在C#中将这些文本框分配为“ 0”值(如果在C#中为空)

[英]How to validate if the values of four text box is empty string or not and assign these text box as “0” value if it is empty in C# with minimal code

I have four text box in C# ,If the value of any text box is :empty string,then it must be assigned as '0'.I have tried the following code which seems to be lenghty . 我在C#中有四个文本框,如果任何文本框的值是:empty string,则必须将其指定为“ 0”。我尝试了以下似乎冗长的代码。

                if (txtReset1.Text == "")
                {
                    txtReset1.Text = "0";
                }

                if (txtReset2.Text == "")
                {
                    txtReset2.Text = "0";
                }

                if (txtReset3.Text == "")
                {
                    txtReset3.Text = "0";
                }

                if (txtReset4.Text == "")
                {
                    txtReset4.Text = "0";
                }

Is there any more efficient code than the above one? 是否有比以上代码更有效的代码?

Rather than repeat yourself, create a new method to handle it: 与其重复自己,不如创建一个新方法来处理它:

private void SetEmptyTextBoxToZero(TextBox textBox)
{
    if (textBox != null && string.IsNullOrEmpty(textBox.Text)
    {
        textBox.Text = "0";
    }
}

Then you replace your code with: 然后,将代码替换为:

SetEmptyTextBoxToZero(txtReset1);
SetEmptyTextBoxToZero(txtReset2);
SetEmptyTextBoxToZero(txtReset3);
SetEmptyTextBoxToZero(txtReset4);

As " Binkan Salaryman" suggests, if you have lots of text boxes that need to be handled this way, then you can store references to them in a list and then iterate over them, rather than listing them out as above: 正如“ Binkan Salaryman”建议的那样,如果您有很多需要以这种方式处理的文本框,则可以将对它们的引用存储在列表中,然后对其进行迭代,而不是像上面那样列出它们:

var textBoxes = new List<TextBox> { txtReset1, txtReset2, txtReset3, txtReset4 };

...

// Option 1: using .ForEach()
textBoxes.ForEach(tb => SetEmptyTextBoxToZero(tb));

// Option 2: using foreach
foreach (var tb in textBoxes)
{
    SetEmptyTextBoxToZero(tb);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#Windows窗体-如何为组合框字符串分配一个双精度值,然后将其显示在文本框中? - C# Windows Form - How do I assign a double value to a combo box string to then be displayed in a text box? C#文本框保持为空 - C# Text box remaining empty 如何比较两个文本框,如果两个都为空,如何在C#中打印消息框 - how to compare two text box and if both is empty how to print a message box in c# 如何在一段时间内将“NULL”字分配给文本框(如果文本框为空)? - How to assign 'NULL' word to a text box (if the textbox is empty) during time? 如何验证 selenium c# 中文本框的标题 - How to validate the title of text box in selenium c# 如果在RDLC报告中tablix中的值为空,如何显示文本框 - How to show a text box if value in tablix is empty in RDLC report 如何防止int类型的文本框为空值 - How to prevent the empty value of an int type text box 如果是默认文本,则asp.net c#文本框onclick为空。 如果不是默认文本,则保持不变 - asp.net c# text box onclick empty if default text. Unchanged if not default text 如何在C#编码文件的表列内为文本框控件赋值? - How to assign value to text box control inside the table column in C# coding file? 将文本框字符串值保存到数据库C# - saving text box string value to database c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM