-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

5
  • 1
    "Reflection" is the term you are looking for. Commented Apr 10, 2014 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. Commented Apr 10, 2014 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. Commented Apr 10, 2014 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. Commented Apr 10, 2014 at 22:04
  • stackoverflow.com/questions/3898588/… this is my resolvement Commented Apr 10, 2014 at 22:49

1 Answer 1

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;
}
Sign up to request clarification or add additional context in comments.

1 Comment

var code = field.GetValue(this) as Textbox;// null refence exeption

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.