简体   繁体   中英

C# Iterating over known textboxes from array

I am trying to iterate over textboxes from a string in string Array

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. 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?

Just read a source saying that this.getProperty may work so I'll try that.

Error

Cannot apply indexing with [] to an expression of type '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() :

    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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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