Hi I am New to C# i Am trying to create a grade book form but I cannot build successfully because of the following error.
The name 'InitializeComponent' does not exist in the current context

// Fig. 19.7: GradeBook.cs
// A reusable Windows Form for the examples in this chapter.
using System;
using System.Windows.Forms;

namespace GradeBook
{
   public partial class GradeBook : Form
   {
      protected int TextBoxCount = 5; // number of TextBoxes on Form

      // enumeration constants specify TextBox indices
      public enum TextBoxIndices
      {
         
         
         LAST,
 	     FIRST,
	     ID,
	     CLASS,
	     GRADE,
         
      } // end enum

      // parameterless constructor
      public GradeBook()
      {
         InitializeComponent();
      } // end constructor

      // clear all TextBoxes
      public void ClearTextBoxes()
      {
         // iterate through every Control on form
         foreach ( Control guiControl in Controls )
         {
            // determine whether Control is TextBox
            if ( guiControl is TextBox )
            {
               // clear TextBox
               ( ( TextBox ) guiControl ).Clear();
            } // end if
         } // end for
      } // end method ClearTextBoxes

      // set text box values to string array values
      public void SetTextBoxValues( string[] values )
      {
         // determine whether string array has correct length
         if ( values.Length != TextBoxCount )
         {
            // throw exception if not correct length
            throw ( new ArgumentException( "There must be " +
               ( TextBoxCount + 1 ) + " strings in the array" ) );
         } // end if
         // set array values if array has correct length
         else
         {
            // set array values to text box values
            
          
            lastNameTextBox.Text = values[ ( int ) TextBoxIndices.LAST ];
            firstNameTextBox.Text = values[ ( int ) TextBoxIndices.FIRST ];
            iDTextBox.Text = values[ ( int ) TextBoxIndices.ID ];
            classTextBox.Text = values[ ( int ) TextBoxIndices.CLASS ];
            gradeTextBox.Text = values[ ( int ) TextBoxIndices.GRADE ];
         } // end else
      } // end method SetTextBoxValues

      // return text box values as string array
      public string[] GetTextBoxValues()
      {
         string[] values = new string[ TextBoxCount ];

         // copy text box fields to string array
     
         values[ ( int ) TextBoxIndices.LAST ] = lastNameTextBox.Text;
         values[ ( int ) TextBoxIndices.FIRST ] = firstNameTextBox.Text;
         values[ ( int ) TextBoxIndices.ID ] = iDTextBox.Text;
         values[ ( int ) TextBoxIndices.CLASS ] = classTextBox.Text;
         values[ ( int ) TextBoxIndices.GRADE ] = gradeTextBox.Text;

         return values;
      } // end method GetTextBoxValues
   } // end class  GradeBook.cs
} // end namespace GradeBook

Recommended Answers

All 6 Replies

You have no method in your class called InitializeComponent, which you call in your parameterless constructor.

You have no method in your class called InitializeComponent, which you call in your parameterless constructor.

I don't mean to be a bother but would you mind you showing me in code?

Code looks fine to me, including the constructror. The only thing that bothers me, are the names of the nameSpace and the class name - they are both the same. Maybe is this the issue. Not sure, never tried with both same names.
Mitja

In your method ClearTextBoxes (Line 32) you walk through the Controls of your form and test if they are a TextBox (Line 38) so there is no need to cast to a TextBox on line 41.
InitializeComponent(); is from a partial class component is Visual Studio a part you are missing?

Thanks I managed to get It Right. Thank God for this Forum you guys Rock!!

Hi I managed to get it right thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.