Home  >  Article  >  Backend Development  >  Code example of Microsoft bug .net component being referenced by the container trap

Code example of Microsoft bug .net component being referenced by the container trap

黄舟
黄舟Original
2017-03-04 11:11:341319browse

 .net(C#) WinForm development, because it is a visual design, you can directly add a required component to the design interface manually. .net will automatically initialize this component, including property settings, etc., and add it to InitilizeComponent(), and this component will be added to the corresponding parent component. All of this is done automatically by .net.

However, in some scenarios, we need to write code manually to change the parent container to which the component belongs. For example, some components originally belonged to parent container A, but we want to adjust these components to parent container B. At this time, an interesting problem arises.

The following three components were originally located in this:

            this.Controls.Add(this.operRateUC);
            this.Controls.Add(this.personProductUg);
            this.Controls.Add(this.procedingPanel);

I want to adjust these three components to the adjustPanel component. As shown in the following code:

     private void moveToAdjustPanel()
     {          //AdjustablePanel是一个Control类
          AdjustablePanel adjustPanel = new AdjustablePanel();          
          foreach (Control ultraControl in this.Controls)
            {                    
            if (ultraControl.GetType() == typeof(UltraGrid) || 
                        ultraControl.GetType() == typeof(UltraChart) ||
                        ultraControl.GetType() == typeof(Panel))
                {
                    adjustPanel.Controls.Add(ultraControl);                    
                }  
            }
     }

This method of batch moving components to another parent component fails.
Every time adjustPanel adds a new component, the component of this.Controls will change, and no exception that the foreach iterator is modified will be thrown. I don’t know if this is a Microsoft bug.

Posted on bbs.csdn.net for help and responded. Most people thought that foreach traversal would report an error, but it was true that the compiler did not throw any exception. I re-ran a simple test with the compiler. As a result, I found that the foreach traversal did not report an error, but I could not get the desired result.


Code example of Microsoft bug .net component being referenced by the container trap

The test code is as follows. The expectation of the test is to move the two Button components from this to groupBox1. But the result is that there is still button1 in this, and only button2 has been moved to groupBox1.


Strange point:

The foreach iterator is modified, why does it not report an error? ? ?
Why is only button2 moved to groupBox1? ? ?

        public Form1()
        {
            InitializeComponent();
            moveButtonsToGroupBox();            //controlNames的结果为{groupBox1,button1}
            var controlNames = showAllChildControls(this); 
            //controlNamesInGroup的结果为{button2}
            var controlNamesInGroup = showAllChildControls(this.groupBox1);             
        }        /// <summary>
        /// 移动位于Form上的按钮到GroupBox中
        /// </summary>
        private void moveButtonsToGroupBox()
        {            foreach(Control c in this.Controls)
            {                if (c.GetType() == typeof(Button))                    
            this.groupBox1.Controls.Add(c);
            }
        }        /// <summary>
        /// 展示c控件的所有子组件的Name
        /// </summary>
        /// <param name="c"></param>
        /// <returns></returns>
        private List<string> showAllChildControls(Control 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 development, because it is a visual design, you can directly add a required component to the design interface manually, and .net will automatically initialize this component. Including property settings, etc., are added to InitilizeComponent(), and this component will be added to the corresponding parent component. All of this is done automatically by .net.

The above is the content of the code example of the Microsoft bug .net component being referenced by the container. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn