隐藏一系列图片框的最快方法

时间:2013-07-02 19:05:32

标签: arrays vb.net winforms list foreach

我有一系列从B11(合作伙伴1,1)到B55(合作伙伴5,5)命名的图箱。我想在启动时(以及在运行中)隐藏这些全部内容。我正在考虑手动制作一系列名称,但这是最好的解决方案吗?

3 个答案:

答案 0 :(得分:1)

如果它们都有一个共同的父控件,例如面板或组框(甚至是表单):

Parent.SuspendLayout()
For Each pbox As PictureBox in Parent.Controls.OfType(Of PictureBox)()
   pbox.Visible = False
Next pbox
Parent.ResumeLayout()

Suspend / Resume-Layout()是为了避免在你一次修改一堆控件时出现闪烁。

答案 1 :(得分:0)

您可以扩展PictureBox类并使用事件处理来实现此目的:

  • 向表单添加公共属性,以判断是应显示还是隐藏图片框。
  • 向显示/隐藏图片框属性更改时引发的表单添加事件。
  • 扩展PictureBox类,以便它订阅父窗体的事件。
  • 将扩展PictureBox类的visible属性设置为父窗体的show / hide属性。

当在父表单上更改显示/隐藏标志时,所有图片框都会相应地更改其可见性属性。

表格代码:

    public partial class PictureBoxForm : Form {
        public PictureBoxForm() {
            InitializeComponent();
            this.pictureBoxesAdd();
        }


        private void pictureBoxesAdd() {
            MyPictureBox mp1 = new MyPictureBox();
            mp1.Location = new Point(1, 1);         
            MyPictureBox mp2 = new MyPictureBox();
            mp2.Location = new Point(200, 1);
            this.Controls.Add(mp1);
            this.Controls.Add(mp2);
        }

        public event EventHandler PictureBoxShowFlagChanged;

        public bool PictureBoxShowFlag {
            get { return this.pictureBoxShowFlag; }
            set {
                if (this.pictureBoxShowFlag != value) {
                    pictureBoxShowFlag = value;
                    if (this.PictureBoxShowFlagChanged != null) {
                        this.PictureBoxShowFlagChanged(this, new EventArgs());
                    }
                }
            }
        }

        private bool pictureBoxShowFlag = true;

        private void cmdFlip_Click( object sender, EventArgs e ) {
            this.PictureBoxShowFlag = !this.PictureBoxShowFlag;
        }
    }

扩展PictureBox代码:

    public class MyPictureBox : PictureBox {

        public MyPictureBox() : base() {
            this.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.ParentChanged += new EventHandler(MyPictureBox_ParentChanged);
        }

        private void MyPictureBox_ParentChanged( object sender, EventArgs e ) {
            try {
                PictureBoxForm pbf = (PictureBoxForm)this.Parent;
                this.Visible = pbf.PictureBoxShowFlag;
                pbf.PictureBoxShowFlagChanged += new
                    EventHandler(pbf_PictureBoxShowFlagChanged);
            } catch { }
    }

    private void pbf_PictureBoxShowFlagChanged( object sender, EventArgs e ) {
            PictureBoxForm pbf = (PictureBoxForm)sender;
            this.Visible = pbf.PictureBoxShowFlag;
    }
}

答案 2 :(得分:0)

...或者只是将它们全部放在Panel上,并更改面板的可见性。

相关问题