Vincent
Vincent

Reputation: 524

PictureBox visible property does not work... help please

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...

Upvotes: 7

Views: 18552

Answers (3)

Daya Paari
Daya Paari

Reputation: 96

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;
}

Upvotes: 5

Samuel Slade
Samuel Slade

Reputation: 8613

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.

Upvotes: 1

Iridium
Iridium

Reputation: 23721

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.

Upvotes: 5

Related Questions