如何为多个元素生成if语句?C#

如何为多个元素生成if语句?C#,c#,winforms,if-statement,C#,Winforms,If Statement,我在Windows窗体上使用VS2017和C 我正在创建一个小工具,我需要一些帮助 我有10个文本框,名称textGame1、textGame2、textGame3等等 我想创建一个代码,如果其中的任何文本框为空,那么它会将文本框文本替换为“空” 我很容易做到 if (string.IsNullOrWhiteSpace(textGame1.Text)) { textGame1.Text = "EMPTY"; } 但是,我必须为每个文本框创建10个 有没有比创建10条IF语句更简单的方

我在Windows窗体上使用VS2017和C

我正在创建一个小工具,我需要一些帮助

我有10个文本框,名称textGame1、textGame2、textGame3等等

我想创建一个代码,如果其中的任何文本框为空,那么它会将文本框文本替换为“空”

我很容易做到

if (string.IsNullOrWhiteSpace(textGame1.Text))
{
    textGame1.Text = "EMPTY";
}
但是,我必须为每个文本框创建10个


有没有比创建10条IF语句更简单的方法?

首先,创建一个集合来存储要操作的每个文本框。您可以手动执行此操作:

var textBoxes = new[] { textGame1, textGame2, textGame3, ... };
或者使用集合(一点Linq可能会有所帮助):

或者,如果需要递归搜索所有子级,请使用以下方法:

现在,只需使用
foreach

foreach(var tb in textBoxes)
{
    if (string.IsNullOrWhiteSpace(tb.Text))
    {
        tb.Text = "EMPTY";
    }
}

您可以使用通用代码:

            const string baseName = "textBox";
            var names = Enumerable.Range(1, 10).Select(x => baseName + x.ToString()).ToList();

            var tbxs = names.Select(name => this.Controls.Find(name, true).FirstOrDefault()).Where(x=> x != null).ToList();
            foreach (var txt in tbxs) {
                if (string.IsNullOrWhiteSpace(txt.Text))
                {
                    txt.Text = "EMPTY";
                }
            }
您可以使用占位符功能创建“自己的”文本框:

public class PlaceHolderTextBox : TextBox
{
    bool isPlaceHolder = true;
    string _placeHolderText;
    public string PlaceHolderText {
        get { return _placeHolderText; }
        set {
            _placeHolderText = value;
            setPlaceholder();
        }
    }

    //when the control loses focus, the placeholder is shown
    private void setPlaceholder()
    {
        if (string.IsNullOrEmpty(this.Text)) {
            this.Text = PlaceHolderText;
            this.ForeColor = Color.Gray;
            this.Font = new Font(this.Font, FontStyle.Italic);
            isPlaceHolder = true;
        }
    }

    //when the control is focused, the placeholder is removed
    private void removePlaceHolder()
    {
        if (isPlaceHolder) {
            this.Text = "";
            this.ForeColor = System.Drawing.SystemColors.WindowText;
            this.Font = new Font(this.Font, FontStyle.Regular);
            isPlaceHolder = false;
        }
    }
    public PlaceHolderTextBox()
    {
        GotFocus += removePlaceHolder;
        LostFocus += setPlaceholder;
    }
}
代码学分:


我刚从VB中转换过来;)

为了清楚起见,是否希望“如果任何文本框为空,则将所有文本框设置为“空”或“如果任何文本框为空,则将特定文本框设置为“空”呢?请使用
控件
类或使用lamda@p.s.w.g你的第二点是正确的。这就是你能做到的。新计划,vs 2017,WinForms。怀旧…非常感谢!这种方法看起来最适合我需要做的其他任务!
            const string baseName = "textBox";
            var names = Enumerable.Range(1, 10).Select(x => baseName + x.ToString()).ToList();

            var tbxs = names.Select(name => this.Controls.Find(name, true).FirstOrDefault()).Where(x=> x != null).ToList();
            foreach (var txt in tbxs) {
                if (string.IsNullOrWhiteSpace(txt.Text))
                {
                    txt.Text = "EMPTY";
                }
            }
public class PlaceHolderTextBox : TextBox
{
    bool isPlaceHolder = true;
    string _placeHolderText;
    public string PlaceHolderText {
        get { return _placeHolderText; }
        set {
            _placeHolderText = value;
            setPlaceholder();
        }
    }

    //when the control loses focus, the placeholder is shown
    private void setPlaceholder()
    {
        if (string.IsNullOrEmpty(this.Text)) {
            this.Text = PlaceHolderText;
            this.ForeColor = Color.Gray;
            this.Font = new Font(this.Font, FontStyle.Italic);
            isPlaceHolder = true;
        }
    }

    //when the control is focused, the placeholder is removed
    private void removePlaceHolder()
    {
        if (isPlaceHolder) {
            this.Text = "";
            this.ForeColor = System.Drawing.SystemColors.WindowText;
            this.Font = new Font(this.Font, FontStyle.Regular);
            isPlaceHolder = false;
        }
    }
    public PlaceHolderTextBox()
    {
        GotFocus += removePlaceHolder;
        LostFocus += setPlaceholder;
    }
}