PictureBox visible property does not work… help please

前端 未结 3 1607
北荒
北荒 2021-01-13 00:10

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 thi

相关标签:
3条回答
  • 2021-01-13 00:52

    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.

    0 讨论(0)
  • 2021-01-13 00:57

    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;
    }
    
    0 讨论(0)
  • 2021-01-13 01:03

    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.

    0 讨论(0)
提交回复
热议问题