Adding a view to another view

Topics: CAB & Smart Client Software Factory
Nov 30, 2007 at 2:07 PM
Hi there

I am in serious need of some help. I have created a Smart Client Application and added a module to it called "ApplicantModule". I then added two views to the module, one is called ApplicantDetailsView and the other is called ApplicantAddressDetailsView. When I open the UI designer for ApplicantDetailsView I want to select the ApplicantAddressDetailsView control from the toolbox and place it on the ApplicantDetailsView. When I try this I get the following error :

Failed to create component 'ApplicantAddressDetailsView'. The error message follows:
'System.NullReferenceException: Object Reference not set to an instance of an object.
at MySmartClient.ApplicantModule.ApplicantAddressDetailsView.OnLoad(EventArgs e).....

What does this mean and what can I do?
Nov 30, 2007 at 5:02 PM

rltromp wrote:
Hi there

I am in serious need of some help. I have created a Smart Client Application and added a module to it called "ApplicantModule". I then added two views to the module, one is called ApplicantDetailsView and the other is called ApplicantAddressDetailsView. When I open the UI designer for ApplicantDetailsView I want to select the ApplicantAddressDetailsView control from the toolbox and place it on the ApplicantDetailsView. When I try this I get the following error :

Failed to create component 'ApplicantAddressDetailsView'. The error message follows:
'System.NullReferenceException: Object Reference not set to an instance of an object.
at MySmartClient.ApplicantModule.ApplicantAddressDetailsView.OnLoad(EventArgs e).....

What does this mean and what can I do?



What it means is that an object somewhere is null, when it shouldn't be. Which object I cannot tell - I'd need to see the code and the entire exception, which should give you a line number in the code where the null reference is causing the problem.

But the larger issue here is that you're trying to drag-and-drop CAB SmartParts onto each other, and that is not the way the CAB is designed to be used. CAB is not a drag-and-drop framework for UI applications. It is designed as a composite framework where the UI is composed dynamically at runtime. What that means is that you don't drag and drop components to build the UI. Instead, you build your components one by one, and then compose them together at runtime through code.

If you want to put your AddressDetailsView on your ApplicationDetailsView then the thing you need to drag-and-drop is a SmartPartPlaceHolder onto your ApplicationDetailsView. The SmartPartPlaceHolder needs to have a SmartPartName property - you need to set this to the string name you plan to give the AddressDetailsView. So, something like "AddressDetailsView".

Now, in code, you need to create the two views. The AddressDetailsView needs to be created first, and then the ApplicationDetailsView (with the SmartPartPlaceHolder on it) should be created second, like so:

IApplicationAddressDetailsView addressDetailsView = SmartParts.AddNew<ApplicationAddressDetailsView>("AddressDetailsView");
IApplicationDetailsView applicationDetailsView = SmartParts.AddNew<ApplicationDetailsView>("ApplicationDetailsView");

CAB will detect that the ApplicationDetailsView has a SmartPartPlaceHolder on it. It will check the SmartPartName property of the SmartPartPlaceholder. It will realize that a view exists with the same name, and it will dynamically add that view to the SmartPartPlaceholder.

The key thing to realize is that you don't drag-and-drop views in CAB. You show them in Workspaces or SmartPartPlaceHolders. This allows you to compose your screen at runtime, and change views during the lifetime of the application.