Adding code to InitializeComponent in a UserControl

  • Thread starter Thread starter Scott Kilbourn
  • Start date Start date
S

Scott Kilbourn

Hi,

I added some custom code to the InitalizeComponent function of a control
that I'm working on. Every once in a while, I've noticed that the code that
I added disappears. I finally tracked it down, and it seems that the code
disappears whenever I make a change to the UI of the control. For instance,
I just resized the control in the IDE, and my code disappeared.

Is this normal behavior? Is there a better place for this code? The code
that I inserted into InitializeComponent has to run before the OnSizeChanged
event runs.

Thanks
 
Scott,
Did you read the comment just before the routine? :-)

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()

Sometimes the designer will put your controls (variables) between the
comments & InitializeComponent.
Is this normal behavior?
Yes, the routine belongs to the Designer, it will be replaced when you
modify the control in the designer. As the above comment states, do not
modify the InitializeComponent routine!
Is there a better place for this code?
Yes, put your code in the constructor, after the call to
InitializeComponent, there is a comment indicating where.

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

' **** put your code here **** '

End Sub

Hope this helps
Jay
 
* "Scott Kilbourn said:
I added some custom code to the InitalizeComponent function of a control
that I'm working on. Every once in a while, I've noticed that the code that
I added disappears. I finally tracked it down, and it seems that the code
disappears whenever I make a change to the UI of the control. For instance,
I just resized the control in the IDE, and my code disappeared.

Is this normal behavior? Is there a better place for this code? The code
that I inserted into InitializeComponent has to run before the OnSizeChanged
event runs.

Did you realize the comments created by the designer automatically? Add
the code to the control's constructor (before the call to
'InitializeComponent').
 
Thanks for the responses. I guess I'm a bit overwhelmed by how different
this stuff is after so many years working with VB6. ;)
 
Back
Top