Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / January 2007

Tip: Looking for answers? Try searching our database.

Is this normal

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Robert Brown - 11 Jan 2007 23:33 GMT
Hi there,

Is it normal for form-based events to fire in "InitializeComponent()". I've
got a "DataGridView" on a form and I set its "CellValueChanged" event using
the forms designer. Ok, VS initializes my event in "InitializeComponent()".
However, "InitializeComponent()" later calls
"ComponentResourceManager.ApplyResources()" which (to my great surprise)
fires my event (which my handler then chokes on since it thinks the window
is already running). Looking at the call stack, the event was fired while
initializing the header text on one of my "DataGridView" columns (from the
resource file). I'm not sure if this particular event should even apply in
this situation but in any case, the form hasn't even been created yet. Is it
therefore normal for some form-based events to be fired while the
constructor is still running. If so then it's potentially dangerous to
define events using the forms designer so we should presumably add them
ourselves in "OnLoad()" typically (or otherwise design all handlers to check
that the window has actually been created which is ugly IMO). Can anyone
elaborate on this situation. Thanks.
Ben Voigt - 11 Jan 2007 23:39 GMT
> Hi there,
>
[quoted text clipped - 9 lines]
> this situation but in any case, the form hasn't even been created yet. Is
> it therefore normal for some form-based events to be fired while the
Yes, that happens.

> constructor is still running. If so then it's potentially dangerous to
> define events using the forms designer so we should presumably add them
> ourselves in "OnLoad()" typically (or otherwise design all handlers to
> check that the window has actually been created which is ugly IMO). Can
> anyone elaborate on this situation. Thanks.

The designer registers events as the last thing it does for any control.
Usually that's good enough.  In a couple cases you have to register the
handler later yourself, I've seen that too.
Robert Brown - 12 Jan 2007 00:31 GMT
>> Is it normal for form-based events to fire in "InitializeComponent()".
>> I've got a "DataGridView" on a form and I set its "CellValueChanged"
[quoted text clipped - 10 lines]
>
> Yes, that happens.

Ok, thanks for the confirmation. It's arguably a questionable design however
since these events really seem to be intended for the post-window (creation)
environment. It's even possible for this problem not to surface until your
program has been released (though in practice it's likely to be caught long
before that).

>> constructor is still running. If so then it's potentially dangerous to
>> define events using the forms designer so we should presumably add them
[quoted text clipped - 5 lines]
> Usually that's good enough.  In a couple cases you have to register the
> handler later yourself, I've seen that too.

Ok, I'll exercise caution from now on. Thanks again.
RobinS - 12 Jan 2007 06:41 GMT
Don't set the event in design mode. Do it in your Form_Load routine.
That will take care of your problem.

Robin S.
-------------------------
>>> Is it normal for form-based events to fire in
>>> "InitializeComponent()". I've got a "DataGridView" on a form and I
[quoted text clipped - 29 lines]
>
> Ok, I'll exercise caution from now on. Thanks again.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.