PictureBox visible property not working ... help please

I am using a windowed application and C #. I have an image that is invisible at the beginning of the application .. when any button is pressed, it is necessary to display the image window.

I use this encoding, but the window with the image 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(); } 

PS .. in the image window I show gif .. so that the user will know that some work continues in the background. It takes a long time to complete the function ...

+6
source share
3 answers

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

The reason the image is not currently displayed is because Windows messages are not processed during a long save operation, and therefore your form will not respond to user requests and will not redraw. When the save operation completes and messages begin to be processed again, the image window is already hidden.

+5
source

To avoid using multithreading, 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; } 
+2
source

Your snapshot will not be displayed because you are performing other operations on the user interface stream while the image is being displayed. The user interface will not be redrawn (a window with an image is displayed) until the user interface stream becomes free - i.e. Following your method.

To overcome this, you first need to show the image window and then start the thread to perform your actions (this will allow WinForms to happily continue interacting and drawing the user interface), and then complete the callback to the user interface to hide the image window.

See this fooobar.com/questions/902928 / ... for help in this multi-threaded execution process.

+1
source

Source: https://habr.com/ru/post/902926/


All Articles