Properly display Panel or custom form when activating OnClick event

1

Background:

I am generating a Windows Form application whose main functionality is to consult information through a web service.

The design of the (controls) form consists of:

  • (1) TextBox
  • (1) Button

Due to the latency of the response, it is necessary to inform the user that once the button is pressed to activate the request the system will take a while to respond. According to the tests I have done, the request takes between 3 to 5 seconds (even a little more) .

For this, I intend to use one of the following options:

  • Custom MessageBox so that once the request is completed is automatically closed.
  • Hidden panel to be activated (Visible = True) before executing the request; When the application is completed, it returns to its previous state (Visible = False).
  • Second form (called FrmMsg ) invoked as shown in video .
  • The second form FrmMsg has the following controls:

    • PictureBox (with a .GIF file)
    • Label "with text set by default".
    • Button (to close the form) .
    • Property FormBorderStyle = None .

    The design of the alternative solutions that I have described looks like this:

    Problem:

    I have chosen (for reasons of time for development) to use option 2 or 3 above; however, when you activate the button to make the request, the following problems appear:

    • With the Hidden Panel , it takes time to visualize. The request takes between 3-5 seconds to complete and I consider that (even though the property Visible changes from False to True ) the interface of the program is frozen and therefore , no change is visible (until the request is completed).
    • With the Second form FrmMsg , it is invoked, but the controls that this form has are translucent = the spaces destined for the controls are empty.

    I tried to add the following line of code in different parts of the logic:

    System.Threading.Thread.Sleep(1500); // 1.5 segundos.
    PanelOculto.Visible = true;
    

    But the visualization (either of the Panel or of the second form FrmMsg ) still does not generate the expected result.

    What can be done to display the Panel and / or the second form correctly?

        
    asked by Mauricio Arias Olave 19.07.2016 в 16:52
    source

    1 answer

    2

    > > the program's interface is frozen and therefore, no change is visible

    This happens because the thread of the UI is blocked, you could intersperse the line

    Application.DoEvents ()

    to return the control to the UI and thus update the panel.

    Another option would be to use the

    BackGroundWorker

    with this you could launch the process in another thread and thus not block the UI and show the movement of the panel. Use the event RunWorkerCompleted to move to invisible the panel.

    > > > the image containing the PictureBox control is a .gif, which is not "animated"

    That's a common problem, just load the gif from code using

    Picturebox1.Image = Image.FromFile("ruta gif");
    

    You could put this code in the form load

        
    answered by 19.07.2016 / 17:03
    source