.net - Concatenate control name in C# -


i don't know how this, have 3 datagridview control (named datagridview1, datagridview2 & datagridview3). question can concatenate name of control?

i mean there way can call this: datagridview(n) or datagridview + n

i'm using winforms.

thanks in advance!

i think want find control dynamic name?

something perhaps:

control match = parentcontrol.controls["datagridview" + n]; 

so if example data grid in single panel called "mypanel", can this:

datagridview match = mypanel.controls["datagridview" + n] datagridview; 

if however, data grids not belong same parent control, can find them using controls.find method:

datagridview match = this.controls.find("datagridview" + n, true)[0] datagridview; 

note: controls.find method returns array, need select first element (assuming control name unique), may worth checking if array has values before trying access first element too.


if want wrap in function, can this:

public datagridview getdatagridviewfortabnumber(int n){     control[] matches = this.controls.find("datagridview" + n, true);     if(matches.length == 0)          return null;     return matches[0] datagridview; } 

and call so:

datagridview dgv = getdatagridviewfortabnumber(1);//gets datagridview1 

note: valid method if creating datagridview controls dynamically @ run time. if however, creating them in designer then, tim said, should give them more meaningful name , reference them directly.


Comments

Popular posts from this blog

php - Why I am getting the Error "Commands out of sync; you can't run this command now" -

linux - Does gcc have any options to add version info in ELF binary file? -

java - Are there any classes that implement javax.persistence.Parameter<T>? -