Question

I'd like to create a roulette game, and I've made a PictureBox with an image of a roulette table.

I've tried to set Visibility = False in the property and in code:

pictureBox1.Visible = !pictureBox1.Visible;

But when I click on the hidden PictureBox, nothing happens.

Do you have any idea how to make it work?

I want to make it visible from being invisible, by clicking on it.

Was it helpful?

Solution

Once you make the visibility of an Item false, it can no longer be clicked. You can handle the click event on the form or container level, check that the mouse coordinates are contained in the bounds of the PictureBox and use that to make it visible again.

i.e.

//Common eventhandler assigned to all of your PictureBox.Click Events
private void pictureBox_Click(object sender, EventArgs e)
{
    ((PictureBox)sender).Visible = false;
}

private void Form1_Click(object sender, EventArgs e)
{
    foreach (var item in this.Controls) // or whatever your container control is
    {
        if(item is PictureBox)
        {
            PictureBox pb = (PictureBox)item;
            if (pb.Bounds.Contains(PointToClient( MousePosition)))
            {
                pb.Visible = true;
            }
        }
    }
}

OTHER TIPS

Problem with your logic is that once the PictureBox is made invisible, you can no longer click it anymore, since it's no longer on the form, and while you can for sure click the empty space it left, you're still not clicking it.

A possibility would be to, instead of making visible/invisible, to put/remove its background picture, so it appears to have disappeared but in fact it's still there, still able to receive clicks and respond to events.

If you want to make the pictureBox1 not visible, you would write pictureBox1.Visible = false;

Edit: What you are doing should work. I created a winform and put a picture box in it. I double clicked the picture box to make Click event handler and typed pictureBox1.Visible = !pictureBox1.Visible;. When I debug the program and click on the picture box, it turns invisible. I can't think of why it isn't working for you.

Edit: I understand what you want now. To make it visible on the click of it when it is invisible. Unfortunately I don't know how to do that.

A maybe-silly but simple way might be to put one picture box on top of the other. pictureBox1 would be your picture, pictureBox2 would have no picture. Then you could do something like this:

    private void pictureBox1_Click(object sender, EventArgs e)
    {
        pictureBox1.Visible = false;
        pictureBox2.Visible = true;
    }

    private void pictureBox2_Click(object sender, EventArgs e)
    {
        pictureBox1.Visible = true;
        pictureBox2.Visible = false;
    }

You could put each of your PictureBox controls in a Panel control. Then handle the Click event of both the Panel and the PictureBox.

private void panel1_Click(object sender, EventArgs e)
{
    pictureBox1.Visible = true;
}

private void pictureBox1_Click(object sender, EventArgs e)
{
    pictureBox1.Visible = false;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top