-1

I am currently doing this one by one: to tidy them

             //created on the form1    
textboxs[0] = textbox0;  
textboxs[1] = textbox1; 
textboxs[2] = textbox2;  
textboxs[3] = textbox3;

Is there a way to convert it to the below somehow?

for (int i = 0; i < KSy; i++)  //kisi sayısı
{
    // getting the i as code (not variable)
    textboxs[i] = textbox+i ;
}

update: this is what i wanted to do : Find control by name from Windows Forms controls

everyone talks about reflection . i coudnt make reflection work

Community
  • 1
  • 1
  • 1
    "Reflection" is the term you are looking for. – DaveShaw Apr 10 '14 at 21:55
  • You didn't even bothered to tell us what are you trying to accomplish nor what does "cb_kisiLER" means or is, we're no wizards. –  Apr 10 '14 at 21:55
  • 7
    It’s possible; don’t do it. Either you have few enough that it can be manual or you have so many that they should never have been put into separate variables in the first place, and you should fix that. – Ry- Apr 10 '14 at 21:56
  • 2
    You also should not have "textbox1" as textbox name - and when you have some more meaningful names your for loop will not be useful... If it is group of related textboxes - make them child of single parent and use parent's `Controls` list to pick your elements. – Alexei Levenkov Apr 10 '14 at 22:04
  • http://stackoverflow.com/questions/3898588/find-control-by-name-from-windows-forms-controls this is my resolvement – user3521369 Apr 10 '14 at 22:49

1 Answers1

0

Assuming cb_kisiN are fields you can do:

for(int i =  0; i < KSy; i++)
{
    var field = this.GetType().GetField("textbox" + i);
    var code = field.GetValue(this) as Code;
    textboxs[i] = code;
}
Lee
  • 142,018
  • 20
  • 234
  • 287