Code examples of Microsoft bug. NET components being container-referenced traps

Source: Internet
Author: User

. 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)!

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.