. NET (C #) WinForm is developed as a visual design, so you can manually add a required component to the design interface by hand, and. NET automatically initializes the component, including property settings, to the initilizecomponent () and this component is added to the corresponding parent component. All of these are. NET auto-complete.
However, in some scenarios, we need handwritten code to change the parent container to which the component belongs. For example, some components originally belonged to parent container A, but we wanted to adjust these components to parent container B, at which point an interesting problem arises.
The following 3 components are originally located in this:
This. Controls.Add (THIS.OPERRATEUC); This. Controls.Add (This.personproductug); This. Controls.Add (This.procedingpanel);
You want to adjust these 3 components to the Adjustpanel component. As shown in the following code:
private void Movetoadjustpanel () { //adjustablepanel is a control class Adjustablepanel Adjustpanel = new Adjustablepanel (); foreach (Control ultracontrol in). Controls) { if (ultracontrol.gettype () = = typeof (Ultragrid) | | Ultracontrol.gettype () = = typeof (Ultrachart) | | Ultracontrol.gettype () = = typeof (Panel)) { adjustPanel.Controls.Add (Ultracontrol);}} }
This method of bulk moving components to another parent component is unsuccessful.
Adjustpanel each time a new component is added, this. The controls ' components change, and the exception that the foreach iterator is modified is not thrown . This does not know whether Microsoft is a bug.
Bbs.csdn.net on the post to help, reply, most think the foreach traversal will error, but the compiler did not throw any exception. I re-made a simple test again by the compiler and found that the foreach traversal did not make an error, but did not get the desired result.
The test code is as follows, and the test is expected to move 2 button components from this to GroupBox1. But the result is that there are still button1 in this, and only Button2 is moved to GroupBox1.
Strange point:
The foreach iterator has been modified, why not error???
Why is only button2 moved to GroupBox1???
Public Form1 () {InitializeComponent (); Movebuttonstogroupbox (); The result of Controlnames is {groupbox1,button1} var controlnames = Showallchildcontrols (this); The result of Controlnamesingroup is {button2} var controlnamesingroup = Showallchildcontrols (This.groupbox1); }///<summary>///Move the button on form to GroupBox///</summary> private void mo Vebuttonstogroupbox () {foreach (Control C in). Controls) {if (c.gettype () = = typeof (Button)) This.groupBox1.Con Trols. ADD (c); }}///<summary>//Show All subcomponents of the C control the name////</summary>//<param name= "C "></param>//<returns></returns> private list<string> Showallchildcontrols (Cont Rol c) {if (c = = null) return null; List<string> controlnames = new list<string> (); foreach (Control CHL in C.controls) {Controlnames.add (CHL. Name); } return controlnames; }
. NET (C #) WinForm is developed as a visual design, so you can manually add a required component to the design interface by hand, and. NET automatically initializes the component, including property settings, to the initilizecomponent () and this component is added to the corresponding parent component. All of these are. NET auto-complete.
These are the contents of the code example where Microsoft Bug. NET components are referenced by containers, and more about topic.alibabacloud.com (www.php.cn)!