c# - Why is there a difference in using for or foreach with TabPage.Controls? -


i have problem, that:

tabpage tab = new tabpage(); [...] (int = 0; < tab.controls.count; i++) {    debug.writeline(i + " - " + tab.controls[i].name + " - " + tab.controls[i].text); } 

has not same result than:

tabpage tab = new tabpage(); [...] int j = 0; foreach (control ctl in tab.controls) {     debug.writeline(j + " - " + ctl.name + " - " + ctl.text);     j++; } 

the for loop has in case result 53 items (count shows 53) foreach loop has result 27 items.

i cannot understand this. reason?

i solve problem own:

the second sourcecode wasn't posted this:

tabpage tab = new tabpage(); [...] int j = 0; foreach (control ctl in tab.controls) {     debug.writeline(j + " - " + ctl.name + " - " + ctl.text);     tab2.controls.add(ctl);     j++; } 

=> moved accidental control during foreach loop, want clone , answer how it: clone controls - c# (winform)

now fixed that:

foreach (control ctl in tab1.controls) {     try     {         if (ctl != null)         {             object obj_clone = cloneobject(ctl);             if (obj_clone != null && obj_clone control)             {                 control ctl_clone = (control)cloneobject(ctl);                 tab2.controls.add(ctl_clone);             }         }     }     catch (exception ex)     {         debug.writeline("exception: " + ex.message + " - targetsite:" + ex.targetsite + " - source: " + ex.source + " - innerexception: " + ex.innerexception.message.tostring());     } }  private object cloneobject(object obj) {     var typ = obj.gettype();     var obj_clone = createinstanceoftype(typ);     propertyinfo[] controlproperties = typ.getproperties(bindingflags.public | bindingflags.instance);     foreach (propertyinfo propinfo in controlproperties)     {         if (propinfo.canwrite && propinfo.name != "windowtarget")         {             try             {                 if (propinfo.propertytype.isvaluetype || propinfo.propertytype.isenum || propinfo.propertytype.equals(typeof(system.string)))                 {                     propinfo.setvalue(obj_clone, propinfo.getvalue(obj));                 }                 else                 {                     object value = propinfo.getvalue(obj);                     if (value == null)                         propinfo.setvalue(obj_clone, null);                     else                         propinfo.setvalue(obj_clone, cloneobject(value));                 }             }             catch (exception ex)             {                 debug.writeline("exception: " + ex.message + " - targetsite:" + ex.targetsite + " - source: " + ex.source + " - innerexception: " + ex.innerexception.message.tostring());             }         }     }     return (object)obj_clone; }  public object createinstanceoftype(type type) {     // first make instance of type.     object instance = null;      // if there empty constructor, call that.     if (type.getconstructor(type.emptytypes) != null)         instance = activator.createinstance(type);     else     {         // otherwise first available constructor , fill in default values.         // (we trying set properties anyway shouldn't of problem).         constructorinfo ci = type.getconstructors()[0];         parameterinfo[] parameters = ci.getparameters();          object[] ciparams = new object[parameters.length];          (int = 0; < parameters.length; i++)         {             if (parameters[i].defaultvalue != null)             {                 ciparams[i] = parameters[i].defaultvalue;                 continue;             }              type paramtype = parameters[i].parametertype;             ciparams[i] = createinstanceoftype(paramtype);         }          instance = ci.invoke(ciparams);     }      return instance; } 

Comments

Popular posts from this blog

javascript - Karma not able to start PhantomJS on Windows - Error: spawn UNKNOWN -

c# - Display ASPX Popup control in RowDeleteing Event (ASPX Gridview) -

Nuget pack csproj using nuspec -