System.Windows.Forms.Control.ControlCollection.Add 是幂等的吗? [英] Is System.Windows.Forms.Control.ControlCollection.Add idempotent?

查看:27
本文介绍了System.Windows.Forms.Control.ControlCollection.Add 是幂等的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

换句话说,下面代码中的第 2 行有什么影响吗?

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 

推荐答案

我相信立即连续执行两次 Add 不会有明显效果.但是,如果有其他控件的介入 Add 调用,它会 - 因为 Add 更新 Z-order,将新添加的控件发送到后面.例如:

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);
    }
}

这在 b2 前面显示 b1 - 但如果你取消对 Add(b1) 的第二次调用的注释,顺序将被颠倒.

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

这篇关于System.Windows.Forms.Control.ControlCollection.Add 是幂等的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆