简体   繁体   English

C# 迭代数组中的已知文本框

[英]C# Iterating over known textboxes from array

I am trying to iterate over textboxes from a string in string Array我正在尝试从string数组中的string迭代文本框

static string[] TextBoxes = {
                             "EmpName",
                             "Sales",
                             "BasePay",
                             "Commission",
                             "GrossPay",
                             "Deductions",
                             "Housing",
                             "FoodAndClothing",
                             "Entertainment",
                             "Misc"
                            };

Each of the above are parts of the form for example txtHousing is the housing input element.以上每个都是表单的一部分,例如 txtHousing 是房屋输入元素。 And so on and so forth.等等等等。

And the iteration takes place here迭代发生在这里

private void btnClear_Click(object sender, EventArgs e)
{
    for (byte i = 0; i < TextBoxes.Length; i++)
    {
        this["txt" + TextBoxes[i]].Text = "";
    }
    this.txtEmpName.Focus();
}

Except I'm getting a strange "error" for this System.String can we not call objects like this from the this object?除了这个System.String出现一个奇怪的“错误”之外,我们不能从this对象中调用this对象吗?

Just read a source saying that this.getProperty may work so I'll try that.只是阅读一个消息来源说 this.getProperty 可能会起作用,所以我会尝试这样做。

Error错误

Cannot apply indexing with [] to an expression of type 'Pay_Calculator.PayCalculator'无法将 [] 索引应用于“Pay_Calculator.PayCalculator”类型的表达式

One solution is to use Reflection/Dynamic object.一种解决方案是使用反射/动态对象。

var me = (dynamic) this;
foreach (var name in TextBoxes)
{
  ((TextBox) me['txt' + name]).Value = string.Empty;
}

Another solution is to recurse over all controls, then you don't need to name all the text boxes.另一种解决方案是递归所有控件,然后您不需要命名所有文本框。

private void btnClear_Click(object sender, EventArgs e)
{
   ClearTextBox(this);
}

void ClearTextBox (Control c)
{
    var t = c as Textbox;
    if (t != null)
       t.Value = string.Empty;
    foreach (var child in c.Controls)
       ClearTextBox(child);
}

I think you need to do something like this我认为你需要做这样的事情

private void btnClear_Click(object sender, EventArgs e)
{
    for (byte i = 0; i < TextBoxes.Length; i++)
    {
        if (this.Controls.ContainsKey("txt" + TextBoxes[i]))
        {
            TextBox txtBox = this.Controls["txt" + TextBoxes[i]] as TextBox;
            if (txtBox != null)
            {
                // Do your Stuff
                txtBox.Text = "";
            }
        }
    }
}

Assuming WinForms , change static to private and use Controls.Find() :假设WinForms ,将static更改为private并使用Controls.Find()

    private string[] TextBoxes = {
                         "EmpName",
                         "Sales",
                         "BasePay",
                         "Commission",
                         "GrossPay",
                         "Deductions",
                         "Housing",
                         "FoodAndClothing",
                         "Entertainment",
                         "Misc"
                        };


    private void btnClear_Click(object sender, EventArgs e)
    {
        foreach (string name in TextBoxes)
        {
            TextBox tb = this.Controls.Find("txt" + name, true).FirstOrDefault() as TextBox;
            if (tb != null)
            {
                tb.Text = "";
            }
        }
        this.txtEmpName.Focus();
    }

You have to loop through the controls and check whether the control is a text box, later you can check the name of the control to match your array element and clear it.您必须遍历控件并检查控件是否为文本框,稍后您可以检查控件的名称以匹配您的数组元素并清除它。

You can follow this example你可以按照这个例子

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM