C# Iterating over known textboxes from array -


i trying iterate on textboxes string in string array

static string[] textboxes = {                              "empname",                              "sales",                              "basepay",                              "commission",                              "grosspay",                              "deductions",                              "housing",                              "foodandclothing",                              "entertainment",                              "misc"                             }; 

each of above parts of form example txthousing housing input element. , on , forth.

and iteration takes place here

private void btnclear_click(object sender, eventargs e) {     (byte = 0; < textboxes.length; i++)     {         this["txt" + textboxes[i]].text = "";     }     this.txtempname.focus(); } 

except i'm getting strange "error" system.string can not call objects this object?

just read source saying this.getproperty may work i'll try that.

error

cannot apply indexing [] expression of type 'pay_calculator.paycalculator'

one solution use reflection/dynamic object.

var me = (dynamic) this; foreach (var name in textboxes) {   ((textbox) me['txt' + name]).value = string.empty; } 

another solution recurse on controls, don't need name text boxes.

private void btnclear_click(object sender, eventargs e) {    cleartextbox(this); }  void cleartextbox (control c) {     var t = c textbox;     if (t != null)        t.value = string.empty;     foreach (var child in c.controls)        cleartextbox(child); } 

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -