(c) 2002 Visual Studio Magazine 
Fawcette Technical Publications

Issue: July 2002
Section: Create a Trickle-Feed Application
Author: Zane Thomas

C#	ExpenseApplicationCore's Form1 Implements IExpenseForm 
Listing 1	IExpenseForm requires the Method LoadPanel shown in this code.  RoleControllers invoke IExpenseForm.LoadPanel. These RoleControllers are loaded into the panel on the left side of Form1 when they need to load a new component into the right-side panel.  LoadPanel invokes LoadControl, which invokes LoadAssembly.  LoadAssembly detects whether the application has been updated, and displays a message if it has been.  The user can begin using the updated application automatically by logging off and back on.

//
// IExpenseForm.LoadPanel Implementation
//
// Invoked by RoleControllers to load a control 
//into the right-side application panel
//
public Control LoadPanel(string TypeName)
{
	if( m_CurrentPanel != null )
		EntityPanel.Controls.Clear();
	m_CurrentPanel = LoadControl(TypeName);

	EntityPanel.Controls.Add(m_CurrentPanel);

	m_CurrentPanel.Width = EntityPanel.Width;
	m_CurrentPanel.Height = EntityPanel.Height;

	m_CurrentPanel.Anchor = 
		AnchorStyles.Top | 
		AnchorStyles.Bottom |
		AnchorStyles.Left |
		AnchorStyles.Right;
	return m_CurrentPanel;
}
//
// Loads a named component from an assembly of
// the same name.
//
private Control LoadControl(string TypeName)
{
	//
	// Full assembly name is Path + ClassName + 
	//.dll
	//
	string AssemblyName = TypeName.Substring
		(TypeName.IndexOf('.') + 1) + ".dll";

	Assembly assembly = 
		LoadAssembly(AssemblyName);
	Type type = assembly.GetType(TypeName);

	return (Control)
		Activator.CreateInstance(type);
}
//
// Loads an assembly.
//
// Copies the assembly from the bin directory if 
//it does not exist in the application directory.
//
// Displays a message box if the assembly is out 
//of date.
//
private Assembly LoadAssembly(string Name)
{
	Assembly assembly = null;

	if( File.Exists(Name) == false )
	{
		File.Copy(m_PartsPath + Name, Name, true);
	} else if( AssemblyIsOld(Name) ) {
		MessageBox.Show
			("application update available");
	}
	assembly = Assembly.LoadFrom(Name);
		
	return assembly;
}

C#	Load New Components Into the Form
Listing 2	This code comes from the Employee Role Control Project.  RoleController buttons can load new components into the right-side of the form by invoking the LoadPanel method.  The controller can then interact with the component using the interfaces it implements.

private void Selector_Click(object sender, 
	System.EventArgs e)
{
	//
	// Load Expenses Selector
	//
	IExpenseSelectorControl c = 
		(IExpenseSelectorControl)
		m_ExpenseForm.LoadPanel
		("ExpenseApplication.
		EmployeeExpensesSelector");
	//
	// Hide employee name column
	//
	c.HideEmployeeColumn(true);
	//
	// Modify main form's header
	//
	m_ExpenseForm.Header = 
		"Select Report or Add New";
}

