如何使图片框隐藏并在阵列中单击时显示?

发布于 2025-01-28 12:59:45 字数 734 浏览 3 评论 0原文

我在一个项目上遇到了麻烦,该项目需要我在单击它时将阵列内部的图片框消失,但是由于某种原因,我很难做到它去做我想要的。我知道这不是一个实际的功能,但是有什么可以做我想做的事情,随时提出问题以获取更多信息,我真的很喜欢一些输入,感谢您的帮助。 ; d

PictureBox[] PB_covers = new PictureBox[48];

private void Form1_Load(object sender, EventArgs e)
        {
            int index = 0;
            foreach (PictureBox item in PB_covers)
            {   
                PB_covers[index].Click += PB_Click;
                index++;
            }
        }

        void PB_Click(object sender, EventArgs e)
        {
            for(int i = 0; i < PB_covers.Length; i++)
            {
                if (PB_covers[i].isClicked)
                {
                    //excecute funtion
                }
            }
        }

im having trouble with a project which requires me to make a picture box inside of an array disappear when i click it, but for some reason, im having trouble getting it to do what i want. i know that isClicked isn't an actual function but is there something that does do what i want, feel free to ask questions for more information, i just would really like some input, thanks for the help. ;D

PictureBox[] PB_covers = new PictureBox[48];

private void Form1_Load(object sender, EventArgs e)
        {
            int index = 0;
            foreach (PictureBox item in PB_covers)
            {   
                PB_covers[index].Click += PB_Click;
                index++;
            }
        }

        void PB_Click(object sender, EventArgs e)
        {
            for(int i = 0; i < PB_covers.Length; i++)
            {
                if (PB_covers[i].isClicked)
                {
                    //excecute funtion
                }
            }
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寂寞美少年 2025-02-04 12:59:45

我不太确定“消失”是什么意思。图片框应该隐藏吗?其他元素之间还应该删除空间吗?

如果您只想隐藏图片框,则可以在单击事件中评估发件人。我还将推荐使用用于循环的循环而不是foreach:

PictureBox[] PB_covers = new PictureBox[48];

private void Form1_Load(object sender, EventArgs e)
{
    for(int i = 0; i < PB_covers.Length; i++)
    {
        PB_covers[i].Click += PB_Click;
    }
}

void PB_Click(object sender, EventArgs e)
{
    if (sender is PictureBox pictureBox)
    {
        pictureBox.Visible = false;
    }
}

如果您还想删除元素之间的空间,则需要更改所有盒子的位置。

I am not quite sure what do you mean with "disappear". Should the PictureBox just be hidden? Should the space also be removed between the other elements?

If you only want to hide the PictureBox, you can just evaluate the sender within the click event. I would also recomment to use a for loop instead of a foreach:

PictureBox[] PB_covers = new PictureBox[48];

private void Form1_Load(object sender, EventArgs e)
{
    for(int i = 0; i < PB_covers.Length; i++)
    {
        PB_covers[i].Click += PB_Click;
    }
}

void PB_Click(object sender, EventArgs e)
{
    if (sender is PictureBox pictureBox)
    {
        pictureBox.Visible = false;
    }
}

If you also want to remove the space between the elements, you need to change the positions of all the boxes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文