Creating Forms with COBOL

Article Description 

Numerous articles have described how Micro Focus COBOL interacts seamlessly with the Microsoft .NET environment. But how exactly if you were programming only in managed COBOL would you create a form or call a specific .NET assembly?

In this article we're going to create a COBOL based WinForm that will accept two numbers multiply them together and present the results back to the user. Sounds pretty simple, right? In reality there are a few 'gotchas' we're going to identify and then show you how to handle them. We're also going to show you a couple of neat tricks, so let's start!

As with all the articles presented, please keep in mind that the example is not of a completely finished and polished application. The intent is to demonstrate how to accomplish specific coding techniques in the environment.

Basic WinForm

We will not go through all the steps in detail of how to create a COBOL WinForm. In some instances we will merely provide the navigation path, such as 'File àNew à Project' to begin our project. When you follow this path you will be presented with the following screen:

NewProject.JPG

I've selected the 'Windows Forms Applications' and the bottom of the page has been completed for me automatically. We're going to change the name of project from 'WindowsFormsApplication1' to 'CBLForm'. The location is my default location for projects so feel free to update this value if you'd like. Click OK and a new project will be created for you as shown.

BlankForm1.JPG

To this form we are going to add the following controls:

1.    3 labels: They'll tell the user what to enter

2.    3 text boxes: The area for the user to enter in their data. Two will accept input and one will not

3.    Command Button: How we'll call the method to do the calculation.

Before we start adding the controls I like to update the properties for the form. The title of the form as you can see is 'form1', which isn't very descriptive or nice to look at. Let's change this to 'COBOL WinForm Example'. Not very interesting either but a bit more descriptive. To do this select the form and either right click and select properties or look in the lower right hand corner of your screen at the properties window. The property we are going to be updating is 'Text'. Locate it and change the value to something more suitable. Next click back on the form and notice the title is now what you entered. Now we can start adding the controls listed above.

Adding Controls

To add a control to a form a technique known as 'drag and drop' is used. Basically you select the control from the palette on the left by clicking on with your left mouse button, holding down the left mouse button and dragging the control to where you would like to position it on the form. Do this with the 'Label' control and place it on your screen as shown below.

label1.JPG

We are going to update the following properties for the control with the values noted in the table below:

Property

Value

Name

lblNum1

Text

Enter the first number:

Remember, right click and select properties from the menu or go to the lower right portion of the Visual Studio screen. Update the properties noted. You can also experiment with the 'font' property. By default the size is quite small. Experiment with the size and font selection to make the label a little larger. After you have this first label control created add the following controls and update their properties based on the following table

Control

Property

Value

Label

Name

lblNum2

 

Text

Enter the second number:

Label

Name

lblResult

 

Text

And the result is:

Textbox

Name

tbNum1

 

MaxLength

4

Textbox

Name

tbnum2

 

MaxLength

4

Textbox

Name

tbResult

 

MaxLength

16

Button

Name

cmdProduct

 

Text

Calculate

After you've added the above controls your form should look similar to the following:

PaintedForm.JPG

Now we need to do some coding to enable the command button.

Enabling the controls

If you double click on the command button you will be placed into a Visual Studio edit panel as shown.

NewMethod.JPG

The first thing we need to do is add some variables to store the information in from the screen and to calculate the result of the operation. More on that in a minute.

For the data area we're going to use standard COBOL PIC clauses to define our variables. We will need to define three variables, Num1, Num2 and Result. Remember though, in our definitions for the textboxes we defined Num1 and Num2 as having a maximum length of 4 and Result as having a maximum length of 16. So our completed Working-Storage Section should look like:

ws-section.JPG

Now that we have our variables declared we need to capture the information that is entered on the screen. The following code does this for us:

Click01.JPG

However, if you'll notice at the bottom of your screen intelli-sense has identified a problem with our code:

ErrorList.JPG

The listing is telling us we have a type mis-match error. The data coming in from the screen is defined as 'string' (non-numeric) and we are trying to move it into a numeric field. One possible solution would be to add another variable of type character that is then redefined into the numeric type we need. I prefer to add two separate variables, without the redefine, to the working-storage section. After adding these two variables my working storage section looks like:

ws-section02.JPG

The updated code:

Method02.JPG

But there is still a problem. If you'll remember our original task was to take two numbers and multiply them together. While we have succeeded in getting the information from the screen into our variables, they are still in the wrong format. We need to convert the data to numeric variables. For that .NET comes to the rescue!

.NET to the Rescue

.NET has a whole host of utilities that can be used for specific issues such as this. For our purpose we will want to use the 'Convert' class. Convert has many different methods available for converting from one data type to another. But how do we invoke the class? Let me show you the code and then let me explain. The code to convert from character to numeric using the convert class is as follows:

txt2int.JPG

The above code shows two ways to convert a text value to a numeric value. The first line is called an 'inline invocation'. The variable Num1 is being set to the outcome of the call to the 'ToInt16' method. The method is in turn getting it's input from the textbox tbNum1 and it's text property (tbNum1::"Text"). This is in fact a shorter method of doing what the next two lines of code accomplish, get the values from the screen and then convert the text values to integer values.

.NET makes our task simple by providing the methods necessary to convert data from one type to another. The invocation can be a bit tricky though until you understand the syntax being used. In our example we have the following:

          type "System.Convert"::"ToInt16"

type

Keyword to denote we are going to specify an alphanumeric literal pointing to a class

System.Convert

The namespace and class we are going to be calling

ToInt16

The method we are going to be invoking from the above namespace and class

This is but one method of invoking .NET assemblies. Another is to use the ILUSING directive. For details on this please refer to the Micro Focus .NET Help file.

Our finished method with the calculations looks like:

calconly.JPG


Wrap-Up

We're going to stop at this point. In this article we've shown how to create a form, add controls and enable the controls, all with using COBOL. We've also shown how to invoke .NET assemblies and the syntax for doing so. In our next article we'll expand the solution to make controls appear and disappear as well as placing the cursor in a pre-defined spot on the form. Again, the zip file has all the code necessary. Experiment and have some fun with it.

Happy Coding!

 

Up Next
    Ebook Download
    View all
    Learn
    View all