You are on page 1of 22

Creating Forms with COBOL

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:

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.

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.

We are going to update the following properties for the control with the values noted in the table below: Property Name Text Value lblNum1 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 Label Property Name Text Value lblNum2 Enter the second number:

Label Textbox Textbox Textbox Button

Name Text Name MaxLength Name MaxLength Name MaxLength Name Text

lblResult And the result is: tbNum1 4 tbnum2 4 tbResult 16 cmdProduct Calculate

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

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.

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:

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:

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

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:

The updated code:

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:

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 System.Convert ToInt16 Keyword to denote we are going to specify an alphanumeric literal pointing to a class The namespace and class we are going to be calling 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:

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!

Using Labels with Visual COBOL


Micro Focus released Visual COBOL R4 earlier this year and it has turned out to be an awesome addition to Visual Studio 2010. The development teams have done a great job in delivering a very usable product. To help you learn about Visual COBOL and COBOL.NET in a managed environment we'd like to provide some example solutions. Now there are great samples that come with Visual COBOL but they don't define or detail one specific control, they show a finished application and the user has to read through the code to figure out how the code was created. Our examples will focus on a single control. We'll show you how to manipulate the properties of that control programmatically. We may have to include one additional control but the focus on each project will be on a single control with a supporting role for the additional control. Our first control is the Label. While maybe not a glamorous control, the Label provides a very important function for the developer: It enables the developer to present content, instructions or images to the user. Without the Label presenting information to the user all we'd have is a form with a bunch of boxes or circles without any idea what they were for. So let's begin by looking at how to work with Labels. WinForm We've already created the WinForm we'll be using. Remember, the intent is to learn how to manipulate Labels, not create WinForms. Our WinForm was specifically designed to be simple and present only the basic information. It appears as:

Pretty simple, right? At the top we have a Label that says "This is a Label". We also have a button on the screen that says "Click Me". The button is in a supporting role for now and will be explored in another article. Our interest is in the Label. Label Properties Labels, like all controls, have properties associated with them. Although there are many properties that can be manipulated there really are only a few that have any real implication when working with Labels. When working with properties the general format used to update a property is: SET controlName::property TO {something} Where: controlName is the name of the control Property is the name of the property to be manipulated {something} is specific to each property and can be text, Boolean, enums, etc. We're going to concentrate our discussion on the following properties: Property Text Visible BackColor ForeColor Font TextAlign Description The actual text that is displayed to the user Can you see the control. Accepts a Boolean value of 'true' or 'false' The background color for the control The color the text displayed in the Text property The type and size of the font to use when displaying the Text property Where does the text show-up in the label

For information on the remaining properties for a Label or to review the properties above please see the Visual COBOL documentation or Microsoft Developer Network (MSDN). Text Property The Text property contains the information we want to show to the user. Updating the value of the property can be done by the following statement:

Our example we set the Text property to "This is how to change what a label shows". Visible Property The Visible property is used to either show or hide a control on a form. You can use hidden fields to store information that the user entered on a screen, as a counter or just about anything you don't want the user to see. In our example we use the Visible property to both show and hide different labels on the screen:

The first line of code shows a hidden label on the form. (lblHidden::Visible to true). The next line of code shows a hidden button on the form. (btnReset::Visible to true). The last line of code hides the 'Click Me' button. (btnClickMe::Visible to false). BackColor Property BackColor is used to define the color of the background in the label. In our example we set the color to Red with the following statement:

Visual COBOL R4 has made accessing native .NET classes very simple by inclusion of the keyword 'type'. "Type" enables you to directly access classes and properties without having to create Repository settings pointing to different classes. In the supplied project try this, create a new line immediately after the BackColor line defined above and rekey the line just as it appears. When you enter the word 'type' intellisense will see you've entered a keyword and a pop-up will appear with different classes from which to choose from. Enter in 'co' for color and the following is displayed.

Enter the two semi-colons and another popup will appear with the attributes you can select for the different colors.

Try experimenting with the code, changing the color. You'll see how easy VCr4 makes this. ForeColor Property The ForeColor property is used to define the color the text in the Label will be displayed with. The process is similar to that of the BackColor and thus we won't go into a deep explanation. The code to change the ForeColor property is:

In our example we change the ForeColor on the displayed text to Crimson. Font Property Being able to change the style or size of the displayed font during program execution can help identify errors, warnings or highlight text the user needs to concentrate on. To do that programmatically based on a condition is done using the Font property. In our example we'll change the displayed font to 'Verdana'. Before we do that though we need to save the current font information so we can restore it when we reset everything.

Saving the current font information requires an entry in the Working-Storage section at the Class level, not the method level. The code to do that is:

We create a field called ws-font that is of type 'System.Drawing.Font'. If you'd like to additional information on 'System.Drawing.Font' please review the Microsoft documentation. The next step is to copy the current font information for the label to the working storage field and that is accomplished with the following code:

The code is self-explanatory but basically it's copying the font information from the label (lblThisIs::Font) to the working storage field we created. The next step is to update the label's font to a new value. Our example changes the font to 'Verdana' and the size to '8' with the following code:

There are other options available that can be changed as well. We kept the example simple to show the basic method to changing a font. Experiment with the code and see what else can be changed. TextAlign Property Shifting text from left to right, centering or moving to the upper or lower portion of a field is accomplished with the TextAlign property.

The process is similar to the way the color was changed with BackColor and ForeColor. We use the key word 'type' to gain access the Class 'System.Drawing.ContentAlignment'. You can then select the alignment you'd like to see from the pop-up provided. Reset Button The Reset button is used to return the application to position it was in when we the application first started. Study the code and the technique. There is nothing different in the method that what has already been discussed above, without all the comments in-line in the code.

Wrap-Up The ZIP file has all the necessary source code for you to follow along and see how to update the properties we've described in the article. Read through the code; learn from it and use it in your projects. Even though we used a WinForm to present the information, the same concepts apply to WPF or WebForms. Yes there may be differences but this should at least serve as a starting point to help you figure out how to manipulate similar properties in the other presentation environments. Also, if you have a Label property you're not sure how to work with send it to us. Chances are if you're having questions someone else is also and we'd like to help you figure it out and expand this example! Happy Coding!

Working with buttons in Micro Focus Visual COBOL


Introductionl COBOL Micro Focus released VisualCOBOL R4 earlier this year and it has turned out to be an awesome addition to Visual Studio 2010. The development teams have done a great job in delivering a very usable product. To help you learn about VisualCOBOL and COBOL.NET in a managed environment we'd like to provide some example solutions. Now there are great samples that come with VisualCOBOL but they don't define or detail one specific control, they show a finished application and the user has to read through the code to figure out how the code was created. Our examples will focus on a single control. We'll show you how to manipulate the properties of that control programmatically. We may have to include one additional control but the focus on each project will be on a single control with a supporting role for the additional control. This article is about the Button control. In a previous article we introduced the Label control and also used the Button control in a 'supporting' role to the Label. The Button is what I like to refer to as an 'initiator', that is when the button is clicked or pushed it starts something. Buttons are made to tell the application that "I'm done entering something please go process it". The Button therefore not only has properties that we can change (color, text, etc.) programmatically but methods that we can call when an event occurs. There are numerous events associated with a Button but we'll concentrate on the main events. An event when it occurs invokes or calls a method to handle the situation that has just occurred. For those unfamiliar with the term 'method', a method is a piece of code that is executed when called called, kind of like a small program. We'll go into methods more in a little bit. We're going to re-use our previous Label example for this article. The Label example was a nice, small compact example showing what happens when a button was clicked. It showed how to show and hide a button as well as how to change colors. Let's expand what we did last time a bit shall we? WinForm

We've already created the WinForm we'll be using. Remember, the intent is to learn how to manipulate Buttons, not create WinForms. Our WinForm was specifically designed to be simple and present only the basic information. It appears as:

Pretty simple, right? At the top we have a Label that says "This is a Label". We also have a button on the screen that says "Click Me". There are actually more controls but they are 'hidden' from the user right now. We'll go into more detail about hiding controls in the article. In our previous article the button was in a supporting role and not really explained in detail. In this article it's the star of the show so let's talk about it now. Button Properties Buttons, like all controls, have properties associated with them. Although there are many properties that can be manipulated there really are only a few that have any real implication when working with Buttons. When working with properties the general format used to update a property is: SET controlName::property TO {something} Where: controlName is the name of the control Property is the name of the property to be manipulated

{something} is specific to each property and can be text, Boolean, enums, etc. We're going to concentrate our discussion on the following properties: Property Name Text Visible BackColor ForeColor TextAlign Description How the control is referenced programmatically The actual text that is displayed to the user Can you see the control. Accepts a Boolean value of 'true' or 'false' The background color for the control The color the text displayed in the Text property Where does the text show-up in the labe

For information on the remaining properties for a Button or to review the properties above please see the Visual COBOL documentation or Microsoft Developer Network (MSDN). Name Property The Name property is used to provide a unique name to the control so it can be accessed programmatically. For Buttons I like to use the abbreviation 'btn' in the first three positions and then follow that with something specific to what the button is for. In our example the button says 'Click Me' so I've named the Button "btnClickMe". I like to use camel-case (capital letter for the first letter of each word) for readability purposes. I would highly recommend establishing a naming convention to be used in your shop.

Text Property The Text property is what the user sees on the face of the Button. In most cases a Buttons 'face value' will not change and is set in the Properties panel. Our Text property contains the value "Click Me" as shown in the Property Panel:

Visible Property The Visible property is used to either show or hide a control on a form. You can use hidden fields to store information that the user entered on a screen, as a counter or just about anything you don't want the user to see. In our example we use the Visible property to both show and hide different controls on the screen.There are actually two buttons on the Form with only one being visible. We use the 'Visible' property with a value of 'True' to show a control or a value of 'False' to hide a control. The ClickMe button has it's Visible property set to true via the Property Panel.

For the Reset Button the Visible property is set to 'False' in the Property Panel:

The Visible property can be manipulated through code to either show or hide an existing control. When the ClickMe button is clicked we want to hide it and then show the Reset button. The code to perform this little trick is:

The first line of code shows a hidden label on the form. (lblHidden::Visible to true). The next line of code shows a hidden button on the form. (btnReset::Visible to true). The last line of code hides the 'Click Me' button. (btnClickMe::Visible to false). BackColor Property BackColor is used to define the color of the background in the Reset Button. In our example we set the color to Red with the following statement:

Visual COBOL R4 has made accessing native .NET classes very simple by inclusion of the keyword 'type'. "Type" enables you to directly access classes and properties without having to create Repository settings pointing to different classes. In the supplied project try this, create a new line immediately after the BackColor line defined above and rekey the line just as it appears. When you enter the word 'type' intelli-sense will see you've entered a keyword and a pop-up will appear with different classes from which to choose from. Enter in 'co' for color and the following is displayed.

Enter the two semi-colons and another popup will appear with the attributes you can select for the different colors.

Try experimenting with the code, changing the color. You'll see how easy VCr4 makes this. ForeColor Property The ForeColor property is used to define the color the text in the Label will be displayed with. The process is similar to that of the BackColor and thus we won't go into a deep explanation. The code to change the ForeColor property is:

In our example we change the ForeColor on the displayed text to Crimson. TextAlign Property Shifting text from left to right, centering or moving to the upper or lower portion of a field is accomplished with the TextAlign property.

The process is similar to the way the color was changed with BackColor and ForeColor. We use the key word 'type' to gain access the Class 'System.Drawing.ContentAlignment'. You can then select the alignment you'd like to see from the pop-up provided. Reset Button The Reset button is used to return the application to position it was in when we the application first started. Study the code and the technique. There is nothing different

in the method that what has already been discussed above, without all the comments in-line in the code.

Events If you're new to object-oriented programming you may not be familiar with the term 'event'. So before we talk about the events associated with the button object I thought I'd try and define just what an event is and why it's important in Visual COBOL and distributed programming in general. From MSDN we have the following definition of an event: "An event is a message sent by an object to signal the occurrence of an action. The action could be caused by user interaction, such as a mouse click, or it could be triggered by some other program logic. The object that raises the event is called the event sender. The object that captures the event and responds to it is called the event receiver." Simple, right? Let me add some more explanation to this definition. Let's look at the first sentence: An event is a message sent by an object to signal the occurrence of an action. This is essentially the core explanation, something happens and a notice is sent out to do something. In our case the button is clicked (the something happens); a notice is sent out (a 'button click' event is raised); and something is done (the method btnClickMe_Click is executed). The rest of the definition is for information purposes, this first line is really what an event is: a notification that something has happened and someone has to address what has happened. To access the events for the btnClickMe, in the properties Window select the button with the little lightning bolt on it. If you hover over it a tool-tip will appear informing you this is the button for events.

Click the lightning bolt and the following screen will be displayed (once all the sections are minimized):

If you'll notice I've left one section expanded, the Action area. The action 'Click' has an entry next to it (btnClickMe_Click). This is the method that will be executed when the button is clicked. Each of the methods we've been working with in this article (btnClickMe_Click and btnReset_Click) were created to handle the Click event. Whether you were aware of it or not you were already working with event handlers when you were updating the methods for each of these two buttons. Told you it was simple! If you expand each of the sections above there are numerous events that can be accounted for. A couple of popular events deal with the Mouse (MouseEnter, MouseHover and MouseLeave). For further information about events and event handlers refer to the Micro Focus Visual COBOL documentation or MSDN.

Wrap-Up The ZIP file has all the necessary source code for you to follow along and see how to update the properties we've described in the article. Read through the code; learn from it and use it in your projects. Even though we used a WinForm to present the information, the same concepts apply to WPF or WebForms. Yes there may be differences but this should at least serve as a starting point to help you figure out how to manipulate similar properties in the other presentation environments. Also, if you have a Button property you're not sure how to work with send it to us. Chances are if you're having questions someone else is also and we'd like to help you figure it out and expand our examples! Happy Coding!

You might also like