Can I use Reflection to do this?

  • Thread starter Thread starter Bill
  • Start date Start date
B

Bill

I have a situation where I want to autotab to the next control when the
previous control is defnitively completed with data. The dataentry controls
are arranged in a grid and are named, for example:

txtPC1 txtInstallAmt1
txtPC2 txtInstallAmt2
txtPC3 txtInstallAmt3
txtPC4 txtInstallAmt4

All change events for a column are mapped to a single method appropriate for
that column.

If Pay Code (PC) contols accept 0/1 as valid, it is processesed as follows:

private void txtCPC_TextChanged(object sender, System.EventArgs e)
{
if (name.Substring(0, name.Length - 1) == "txtPC")
{
string row = name.Substring(name.Length - 1, 1);
switch (this.ActiveControl.Text)
{
case "0":
case "1":
switch (row)
{
case "1":
txtCInstallAmt1.Focus();
txtCInstallAmt1.SelectAll();
break;
case "2":
txtCInstallAmt2.Focus();
txtCInstallAmt2.SelectAll();
break;
case "3":
txtCInstallAmt3.Focus();
txtCInstallAmt3.SelectAll();
break;
case "4":
txtCInstallAmt4.Focus();
txtCInstallAmt4.SelectAll();
break;
}
break:
}
}
}

I know the following change to the above is invalid syntax but I offer it as
an example of something I would like to do at runtime rather than compile
time:

string row = name.Substring(name.Length - 1, 1);

switch (this.ActiveControl.Text)
{
case "0":
case "1":
"txtCInstallAmt" + row + ".Focus()";
"txtCInstallAmt" + row + ".SelectAll()";
break;
}

Does anyone know the best way to do this?
 
Bill said:
I have a situation where I want to autotab to the next control when the
previous control is defnitively completed with data. The dataentry controls
are arranged in a grid and are named, for example:

txtPC1 txtInstallAmt1
txtPC2 txtInstallAmt2
txtPC3 txtInstallAmt3
txtPC4 txtInstallAmt4

Rather than use reflection, why not just create an array with the
appropriate entries in, or a map from each control to the next?
 
Back
Top