locked
Is "Loaded" event save to use FindControl? RRS feed

  • Question

  • Hello,

    we use FindControl to find some grids and hide the header for them like:

                AddHandler Me.FindControl("grdPfuscherbeziehungAkt").ControlAvailable, AddressOf Set_Datagrid_HideHeader
                AddHandler Me.FindControl("grdPfuscherbeziehungAktZusatzmaßnahme").ControlAvailable, AddressOf Set_Datagrid_HideHeader
    

    I know that it is not possible to use "Findcontrol" in the InitializeDataWorkspace event because sometimes this results in an error.

    Is it save to use "Findcontrol" in the Loaded event?
    (it fires after InitializeDataWorkspace and before Created)

    the reason why I want to do this not in the Created event is the problem with the "flickering" after the screen is laoded - because I hide the header of some grids and it looks very bad...

    robert

    Tuesday, December 4, 2012 8:42 AM

Answers

  • I always use the Created method to set the visibility of any controls on a screen. If you put the code in any method that fires before the screen's Created method, the controls aren't guaranteed to be available. It might work "most of the time", but occasionally you might get an unexplained error. These errors are very hard to track down, because they won't necessarily happen consistently. You might not even get an error while debugging, but then the screen starts throwing exceptions in the deployed application.

    I strongly recoomend against putting any FindControl calls in any method that fires before the screen's Created method.

    The way I handle the "flickering" that you mention, is to set the control's Visible property to false in the designer. It's a compromise, sure, but it's an acceptable one to me.


    Yann - LightSwitch Central - Click here for FREE Themes, Controls, Types and Commands
     
    Please click "Mark as Answer" if a reply answers your question. Please click "Vote as Helpful" , if you find a reply helpful.
     
    By doing this you'll help others to find answers faster.

    • Proposed as answer by Konrad Neitzel Tuesday, December 4, 2012 4:41 PM
    • Marked as answer by Mad Rian Tuesday, December 4, 2012 4:45 PM
    Tuesday, December 4, 2012 1:28 PM
    Moderator

All replies

  • I always use the Created method to set the visibility of any controls on a screen. If you put the code in any method that fires before the screen's Created method, the controls aren't guaranteed to be available. It might work "most of the time", but occasionally you might get an unexplained error. These errors are very hard to track down, because they won't necessarily happen consistently. You might not even get an error while debugging, but then the screen starts throwing exceptions in the deployed application.

    I strongly recoomend against putting any FindControl calls in any method that fires before the screen's Created method.

    The way I handle the "flickering" that you mention, is to set the control's Visible property to false in the designer. It's a compromise, sure, but it's an acceptable one to me.


    Yann - LightSwitch Central - Click here for FREE Themes, Controls, Types and Commands
     
    Please click "Mark as Answer" if a reply answers your question. Please click "Vote as Helpful" , if you find a reply helpful.
     
    By doing this you'll help others to find answers faster.

    • Proposed as answer by Konrad Neitzel Tuesday, December 4, 2012 4:41 PM
    • Marked as answer by Mad Rian Tuesday, December 4, 2012 4:45 PM
    Tuesday, December 4, 2012 1:28 PM
    Moderator
  • OK Yann, that sounds wise - it would be good that there will be an event in a future release which fires before the screen is visible and all controls are availible...

    robert

    Tuesday, December 4, 2012 3:59 PM
  • Hi,

    the drawback by setting the control isvisible=true in the Created event is that the user sees a white space instead of the grid until the screen is completely loaded...

    also not a good solution :-(

    Tuesday, December 4, 2012 4:49 PM
  • You know what to do if you have a suggestion for an improvement for a future version. :-)

    If waiting for the grid to be populated causes such a problem, you could always disable autoexecuting of the datagrid's query, then load the query data when you're ready.


    Yann - LightSwitch Central - Click here for FREE Themes, Controls, Types and Commands
     
    Please click "Mark as Answer" if a reply answers your question. Please click "Vote as Helpful" , if you find a reply helpful.
     
    By doing this you'll help others to find answers faster.

    Wednesday, December 5, 2012 5:27 AM
    Moderator