0

我有一个从 B11(坐标 1,1)到 B55(坐标 5,5)命名的图片框数组。我想在启动时(以及在运行过程中)隐藏这些。我正在考虑手动制作一个名称数组,但这会是最好的解决方案吗?

4

3 回答 3

1

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

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

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

于 2013-07-02T19:29:37.573 回答
0

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

于 2013-07-03T04:36:51.593 回答
0

您可以通过以下方式扩展 PictureBox 类并使用事件处理来完成此操作:

  • 向窗体添加一个公共属性来判断图片框是显示还是隐藏。
  • 将事件添加到显示/隐藏图片框属性更改时引发的表单。
  • 扩展 PictureBox 类,使其订阅父窗体的事件。
  • 将扩展的 PictureBox 类的可见属性设置为父窗体的显示/隐藏属性。

当父窗体上的显示/隐藏标志发生更改时,所有图片框都会相应地更改其可见性属性。

表格代码:

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

扩展图片框代码:

    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;
    }
}
于 2013-07-02T19:59:43.633 回答