Missing presenter object

Topics: CAB & Smart Client Software Factory
Jun 13, 2007 at 3:26 PM

I have a problem where the overridden 'OnLoad' method in my 'View' (ie my SmartPart) is getting called before the presenter object is set in the default 'Set' property provided by the "Add View with presenter" recipe. Because the presenter object is null, I get a NullReferenceException and I cannot view my SmartPart.

What is happening is:
1) My SmartPart is being added to the current WorkItem within my custom WorkItemController class.
2) The 'InitializeComponent' method is called by the constructor of the SmartPart.
3) The code jumps out of the 'InitializeComponent' method (for no obvious reason) on a line that reads: "this.Font = new System.Drawing.Font("Tahoma", 8F);"
4) The code runs through 30 to 40 non-debuggable methods internal to System.Windows.Forms.
5) The code enters the "OnLoad" method of my SmartPart, where it bombs with a NullReferenceException on the line: "_presenter.OnViewReady();" As yet, the Set Presenter property on the SmartPart has NOT been called by the ObjectBuilder.

This code has been working fine in the past - it is very sudden that this problem has occured. It occured before when I added a custom UserControl with simply a few TextBoxes on it. Removing the control also removed the problem. But now, the problem is caused on the line where it sets the SmartPart's font!

I have no idea why the code leaves the 'InitializeComponent' method of my SmartPart, nor do I know why it suddenly enters 'OnLoad' before it is able to use the 'Set' property of the Presenter. I have my exception settings to debug under any circumstances, but it no exception seems to ever be raised - the code simply jumps into the 'OnLoad' method at completely the wrong time!

I would be grateful if someone could advise me on this issue, because the behaviour is extremely strange!

Jun 14, 2007 at 6:21 PM
Hi Russell,

Some controls fire the OnLoad event of their parents when they are assigned. A simple workaround could be to inject the presenter in the View’s constructor, something like:

public View(CreateNew ViewPresenter newPresenter)
_presenter = newPresenter;
this.Font = new System.Drawing.Font("Tahoma", 8F);

This way the _presenter will be assigned before the control firing the onLoad event.

You can see this thread for further information: http://www.codeplex.com/smartclient/Thread/View.aspx?ThreadId=7559

Hope it helps!

Luciano G. Panaro