Windows Form using COBOL
This article we'll put some finishing touches to our form. We'll show how to make controls appear and disappear as well as placing the cursor in a pre-defined spot on the form.
This article will pick up where the last article, published November 5, 2009, left off. In the previous article we showed how to create a form, add controls and enable the controls, all with using COBOL. We also showed how to invoke .NET assemblies and the syntax for doing so.
This article we'll put some finishing touches to our form. We'll show how to make controls appear and disappear as well as placing the cursor in a pre-defined spot on the form. Knowing not only how to code in the Visual Studio environment is important, but also knowing where to go in the environment to take advantage of all that is has to offer is just as important.
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.
Our Presentation Form
We left off with our form looking as follows:
When you executed the project, did you notice the position of the cursor and the data when you entered information? It was left aligned wasn't it? By default a textbox, that's what we're using for our entry field, is left aligned. We obviously read left to right so a textbox would therefore be set to align the text for reading.
We however are using the textboxes for numeric input and need to adjust the field. To do this we simply do the following:
I have gotten into the habit of rebuilding the project often. While it may seem to be a bother and time-waster, it really does save time in the long run. If after you make a couple of changes you rebuild the project and notice it doesn't build, you have a smaller set of changes to look at to diagnose what the problem is and clean it up before it impacts anything else. Besides, it's quick and like I said, could save you a lot of time later.
Select the first textbox (should be named tbNum1)
Either right click and select 'Properties' or look to the right pane in Visual Studio for 'Properties'
Locate the property called 'TextAlign'. You'll notice it has a drop-down list. Click on the property to reveal the list
- Select the 'Right' value
- Repeat this for the remaining textboxes
- Before proceeding rebuild the solution.
OK, so our form now accepts numeric input that's right justified. It's still pretty boring though isn't it? What if someone wanted to execute our form a couple of times? They'd have to go back, clear the fields, and position the cursor before they could begin. Why not do that for them?
Add a Control
While I'm not an expert in UI design, I like to let the user know what's happening with a screen and direct them to take the appropriate action. By taking advantage of the properties built into the controls within Visual Studio we can make our form more user friendly and in the long run hopefully avoid support incidents.
To begin we need to add another command button to our form. Drag a command button from the palette to our form positioning it in about the following location:
Update the following properties for the new button:
Great, so now we have a new button on the screen. But if you understand what we just did, we made it invisible! We did that by setting the value of the 'Visible' property to 'False'. So what good is that you may ask? What we're doing is setting the initial state of the control. We don't want our users to see this control until we're ready to show it to them. Think through the process, when would we want to show the user this button? After they've clicked the 'Calculate' button right? So let's go add some code to the click event for the Calculate button to enable our new Reset button.
Hiding and Showing Controls
Double click on the 'Calculate' button to edit the source code. We need to do two things to our source code: a. We need to make the Calculate button disappear and b. we need to make the Reset button appear. We accomplish this with the following code:
So that our completed method looks like:
Next we need to add some code to the Reset button to make it accomplish four tasks:
a. Make the Reset button disappear
b. Make the Calculate button appear
c. Reset the field values
d. Position the cursor to the first text box field for entry
Rather than show you each line of code separately, the finished method is presented below. Each task (a through d) is labeled with the corresponding line of code.
One of the last issues we'll address in this article is the order in which the cursor moves about the form. This is called 'tab stop'. Depending on how you created your form depends on the order in which the controls or objects are referenced. I ended up with quite a mess and need to change the order. To change the tab order, all you need to do is update the value in the properties for each object on the form. I set the first text box, tbNum1, to be my first tab stop by setting it's 'tabindex' value to '1' as shown:
I then set the tabindex in tbNum2 to '2' and so on.
C++ and VB developers have been using Visual Studio for a quite a while now. They are very familiar with the material we covered in this article and the previous one. Now COBOL programmers can also use the power of Visual Studio to create or extend existing applications using COBOL. Work with the examples, try changing background or foreground colors, experiment with font sizing, colors, special effects. Add in a new control and enable it. Basically, play with this new environment. You'll find it offers a COBOL developer a new world in which to grow and learn a new way of being productive.