Tutorial: Developing .NET COBOL

Overview

This tutorial introduces the Visual Studio IDE, and shows how to create a solution for a Windows application with a Windows form that says "Hello World".

Starting Enterprise Developer

To run this tutorial, you must have both Microsoft Visual Studio and Enterprise Developer installed.

To start Enterprise Developer:

  1. Depending on your Windows version, do one of the following:
    Windows 7 and Earlier
    • From your Windows desktop, click Start > Visual Studio 2017.
    Windows 8.1
    • From your Windows desktop, click the Visual Studio 2017 tile.
    Windows 10 and Later
    • From your Windows desktop, click Start > Visual Studio 2017.
  2. You might be prompted to set the environment, depending on the version installed. If you are prompted, choose General development environment. In this setup Visual Studio is customized for work with COBOL projects.

The IDE is shown in the figure below, although the information displayed in the large pane will be different.

The large pane is where your solutions and projects will be opened. At the moment, this shows the Start Page, with up-to-date information on Visual Studio. When you start work, this is the pane where your code is displayed.

The pane at the bottom is the Output window, where messages from the IDE and Compiler are displayed.

The right-hand pane is the Solution Explorer, which displays the structure of your solution and projects. At the bottom of the Solution Explorer pane are some tabs. Solution Explorer appears by default.

If any of these panes is hidden, you can show it from the View menu.

Creating a Solution and a Project

The first task is to create a solution and a project. A solution is a container holding one or more projects that work together to create an application. The solution has the extension .sln and is a readable text file. Microsoft recommends that you do not edit the file outside of Visual Studio.

A COBOL project has the extension .cblproj and again is human readable but Microsoft recommends that you do not edit it. Different types of project have different extensions, so for example a C# project has the extension .csproj.

In this section, you create a project and solution, as follows:

  1. Start Visual Studio.
  2. In Visual Studio, click File > New > Project.
  3. In the New Project dialog box, expand Installed > COBOL.
  4. Select Managed.
  5. In the center pane, select Windows Forms Application.

    The drop-down list above the center pane allows you to change the version of the target framework. Select the framework your clients are using to ensure your application includes classes and methods supported by it. You can leave the default .NET Framework for this demonstration.

  6. In the Name field at the bottom of the window, specify WinHello as the name of the project.

    Notice that the Name and Solution Name fields have the same name. Changing the name of the project automatically changes the name of the solution.

  7. In the Location field, browse to a directory where you want to put this tutorial.

    For example, if you create a folder on your c: drive called "Tutorials," change the location field to c:\tutorials. The solution will be stored in a subdirectory WinHello, according to the project name.

  8. Click OK.

This creates a solution, a project and an empty form, which is opened in the form designer. The Solution Explorer pane shows the WinHello project, which contains:

  • Properties, which contains the full project properties.
  • References, which contains the references to assemblies containing classes such as System.Windows.Forms that are used by the project. From the context menu for the References folder you can add references to other .NET assemblies, COM objects registered on your machine as well as other assemblies that your .NET code refers to.
  • Form1.cbl, which contains your code for the form. Under the expanded Form1.cbl is Form1.Designer.cbl, which is created by the form designer. Don't edit this code, because this defines the form, and if you change it you might not be able to open the Design window. Also, any changes you make might be overwritten by the form designer.
  • Main.cbl, which contains the trigger program that displays the main form.

If you need to view the contents of the project directory, you can open it from the IDE as follows:

  • In Solution Explorer, right-click the WinHello project and choose Open Folder in Windows Explorer.

Painting a Button and a Label

This section "paints" a button and a label on the page or form.

  1. Paint a button as follows:
    • Click or View > Toolbox.
    • If the Button object isn't listed, click Common Controls in the Toolbox.
    • Point to the Button object and drag it onto the page or form.
  2. Change the text on the button to "Say Hello", by editing the button's Text property, as follows:
    • Click on the button.
    • In the Appearance section of the Properties pane, scroll to the Text property.
      Note: Right-click the button in the designer, and then click Properties to display the Properties pane if it is not visible.
    • Edit the text to Say Hello and press Enter.

      Notice that the Name property of the button (in the Design section of the Properties pane) is, and remains, "button1."

  3. Paint a label somewhere on the form by dragging the Label object in the same manner as you did the Button object.
  4. Clear the default text in the label, by deleting the text in the Text property field and pressing Enter.

    The label essentially disappears upon this action, but remains on the form, as you will see when you build and run the application. Notice that the name property of the label control is, and remains, Label1 (or label1).

Adding a Click Event Handler

Now, you add an event handler to display "Hello World" when the button is clicked.