How to know when a control is added to the form in Design

  • Thread starter Thread starter Skysurfer
  • Start date Start date
S

Skysurfer

Is it possible to know when a control is added to a form in Design time?

I tryed with ControlAdded, but this is also fired when the program starts.

I want to set some properties only when a control is added to the form and
not when the program starts.

Thanks all.

Skysurfer
 
Is it possible to know when a control is added to a form in Design time?

I tryed with ControlAdded, but this is also fired when the program starts.

I want to set some properties only when a control is added to the form and
not when the program starts.

Thanks all.

Skysurfer

I wonder where you are planning to write your code ? At design time,
when you drag and drop a control, object of the same is created in
background. So the control is already added to the form.
What do you mean by design time ? What actually you want to do ?
 
I created a form, call it MyForm, and I want to use it as a base for all my
forms.
When i drag some controls on a form that inherits from MyForm, i want to set
some properties of the control.
If the control that I drag to the form has a property named
"VisualStyleManager", i want to set this property with a particular value.
Thanks
 
A form has a property called DesignMode, this is set to true when in
design mode.

You can handle the ControlAdded event and just simply check this
property to know whether it is design mode or not.
 
Olie said:
A form has a property called DesignMode, this is set to true when in
design mode.

You can handle the ControlAdded event and just simply check this
property to know whether it is design mode or not.

If I close the form and re-open it, always in Design, the property
DesignMode is True, so the property initialization is fired.
The problem is that if I change the Property, I close the Form, and i open
it again, my property is overwritten with the property in ControlAdded.

Bye

Skysurfer
 
I am not sure but I think the problem is that you changes are getting
overridden with the code behind property values. You will find that in
design mode Visual Studio stores a copy of the properties and sets
them when the form starts up. If you set them in code before
InitialiseComponents then they will get overridden.

Not entirely sure what you meant from your post and got a bit confused
as to when you where in design mode.
 
Is it possible to know when a control is added to a form in Design time?

I tryed with ControlAdded, but this is also fired when the program starts.

I want to set some properties only when a control is added to the form and
not when the program starts.

Thanks all.

Skysurfer

Controls that are part of the form are added in the
InitializeComponents method. Add a Sub New to your form and the IDE
will add a call to InitializeComponents in that method. Create a
form level Boolean field that is initially set to False. Set it to
True in New() after the call to InitializeComponents(). In your
OnControlAdded method the field will be True for controls added by the
user.
 
Back
Top