Sign in to follow this  

[.net] When a Form is displayed?

This topic is 3863 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi! I have a custom form derived from the Form class. I want to do the startup work done while a control is being displayed on the form with a "Please wait..." sign. And all this without a backgroundworker. I've tried the following: override OnShown, call the base.OnShown first, and then fire my AfterShownEvent. I attach to this event in the form instance, but unfortunately the form is only drawn to the screen after the event is finished. I bet the job done in the event prevents the Form to be painted onto the screen. How to solve this? Thanks in advance!

Share this post


Link to post
Share on other sites
It does sound like you're not giving the form a chance to render itself. If you're doing stuff in the OnShown even move it to somewhere after that event. Show the form, give it a chance to render itself, then do whatever startup work you need to do somewhere outside of this form.

Share this post


Link to post
Share on other sites
Quote:
Original post by Machaira
It does sound like you're not giving the form a chance to render itself. If you're doing stuff in the OnShown even move it to somewhere after that event. Show the form, give it a chance to render itself, then do whatever startup work you need to do somewhere outside of this form.


Please name me an event that automatically occurs after the form is actually rendered. I've tried to overload both OnShow and OnPaint to let the Form draw itself before i go on with my stuff... but nothing...

Maybe somehow i need to ensure that my code runs after all startup events are consumed... maybe i know how... lemme try... nope didnt work.

still waiting any suggestions...

[Edited by - orbano on May 22, 2007 10:37:03 AM]

Share this post


Link to post
Share on other sites
The simplest thing, since its a custom control, would be to impliment two panels with no borders. In design mode, all the user sees is the second 'normal' panel. But when the form is created, it defaults to showing the 'loading' panel.

Typically though, if theres something going on in your form that takes a while to do, its good to pop up a progress dialog. I created a progress dialog form for a project i'm doing at work. It takes about 10-30 seconds to get all the data it needs from the database into the form, so when you click the button that shows the form, while the data is loading, you see a detailed 'loading dialog' with a progress bar and a status label. With this method, your loading code should be in its own function (as it should be anyways)

Button Click:
* Create the progress dialog (always on top), set max steps or whatever
* Create the main form and disable it
* Call your loading functions, stepping and repainting the progress dialog
* Destroy the progress dialog and enable the target form (best to put this in the "finally" section, and the above in the "try" section of a try/finally block so your app doesn't get stuck if something weird happens)

If you want to get fancy you can use an array of callbacks or whatever, but I did it the simplest way :)


I think having a 'loading message' rendered onto the form itself would seem kindof weird, unless it was a progress bar on a bottom status bar or something (ala Internet explorer and friends).

Share this post


Link to post
Share on other sites
Form.Load might work out, or maybe Form.VisibleChanged (iirc). I'm not 100% sure about the first one, but I think the latter fires after the form has become visible and is fully rendered.

Share this post


Link to post
Share on other sites
Quote:
Original post by essial
Typically though, if theres something going on in your form that takes a while to do, its good to pop up a progress dialog...


This is a basic feature of my form. You have to set a flag whether to run az init in a bgworker, and one flag whether you want a progressdialog to be show.

The problem is, i have many init stuff that modifies the form's controls. It lasts only for up to 3-4 seconds, but for that time i want my form up and showing a panel: "Please wait...".

edit:
Btw i think my method is more elegant:
In the Form's Shown, create a special dialog form, that asks for a bgworker parameter. This form closes when the bgworker is either finished or cancelled.
The bgworker dowork event calls the form's public virtual WorkOnLoad(bgworker); method.
Also the messag form handles progress changed events, sets a special (automatcally iterpolatedly advancing) progressbar, and if hte userstate is a string, displays it on a label.

:)

Share this post


Link to post
Share on other sites
Quote:
Original post by remigius
Form.Load might work out, or maybe Form.VisibleChanged (iirc). I'm not 100% sure about the first one, but I think the latter fires after the form has become visible and is fully rendered.


neither works. no attaching to any of the events works...

Share this post


Link to post
Share on other sites
In your Form_Load event you could just have a this.Show(); and this.Activate(); that will show the form and activate it for input, then after that do whatever you want in the event handler.

Share this post


Link to post
Share on other sites
Quote:
Original post by Machaira
What exactly are you doing?!? How can we make suggestions when we don't have enough info?


I do nothing. I WANT something, and need an exact point in my form's, where i can be sure that my form is visible and can call a function that does its job with the controls.

Share this post


Link to post
Share on other sites
Quote:
Original post by Headkaze
In your Form_Load event you could just have a this.Show(); and this.Activate(); that will show the form and activate it for input, then after that do whatever you want in the event handler.


thanks i will try tomorrow. good night or whatever time of day you have :)

Share this post


Link to post
Share on other sites

This topic is 3863 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this