1

I want to use InitializeComponent() when I create a custom control (to be sure everthing is initialized before I use it), but the compiler says it's not declared. But my Designer.vb contains a sub:

<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

with all new instances I created.

Why can't I call that?

edit this is how I call InitizialeComponent:

Public Class CustomControlsTextBox : Inherits TextBox
Public Sub New()
    MyBase.New()
    InitizialeComponent() 'this function is not declared
End Sub
End Class
MaBi
  • 1,372
  • 3
  • 13
  • 19
  • `Private Sub` means it is not accessible from outside the class. http://msdn.microsoft.com/en-us/library/76453kax.aspx – Tim Schmelter Aug 26 '13 at 14:52
  • Are you trying to call it from elsewhere? (note that it's `Private`) YOu should rely on the constructors. – SLaks Aug 26 '13 at 14:52
  • thanks, I think that's not the problem. Before I asked here I also tried it with public sub. May someone could post an example of he/she would use that sub :) – MaBi Aug 26 '13 at 15:00
  • @SLaks: MyBase.new() should be enough or? – MaBi Aug 26 '13 at 15:01

2 Answers2

2

InitializeComponent() is private, and can only be called from inside that class. It is called by default by the usercontrol constructor, like this:

Public Sub New()

    ' This call is required by the designer.
    InitializeComponent()

    ' Add any initialization after the InitializeComponent() call.

End Sub

Note that you will only have to call InitializeComponent() yourself if you overload the constructor. Default constructor does it by itself.

MACN
  • 196
  • 7
  • 13
  • "Note that you will only have to call InitializeComponent() yourself if you overload the constructor. Default constructor does it by itself." This is a very useful information, thank you! I've updatet my question and posted the code, how I use it. – MaBi Aug 26 '13 at 15:52
  • that means, I don't have to call it in my example. Correct me if I'm wrong :) – MaBi Aug 26 '13 at 15:56
  • @MaBi mybase.new is not usually necesary, except in special circunstances (i.e. the constructor of the control you are inheriting for requires a parameter). InitializeComponent() is not necesary unless you need to do something inside the constructor (other than calling InitializeComponent()). In the your example, since you don't do any of those things, you do not need to even overload the constructor. You could delete it if you wish. Only If you do add something to the constructor, you will need to call InitializeComponent(). – MACN Aug 26 '13 at 16:21
  • k, thanks. But nevertheless it's strange that Compiler throws that error if I do. – MaBi Aug 27 '13 at 12:48
0

You should not depend on your Designer.vb's InitializeComponent. Rather, create a new constructor in your Form1 which will call InitializeComponent()

For example:

Public Class Form1

    Public Sub New()
        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        'Type all your code here! This code will be executed before the "FormLoad" if you call your new form
    End Sub
End Class

Now whenever we use the following code:

Dim NewFrm1 As New Form1

The New constructor in Form1 will be called.

Alex
  • 4,821
  • 16
  • 65
  • 106