PictureBox visible property does not work... help please
Asked Answered
C

3

7

I am using window app and C#.. i have a picture which is invisible at the start of the app.. when some button is clicked, the picture box has to be shown..

i use this coding but the picture box is not visible

private void save_click(object sender, EventArgs e)

{

      pictureBox1.Visible = true;
      pictureBox1.Show();

      //does the work here 
      //storing and retreiving values from datadase

     pictureBox1.Visible = false;
     pictureBox1.Hide();
}

P.S... in the picture box i am showing a gif.. so the user will know that some work is going on in the background.. It will take long time for the function to finish...

Coomb answered 2/12, 2011 at 12:56 Comment(0)
C
5

Assuming that the saving to the database takes some time, you should be doing it asynchronously using BackgroundWorker, hiding your PictureBox once the operation completes.

The reason that the image is not showing currently is because while your long-running save operation is occurring, Windows messages are not being processed, and so your form will be unresponsive to user input and not perform repaints. When the save operation finishes, and messages start being processed again, the picture box has already been hidden again.

Cuttie answered 2/12, 2011 at 13:7 Comment(0)
W
5

To avoid using multi-threading, all you can do is pictureBox1.Refresh(); after pictureBox1.Visible = true; as below:

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

    //does the work here 
    //storing and retreiving values from datadase

        pictureBox1.Visible = false;
}
Wateriness answered 19/5, 2015 at 13:56 Comment(0)
I
1

Your picture box will not be displayed because you are running other operations on the UI thread during the time you want the picture box to be displayed. The UI will not be re-painted (showing the picture box) until the UI thread becomes free - i.e. after your method.

To overcome this, you need to first show the picture box, then fire off a thread to run your operations on (this will allow WinForms to happily continue interacting and painting the UI), then finish with a call back to the UI thread to hide the picture box.

Refer to this StackOverflow Question for help on this multithreaded execution process.

Ichor answered 2/12, 2011 at 13:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.