problems with the embedding of UserControls

  • Thread starter Thread starter programcounter
  • Start date Start date
P

programcounter

Hello,

I have written 2 controls for a SmartDeviceApplication (Control1.dll
and Control2.dll)
Now I will place Control2 on Control1. Both controls are derived from
System.Windows.Forms.Control and so it´s possible to set the Control2
by using the properties "Location" and "Size". During the "Build
Solution" - process of Control1 there are no errors.
But if I place the Control1 in the ToolBox of the .Net - Studio and
drag the Control1 to the Form of the SmartDeviceApplication I get an
error:

--> System.NullReferenceException: Object reference not set to an
instance
of an object!
at System.Windows.Forms.Controls.set_ParentInternal(Control
value)

Following code-fragement in Control1.dll initiate the exception:

--> this.myControl2.Location = new Point(10,10);



Thanks
 
You probably need to place that code elsewhere since your control2 is
obviously not created at that point - but it's difficult to tell how you are
creating it without seeing a code snippet. The actual window which forms the
"canvas" of your custom control is not created until the control is added to
a form, so there are some things you can't do with it in the constructor for
example.

Peter
 
Hello Peter,

thanks for your answer.
At first a code fragment of the controls.

CONTROL2:
namespace Control2
{
public class Control2: System.Windows.Forms.Control
{
//some stuff
}
}


CONTROL1:
namespace Control1
{
public class Control1: System.Windows.Forms.Control
{
private Control2 myControl2;

private void InitializeComponent()
{
this.myControl2= new Control2();
this.Controls.Add(this.Control2);
}
}
}

If I add the Control2 to the Control1 I get the following Exception:

--> "Attempted to store an element of the incorrect
type into the array"
But the exception occurs if I drag the Control1 from the Designer
ToolBox to the Form. (Not during the "Build solution"-process)

Furthermore I have added my Control2 seperatly to the Designer-ToolBox.
In this case I can drag the Control2 to my SmartDeviceApplication and
have full access to all public Methods and properties. So I would say
that the Softwarestructure of my Control2 is correct.
I believe that the error is dependent on the fact, that I will add my
Control2 to Control1.

I hope that you can give me an answer

Thanks
 
Should this:
this.Controls.Add(this.Control2);
not be like this:
this.Controls.Add(this.myControl2);

Cheers
Daniel
 
Back
Top