Reader Level:
ARTICLE

Working with buttons in Micro Focus Visual COBOL

Posted by Rick Malek Articles | COBOL.NET December 01, 2011
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.
  • 0
  • 0
  • 4011

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:

1.gif

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

Description

Name

How the control is referenced programmatically

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

TextAlign

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.

4.gif


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:

5.gif



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.

6.gif

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

7.gif

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:

8.gif

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:

9.gif

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.

10.gif

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

11.gif

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:

12.gif

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.

13.gif

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.

14.gif

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.

15.gif

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

16.gif

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!

COMMENT USING

Trending up