Implements

The ActiveControl property activates or retrieves the active control on the container control.

In order to receive a valid value from this property, the object that calls it must either contain or be contained in the control it is calling. If one form tries to call another form's ActiveControl properties, it will receive an undefined value. In this case, you need to define your own communication mechanism between the forms to pass this data.

using System;
using System.Windows.Forms;
using System.Drawing;
publicclass MyContainer : ScrollableControl, IContainerControl
{
private Control activeControl;
public MyContainer()
{
// Make the container control Blue so it can be distinguished on the form. this.BackColor = Color.Blue;
// Make the container scrollable. this.AutoScroll = true;
}
// Add implementation to the IContainerControl.ActiveControl property. public Control ActiveControl
{
get
{
return activeControl;
}
set
{
// Make sure the control is a member of the ControlCollection. if(this.Controls.Contains(value))
{
activeControl = value;
}
}
}
// Add implementations to the IContainerControl.ActivateControl(Control) method. publicbool ActivateControl(Control active)
{
if(this.Controls.Contains(active))
{
// Select the control and scroll the control into view if needed.
active.Select();
this.ScrollControlIntoView(active);
this.activeControl = active;
returntrue;
}
returnfalse;
}
}