Hi all,
I am currently thinking about creating a Component that should be dragged from the ToolBox to a Form/UserControl.
Once it is placed it should iterate through all controls on the Form/Usercontrol where the component was placed on and apply some formatting.
It should be able to automatically figure out its owner and then iterate the controls (best) before they are shown to appy the formatting logic (the iteration already works, so no problem here).
I already had the following:
public Control OwningControl{get;set;}
to be at least able to set the control where I wanted the iteration to start but I always need to fire a function (in the Load event of the owning form) that calls the ApplyFormatting() function of the component to apply the formatting...
It would be great to drag the component to the form/UserControl and then just forget about it...
Maybe adjusting something in the Designer (for my colleagues to use it easily) and not in the code...
Is that possible?
UPDATE:
I figured out that there is a Site (ISite) property that allows interaction with the owning container.
This at least gives me the container at runtime, but I am not able to get all the controls in the container.
Update II
Ok, I made a step foreward thanks to a post I found on the net.
I added the following to get a hand on the owning control of a component, which is in my case a Form or a UserControl:
private delegate void HostingControlChangedHandler(object sender);
private event HostingControlChangedHandler HostingControlChanged;
private void InvokeHostingControlChanged()
{
HostingControlChangedHandler handler = HostingControlChanged;
if (handler != null) handler(this);
}
Control hostingControl = null;
[BrowsableAttribute(false)]
public Control HostingControl
{
get
{
if ((hostingControl == null) && this.DesignMode)
{
IDesignerHost designer =
this.GetService(typeof(IDesignerHost)) as IDesignerHost;
if (designer != null)
{
hostingControl = designer.RootComponent as Control;
}
}
return hostingControl;
}
set
{
if (!this.DesignMode)
{
if ((hostingControl != null) && (hostingControl != value))
{
throw new
InvalidOperationException
("Can't set HostingForm at run time.");
}
}
hostingControl = value;
InvokeHostingControlChanged();
}
}
void DefaultGridFormatter_HostingControlChanged(object sender)
{
_ultraGridDefaultFormatter.HostingControl = this.HostingControl;
Form frm = this.HostingControl as Form;
if (frm != null)
{
((Form)this.HostingControl).Load += new EventHandler(DefaultGridFormatter_Load);
return;
}
UserControl ctr = this.HostingControl as UserControl;
if(ctr != null)
{
if(ctr.ParentForm != null)
{
ctr.ParentForm.Load += new EventHandler(DefaultGridFormatter_Load);
}
}
}
I am now able to grab the owning control and iterate through its child-controls to apply whatever is needed.
When I pull the component from the toolbox to my form the HostingControl is correctly set in InitializeComponent.
Unfortunately it does not work in the design-mode, only at runtime....
Am I missing something?
Is this in general a good approach or does it violates everything that is good in software engineering ;)
Any ideas/comments are kindly appreciated,
cheers
Andy