使多个图片框可见 [英] Making multiple pictureboxes visible

查看:70
本文介绍了使多个图片框可见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有30个图片框,它们是"Picturebox.visible = false".
我有3个单选按钮,它们决定单击按钮后可见的图片框:

I have 30 pictureboxes that are "Picturebox.visible = false".
I have 3 radiobuttons who decide how much pictureboxes that get visible after clicking a button:

private void buttonStart_Click(object sender, EventArgs e)
        {
            if (radioButtonMakkelijk.Checked)
            {
                aantal = 10;
            }
            else if (radioButtonGemiddeld.Checked)
            {
                aantal = 20;
            }
            else if (radioButtonMoeilijk.Checked)
            {
                aantal = 30;
            }
            else
            {
                buttonStart.Enabled = false;
            }

            for (counter = 0; counter < aantal; counter++)
            {
                string Visible = "Picturebox" + counter;
                Visible.Visible = true;
            }

        }



正如您在上面看到的那样,我使用了for来使这些图片框可见..但我不知道如何才能使它们可见,因为您不能在字符串上使用".Visible".



As you can see above I used a for, to make those pictureboxes Visible.. But I dont know how I can actually make them visible cause you can''t use ".Visible" on a string.

推荐答案

我将创建一个List<PictureBox>,它将容纳您所有的PictureBox es.
然后:

I would create a List<PictureBox> which would hold all your PictureBoxes.
Then :

for (int i = 0; i < aantal; i++)
{
   pictureBoxes[i].Visible = true;
}


枚举窗体中的子控件,如果控件是图片框,则执行所需的操作.

Enumerate the child controls in the form and if a control is a picture box, take the desired action.

foreach (Control ctrl in this.Controls)
{
    if (ctrl is PictureBox)
    {
        // take further action on the picture box
    }
}



如果您的 PictureBox控件位于容器内部,则必须编写一个将接受父容器的递归方法,如下所示:



If your PictureBox controls are inside containers, you''ll have to write a recursive method that will accept the parent container, like so:

private void ChangePictureBoxes(Control container)
{
    foreach (Control ctrl in this.Controls)
    {
        if (ctrl.HasChildren)
        {
            ChangePictureBoxes(ctrl);
        }
        else if (ctrl is PictureBox)
        {
            // take further action on the picture box
        }
    }
}



这确实是您可以执行所需操作而不必担心控件名称的唯一方法...



This is really the only way you can do what you want without worrying about control names...


for (counter = 0; counter < aantal; counter++)
            {
                string Visible = "this.PictureBox" + counter;
                this.Controls[Visible].Visible = true;
            }



试试这个.



Try This.


这篇关于使多个图片框可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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