Program Code

Page update date :
Page creation date :

Let's take a look at the code of the program in action. I'm using C# as the programming language, but I'll explain it as if I know C# to some extent. If you're not sure, there are many books on C# programming that you can read and study. There are places where it is explained in quite a lot on other websites, so if you do not have money, it is good to look for such a place.

First, let's open the "Program .cs" file, which is also the starting point of the program. By the way, the extension of C# code files is ". cs". Open Solution Explorer to the right of the development screen (depending on the user). In some cases, the window is already pinned to the right. From there, double-click on "Program .cs".

ソリューション エクスプローラー

I think the Source Editor is open.

ソースエディタ

It's better to have each of you study C#, but for now I'd like to briefly explain the code in these tips.


using System;
using System.Collections.Generic;
using System.Windows.Forms;

Classes are always supposed to be written in a namespace somewhere. For example, the "File" class, which deals with files, is contained in the "System" namespace and in the "IO" namespace. Normally, to use the "File" class, you must write all namespaces and finally the class name as "System.IO.File".

System.IO.File f;

However, if you register it in advance using the "using" statement, you will be able to omit the namespace when actually using the "File" class.

using System.IO;
File f;

namespace Sample

It's called a namespace. All execution processors are written in it. The namespace has the same name as the project name you created. If you don't like it, you can change it later.


static class Program

The class name. The name to the right of "class" is the class name. Basically, it is easier to understand if the file name and the class name are together. The "static" to the left of "class" is usually not necessary, but for this "Program" class, you should think of it as a way of putting it on.


/// <summary>
/// アプリケーションのメイン エントリ ポイントです。
/// </summary>
[STAThread]
static void Main()
{
  Application.EnableVisualStyles();
  Application.SetCompatibleTextRenderingDefault(false);
  Application.Run(new Form1());
}

This is where the process actually begins. The top three lines are comments, so ignore them. It has nothing to do with the actual processing. "[STAThread]" may not actually be necessary, but if you do not understand it, it is safer to put it on as it is. The "static void main()" in the next line is the head of the method. Leave this as it is unless something bad happens. The program is executed from this method.

「Application.EnableVisualStyles();」 describes the controls you want to place on the form if you want them to be in XP style. Remove this line only if you want to make it look like an old control. Basically, leave it like this. 「Application.SetCompatibleTextRenderingDefault(false);」 is related to the text drawing of the control, but it can basically be left as is. 「Application.Run(new Form1());」 to actually run the application. Here, we will execute the form class "Form1" as a top-level window. "Form1" is the class of the window displayed in "Debug Execution" earlier.


Let's take a look at that "Form1" class. In Solution Explorer, right-click the "Form1.cs" file and display it in "Code View".

Form1 コードの表示

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Sample
{
  public partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();
    }
  }
}

That's basically all the code written in the "Form1" class. Only the parts that are different from the "Program" class are explained.


public partial class Form1 : Form

Consider that "public" is something you always put on when you create a class (it can change in some cases). Classes are basically "public", so I will keep them on. "partial" is used to split a class into multiple files. This is also required for classes that create forms. The "Form" at the end is a class that belongs to the "System.Windows.Forms" namespace. In the above code, "using System.Windows.Forms;" It is written as "Form" for short. If you add ":Form" after the class name "Form1", it means "Form1 class that inherited from the Form class". Remember, the class you're creating the form is basically what it looks like.


public Form1()
{
  InitializeComponent();
}

It is a method, but it is called a "constructor". The name of the method is always the same as the class name. The constructor is always executed immediately after creating the class. In the "Program" class above, "Application.Run(new Form1());" We are using the "new" operator to create a "Form1" class. Therefore, this constructor is always executed. Now, inside the constructor there is an "InitializeComponent()" method executed. The code for the "Form1" class is the only one I mentioned, but the class is actually divided into two files. In the Solution Explorer, there is a "+" button next to the "Form1.cs" file, so try pressing it.

Form1 の別のファイル

The "Form1.Designer .cs" file came out. Actually, the body of the "InitializeComponent" method is described in this file.

In the first place, what kind of processing is done by the "InitializeComponent" method is that the data set on the "designer" screen of the "Form1" class is replaced with the contents actually processed by the program. So, if you change the properties etc. on the "Designer" screen, the code in the "Form1.Designer .cs" file will be rewritten in real time. In other words, the class is divided into two files so that the programmer does not have to edit the code directly. Therefore, you don't need to look at the detailed code, so you can summarize the code of the file to be edited in this way very concisely. This is a unique benefit of C# 2.0, which unfortunately does not offer this feature. This is one of the reasons why I am going for C# 2.0. (By the way, C# 1.0 is standard in Visual Studio 2002 and 2003, and C# 2.0 is based on Visual Studio 2005.)

If you want to see the contents of "Form1.Designer .cs", you can double-click to see it. I think that it will be surprisingly helpful. In some cases, you may need to edit this, so it's worth taking a look.


Now that we've finished explaining the code immediately after the project was created. Let's change the settings a bit.

The form name (class) after creating the project is always "Form1". I think there are probably more than a few people who don't like this, so let's change the name. Since it will be the main window, let's try it as "MainForm".

First, from the Solution Explorer, right-click on "Form1.cs" and click "Rename".

名前の変更

Since it can be renamed, it will be rewritten as "MainForm .cs".

MainForm

A dialog like the one below is displayed, so press the "Yes" button. (It may not come out.) In that case, you need to manually change it with a refactor, etc.)

名前変更確認

Then, everything related to "Form1" will be renamed "MainForm". All class names will also change. It's that simple, isn't it?

全て名前変更