开发者

Is System.Windows.Forms.Control.ControlCollection.Add idempotent?

开发者 https://www.devze.com 2023-01-10 00:49 出处:网络
In other words, 开发者_开发百科will line 2 in the below code have any effect? // let\'s assume that myForm has not previously been added

In other words, 开发者_开发百科will line 2 in the below code have any effect?

// let's assume that myForm has not previously been added  
myControlCollection.Add(myForm);
myControlCollection.Add(myForm); // line 2 


I believe that executing Add twice in immediate succession will have no obvious effect. However, if there are intervening Add calls with other controls, it will - because Add updates the Z-order, sending the newly added control to the back. For example:

using System;
using System.Drawing;
using System.Windows.Forms;

class Test
{
    static void Main()
    {
        Form f = new Form();
        Button b1 = new Button
        {
            Location = new Point(50, 50),
            Size = new Size(40, 40),
            Text = "b1"
        };
        Button b2 = new Button
        {
            Location = new Point(70, 70),
            Size = new Size(40, 40),
            Text = "b2"
        };

        f.Controls.Add(b1);
        f.Controls.Add(b2);
//        f.Controls.Add(b1); 
        Application.Run(f);
    }
}

This shows b1 in front of b2 - but if you uncomment the second call to Add(b1), the order will be reversed.


No the second line won't have any effect. The collection won't add the same control instance twice.


No effect will be there on execution of second line.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号