Hey I hope someone can help me with a clean solution for the following
puzzle:
I have written a custom toolboxitem that overrides the
CreateComponentsCore() operation that returns two objects, a subclassed
label and a subclassed textbox. The purpose is to create a label and a
textbox that represent a database field (something similar to Microsoft
Access when dragging and dropping a field on a form).
Now my problem is how to position the two controls? In the
CreateComponentsCore() you cannot get the location on the designer since
they are not yet dropped on the designer yet. This means that both controls
will be created at the exact same location thus overlapping eachother. Is
there a clean way to position both controls in the CreateComponentsCore() so
that the textbox control will be positioned behind the label control?
I was thinking of adding a property named ShiftLeft to the subclassed
TextBox, set the value of ShiftLeft in the CreateComponentsCore() and in the
constructor of the subclassed Textbox make sure that TextBox.Left =
TextBox.Left + ShiftLeft but there has to be a better way (?)
Also I do NOT want to create a user control consisting of my custom label
and custom textbox because after creating the user should be able to move
label and textbox around as two seperate components.
Many thanks for any leads :)
Gabriel Lozano-Morán
Real Software
Gabriel Lozano-Morán - 03 Nov 2005 09:48 GMT
I believe that I have found a possible solution for my problem I hope it
might help someone:
...
private delegate void SetRelativeMethodHandler(Control label, Control
control);
...
protected override IComponent[] CreateComponentsCore(IDesignerHost host)
{
Control control = (Control) host.CreateComponent(...);
Control label = (Control) host.CreateComponent(..);
Control c = host.RootComponent as Control;
...
c.BeginInvoke(new SetRelativeMethodHandler(OnSetRelativePosition), new
object[] {label, control});
...
return new IComponent[] {control, label};
}
private void OnSetRelativePosition(Control label, Control control)
{
// When this is invoked we have the location of both controls
...
}