Form.ActiveForm problem....

  • Thread starter Thread starter Peter Morris
  • Start date Start date
P

Peter Morris

Form activeForm = System.Windows.Forms.Form.ActiveForm;

Form activeMdiChildForm = activeForm.ActiveMdiChild;



This code is in a component of mine and is triggered from various places
within my application. My problem is that when I run the app
Form.ActiveForm is always set, whereas when I am stepping through code using
the debugger it is always null. This results in a NullReferenceException
whenever I am debugging and a line of code executes that directly or
indirectly triggers the event that invokes this code.

Is there some kind of a workaround for this problem? It is very counter
productive!



Thanks

Pete
 
Hi Peter,

That is an unfortunately consequence of how the debugger interact with the
normal execution.

As the debugger is the active form then your program has no active form,
hence the result you see.

A possible workaround would be to place the breakpoint after you get the
reference to the active form.
 
A possible workaround would be to place the breakpoint after you get the
reference to the active form.

That's what I do when I debug that particular code, however, that code is
triggered from multiple places.

My component's method is triggered whenever a new business object is
created, whenever a business object is modified or deleted. It checks if it
(the component) belongs to the active form, if it does it adds the
modification to its list of altered business objects. Unfortunately this
happens just about everywhere! I could be stepping through code in a
business object "Customer.cs" in a separate DLL and when it does something
like this

this.MadeLiveDate = DateTime.Now;

It will modify the object and therefore trigger the code in my component
that is subscribed to all changes in the object space. Basically this means
that it is almost impossible to debug my app.


Pete
 
Peter Morris said:
That's what I do when I debug that particular code, however, that code is
triggered from multiple places.

My component's method is triggered whenever a new business object is
created, whenever a business object is modified or deleted. It checks if
it (the component) belongs to the active form, if it does it adds the
modification to its list of altered business objects. Unfortunately this
happens just about everywhere! I could be stepping through code in a
business object "Customer.cs" in a separate DLL and when it does something
like this

this.MadeLiveDate = DateTime.Now;

It will modify the object and therefore trigger the code in my component
that is subscribed to all changes in the object space. Basically this
means that it is almost impossible to debug my app.

Yes,

It can be a challenge with those conditions
 
Back
Top