winforms - c# group controls by name -


im looking way group number of controls in winforms name using c#.

the best way can describe functionality need compare how classes work in html/css same class recycled throughout target controls associated class.

i have tried using name property control.name hasnt worked out planned.

example

//group controls group identifier in case string 'name'` txtforename.name = "name"; txtsurname.name  = "name"; txtnotaname.name = "notaname";  foreach (control control in form.controls) {    if (control.name == "name")    {       console.writeline("true");    } } 

expected output

true;
true;

if more 1 control has same name, can target them while searching collection of controls?

is possible?

you use tag property on controls can store object. code be:

foreach (control control in form.controls) {   if(control.tag != null && control.tag.tostring() == "mytag")   {      //...   } } 

alternatively, use system.linq omit inner if clause:

foreach (control in form.controls.cast<control>().where(c => c.tag != null && c.tag.tostring() == "mytag")) {   //... } 

Comments

Popular posts from this blog

c++ - No viable overloaded operator for references a map -

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - Cannot secure connection using TLS -