Click here to Skip to main content
13,297,048 members (63,797 online)
Rate this:
Please Sign up or sign in to vote.
Which Event is occurred before Form Load event??
Because I have a timer control for which I have written timer.start() method in form load event and current time is displayed in Label Control. Timer starts when the form Loads but it takes few seconds to show the time, so is there any other way to show the time fast.

Thanks in Advance !!!
Posted 15-Aug-12 5:13am
Wes Aday 15-Aug-12 10:28am
Sure there is: yourLabel.Text = DataTime.Now() or any other format of your choice. You have not given enough information to give you much of an answer.
Ank_ush 15-Aug-12 10:42am
yes you are right I have written label.text = DateTime.Now.ToString() in Timer_Tick event. and called Timer.Start method in form Load event.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Form.Load occurs when a form has been completely constructed, all controls have been initialised, and the form is ready to be shown. That only happens when you try to make a form visible (i.e. call Show or ShowDialog on it) for the first time.

You can start things in the form's constructor. However, calling anything which relies on controls being completely initialised before the Load event is received is a bad idea, because they might not be (i.e. they might not yet have window handles yet, data binding won't have happened, etc).

In this case you should set the text of the label in the constructor or in the Load event handler. Timers only start raising events when the interval has elapsed, so you should set the label to a sensible initial value as well as starting the timer.
Ank_ush 15-Aug-12 11:01am
If I set the Label text to Date in form load event then it doesn't show the time changing.
BobJanova 15-Aug-12 11:05am
You need to do that -and- set up a timer.
Ank_ush 15-Aug-12 11:07am
That's ok. If I mention it in Form load event then it doesn't show empty space when Form loads.
Sergey Alexandrovich Kryukov 15-Aug-12 19:28pm
Correct, my 5, but I added my explanations and recommendations. Please see my answer, you might find it interesting.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

In addition to the Solution 1 by BobJanova:

This way, this event is a "fake" one. I mean, yes, this is a real event, but if it was not exist, the user's power would not be changed a bit. Unlike other events which are invoked by the code the user cannot get into. It also important to understand that an event cannot be invoked from anywhere except the class where the event instance was declared, not even in a derived class, so the user would not have any hook to the event except adding a handler to the invocation list of the event instance.

This is not the case for the event Form.Load. Basically, everything the user wants to add to the handle of this event, can simply be called from withing a form's constructor at the very end. This is exactly what I personally do; never handle this event, because I think this is unnatural and excessive.

So, why this event was introduced? I don't really know, can only speculate. It makes ASP.NET, System.Windows.Forms and WPF libraries a bit more similar, so it would exploit the user's habit's of using Page.Load. Also, it mimics the Designer-based "development", where an event handler is added by the Designer. I see only one possible motivation for that in Microsoft: pleasing the weak minded was always on of the priorities in this company. It makes an illusion of "easy" development, and it is really easier… when it comes to the demo applications and simplest ad-hoc project. In real development, Designer means too much of manual repetitive work with poorly maintainable code. What you do, depends on your choice, your concrete case and individual taste.

BobJanova 16-Aug-12 5:42am
If you want to do form-initialisation things that require controls to be fully set up, you need to do them in a Load handler. For example data binding on DataGridViews doesn't run until the control is 'loaded'; I've had to add special processing in Form.Load for that in the past.

Also, it's a good time to kill a splash screen.
Sergey Alexandrovich Kryukov 16-Aug-12 12:45pm
I don't really agree. What makes you think so. I bet I can implement anything which could be done with this event without it, by the reason I tried to explain. Including splash screen. And I still don't see a value of this event for me.
BobJanova 17-Aug-12 4:57am
Between the end of the constructor and Load being called, the operating system and Framework do a whole bunch of stuff behind the scenes that you can't interfere with. That includes making OS controls and window handles, so if you want to do anything which requires that the control actually exists, you can't do it in the constructor.

All that setup also takes quite a bit of time, so if you kill a splash screen in a constructor then there will be an appreciable gap in time before the form actually displays.
Sergey Alexandrovich Kryukov 17-Aug-12 10:48am
That's perfectly true, but can you tell me the functional difference between the case I do it in constructor and in the Load event handler? Just one example would suffice. Besides, one can always handle the event Shown, which is really unavoidable in certain cases, when acting at the end of construction time is too early.

If you give me a functionally essential example, I'll gladly agree and learn the lesson...

Thank you,
BobJanova 20-Aug-12 11:51am
It depends what you mean by 'functionally essential'. I don't think there's a functional difference that you can't cover either in the constructor or in a Shown event handler with a first-use guard. But the time between the end of the constructor and Loaded being called can be several seconds, so if you've got UI to update when something is 'about to happen' there can be a business case for using Loaded.
Sergey Alexandrovich Kryukov 20-Aug-12 15:31pm
By "functionally essential" I mean implementation of some behavior which is not possible if you don't handle the event Load. I think that you cannot point out such case as I would be able to effectively implement the behavior you can device without using this event; but of course it you give me such example, I'll readily admit my mistake.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web02 | 2.8.171207.1 | Last Updated 15 Aug 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100