'how do i make a picture box hide and show on click within an array?
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
}
}
}
Solution 1:[1]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |