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 |
Description |
Text |
The actual text that is displayed to the user |
Visible |
Can you see the control. Accepts a Boolean value of 'true' or 'false' |
BackColor |
The background color for the control |
ForeColor |
The color the text displayed in the Text property |
Font |
The type and size of the font to use when displaying the Text property |
TextAlign |
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!