שתף באמצעות


InitializeComponent - is it necessary

Question

Wednesday, July 13, 2011 3:59 PM

In several code examples from text books and the internet extra data like the following are included:

'This call is required by the Windows Forms Designer

InitializeComponent()

When I copy these code elements without these seemingly uneeded commands, the code seems to run ok without them but it's hard to believe the software developers put them in without needing them.

Is there a reason to keep these mysterious pieces of code and should I be including them in my own applications?  This is one of the confusing parts of the transition from vb6 to vb.net!

 

Thanks

All replies (5)

Wednesday, July 13, 2011 4:57 PM ✅Answered | 1 vote

To my understanding from VS2005 and up, you need to call InitializeComponent() method when you add your own constructors to your windows form project. Also this only apply to windows form application but not console. By the default, the project will automatically called InitializeComponent() from designer.vb page if there is no constructors added by developer

kaymaf

CODE CONVERTER SITE

http://www.carlosag.net/Tools/CodeTranslator/.

http://www.developerfusion.com/tools/convert/csharp-to-vb/.


Friday, July 15, 2011 1:01 AM ✅Answered | 1 vote

Hi Jercook,

If it helps to demystify InitializeComponent

then if you open SOLUTION EXPLORER from the VIEW menu ( CTRL + R ),

then left-click the little + next to Form1.Vb

 

You will see two files;

  • Form1.Designer.Vb
  • Form1.resx

If you double click the Form1.Designer.Vb file

you will see the PRIVATE SUB called InitializeComponent

 

 

Warning: Do not do anything with the content of this file if you don't know what you are doing.

 

 

Here is how the file content looks with a button named Button1 on the Form called Form1

You may notice it is a Partial Class for Form1

meaning it is a part of the Form1 Class code.

In this case the InitializeComponent Sub

sets up the Form.

>>

 

 

<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Form1
 Inherits System.Windows.Forms.Form

 'Form overrides dispose to clean up the component list.
 <System.Diagnostics.DebuggerNonUserCode()> _
 Protected Overrides Sub Dispose(ByVal disposing As Boolean)
  Try
   If disposing AndAlso components IsNot Nothing Then
    components.Dispose()
   End If
  Finally
   MyBase.Dispose(disposing)
  End Try
 End Sub

 'Required by the Windows Form Designer
 Private components As System.ComponentModel.IContainer

 '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()
  Me.Button1 = New System.Windows.Forms.Button
  Me.SuspendLayout()
  '
  'Button1
  '
  Me.Button1.Location = New System.Drawing.Point(63, 55)
  Me.Button1.Name = "Button1"
  Me.Button1.Size = New System.Drawing.Size(75, 23)
  Me.Button1.TabIndex = 0
  Me.Button1.Text = "Button1"
  Me.Button1.UseVisualStyleBackColor = True
  '
  'Form1
  '
  Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
  Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
  Me.ClientSize = New System.Drawing.Size(284, 262)
  Me.Controls.Add(Me.Button1)
  Me.Name = "Form1"
  Me.Text = "Form1"
  Me.ResumeLayout(False)

 End Sub
 Friend WithEvents Button1 As System.Windows.Forms.Button

End Class

 

Regards, John

Click this link to see how to insert a picture into a forum post.

Installing VB6 on Windows 7

XNA is coming to VB.Net

App Hub forums


Friday, July 15, 2011 4:34 AM ✅Answered

Thank you John, for your clarification.  I am finding the learning curve between VB6 and VBNet much geater than I anticipated but with the fine people on the forum, it is slowly starting to make some sense.  Still have a ways to go but it is fun learning about it.

 

Thanks again,

 

Jerry


Friday, July 15, 2011 11:15 AM ✅Answered | 1 vote

Thank you John, for your clarification.  I am finding the learning curve between VB6 and VBNet much geater than I anticipated but with the fine people on the forum, it is slowly starting to make some sense.  Still have a ways to go but it is fun learning about it.

 

Thanks again,

 

Jerry

You might have seen this free book from microsoft 

Upgrading Microsoft Visual Basic 6.0 to Microsoft Visual Basic .NET

kaymaf

 

CODE CONVERTER SITE

http://www.carlosag.net/Tools/CodeTranslator/.

http://www.developerfusion.com/tools/convert/csharp-to-vb/.


Wednesday, July 13, 2011 5:39 PM

Thank you.  So I am probably not commiting a software faux paux by ignoring them unless I start designing my own components or something. Hopefully, if I get were I need them, the editor will complain.

Thanks again.

 

Jerry