locked
C# - Code CS0103: The name CS0103'InitializeComponent' does not exist in the current context RRS feed

  • Question

  • I am fairly new to C# and mainly I am coping samples to see them run.

    I have the following lines in one separate file:

    using System;
    using System.Windows.Forms;
    using Excel = Microsoft.Office.Interop.Excel;

    namespace The_OACI_Form
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent(); <= This is the line that generates the CODE CS0103 so there is a red wiggling line underneath.

            }

    MS VS 2015 C# generated the following code in another file automatically while doing a Windows Form Application:

    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;


    namespace The_OACI_Form
    {
        public partial class Searcher : Form
        {
            public Searcher()
            {
                InitializeComponent(); <= This one doesn't generate an ERROR CODE nor has the wiggling red line underneath.
            } 

    I do not get it. Why it does accept one and not the other identically under same circumstances? 

    Regards,

    Thursday, April 28, 2016 5:21 PM

Answers

  • Hi

    InitializeComponent() method will be present in the <yourformname>.designer.cs which will be present in the same directory where your form's .cs file is present.

    Please check whether the Class name and the Namespace name in the designer.cs is the same.

    Form1.cs

    Form1.Designer.Cs

    Solution Explorer


    Thursday, April 28, 2016 6:56 PM

All replies

  • A form that is created by Visual Studio consists of several files, such as Form1.cs, Form1.Designer.cs, Form1.resx. The file Form1.Designer.cs contains a definition of special function InitializeComponent, which is created by Form Designer when you edit the form (e.g. place a button). These files can be found on disk and in Solution Explorer.

    When you created the file Form1.cs manually, you obviously did not accomplish the required structure of files and functions. As a result, Visual Studio does not recognise this unexpected situation.

    You should create an empty form using Visual Studio. Then you can add more code and controls. Usually you only change manually the Form1.cs file. The other files are maintained by Visual Studio.

    If you are interested what InitializeComponent is in your good project, then select it and press <F12>; but do not edit it manually.

    Thursday, April 28, 2016 6:54 PM
  • Hi

    InitializeComponent() method will be present in the <yourformname>.designer.cs which will be present in the same directory where your form's .cs file is present.

    Please check whether the Class name and the Namespace name in the designer.cs is the same.

    Form1.cs

    Form1.Designer.Cs

    Solution Explorer


    Thursday, April 28, 2016 6:56 PM