Adding Cultural Information To The DateTimePicker In Micro Focus Visual COBOL For .NET

In a previous article we described how to utilize the .NET object ‘Date Time Picker’ in Visual COBOL. After reviewing the article Alwyn Royall (Mr), one of our UK Solutions Consultants provided some additional information about applying Cultural information to displaying dates. For those of you who work in a single location the thought of applying cultural information may not have crossed your mind. There are however applications that are used in a many different locations worldwide and the ability to present information, specifically date information, in a format they are used to seeing is huge benefit not only to the user but to the company receiving and processing the information. They are more assured the data will be correct and the end user finds the application easier to use.

WinForm

Alwyn has expanded the previously created WinForm to include a couple of new combo boxes. The new form now looks like the following:

form

If you will notice the bottom of the form there are three new items added:

Item

Description

Date Pattern

How to display the date time information

Country Format

What country specific format will be applied

Result

The output from applying the culture information

The process of executing the form has changed a bit with these additional fields. You still need to select the start and end dates at the top of the form but prior to clicking the “Calculate” button, but you now can select the pattern and country formats to apply to the date being presented in the result textbox.

Before we execute the form let’s take a look at the updates to the solution.

Variable declarations

In order to update the display of the date information to the selected culture some additional variables and data types are required. In the source code for the form we’ve separated these new variables so they are easier to locate and identify the specific updates applied.

variable

Variable

Description

dt1

Hold variable for our date

dateFormat

Selected format from the combobox

dateCulture

Selected Culture to use from the combobox

dtFmt

The way the date will be displayed

dtCult

The specific culture to apply to the format

newCulture

Instantiated CultureInfo class

origCulture

Holder for the original culture setting

In order to fully understand the need for these variables let’s take a look at the new combo boxes that have been added to the form:

form

Date Pattern

The Date Pattern combo box will determine the format for the date to be displayed. The combo box has been predefined with the available formatting options for a date. If we look at the properties for the Date Pattern combo box we see the following:

pattern

Under the ‘Data’ area the ‘Items’ property has been updated to contain a collection of available selections for the combo box. Clicking on the “(Collection)” item yields the following display:

collection

The values in the Collection editor represent the available formats available for a datetime data variable. This information was keyed in manually to create the list and if you’ll notice a specific format was used throughout:

Indicator space Description

The indicator corresponds to the formatting variables used by the DateTime data type and further information about the formats and data type can be found on MSDN.

A space was used as a separator in the list.

Description represents the way the date or time will be displayed.

When a selection is made from the combo box the value selected will be placed in the ‘dateFormat’ variable discussed earlier.

Country Format

Similar to the Date Pattern combo box, the Country Format combo box has a collection of available formats that can be applied. The properties for this combo box are:

format

Selecting the “(Collection)” ellipsis yields a similar string collection editor with data representative of the cultural information:

collection

The format is identical to the Date Format data, an indicator followed by a space followed by a description of what the indicator is implying. In this instance though the indicator is not a single value but a combination of values separated by a dash.

The CultureInfo class specifies a unique name for each culture. The name is a combination of a two-letter lowercase culture code associated with a language and a two-letter uppercase subculture code associated with a country or region. For further information on the CultureInfo class see the MSDN documentation. When a selection is made the value from the above table will be placed into the variable ‘dateCulture’ discussed earlier.

Now that we have shown the fields that will enable the selected date format and culture to be applied let’s talk about how to enable all this in our code.

Tying it all together

As with our data variables we’ve kept the update segment of code that applies the date and culture format information separate from our original code. The first thing we need is a date to manipulate! For our example we will use our StartDate that was selected at the top of the form. We can create a copy of it and store it in the ‘dt1’ variable with the following line of code:

code

Our next step is to determine what type of format to apply to the date. This information is contained in the date format combo box value that was selected prior to pressing ‘Calculate’. We have used a construct some of you may not be too familiar with, a ‘try-catch’ block. A ‘try-catch’ block is a coding construct distributed developers are very familiar with it, it basically says “try this bit of code, if it doesn’t work catch any errors here”.

error

Our example is ‘trying’ to set the dateFormat variable to the value from combo box that was selected prior to pressing the ‘Calculate’ button. The next line in the ‘try’ block says to move the first character found in position one to the variable dtFmt. This is the option that will be used when we format the date or time for display.

But what happens if you didn’t make a selection prior to pressing the ‘Calculate’ button? The ‘catch’ portion of the block says to move the letter ‘d’ to the dtFmt variable. This is in effect preventing an error from occurring by saying “if you didn’t make a selection use this as the default value”, it will also catch any invalid selections for your installation.

We next need to retrieve the specific culture information. This is done in a similar fashion with the following code:

code

Using another ‘try-catch’ block we first read the value that was selected into the variable ‘dateCulture’. The indicator starting in position one for a length of 5 is next moved into the variable ‘dtCult’. On the next line of code, the variable ‘dtCult’ is used as a parameter to update the cultural format to be used for our run-unit. The final line of code in the ‘try’ portion of the block is applying the new cultural information to the current thread or process, thus changing the way the date is formatted on the screen.

The final line of code ties it all together. We have already changed the cultural view of the date we now have to apply the updated formatting.

code

The textbox named “Result” (tbxResult) is updated to reflect the new format by applying the date format to the start date, dt1 in our sample. For our sample I have selected the long date format to be displayed in the Russian language. Prior to pressing the ‘Calculate’ button my screen appears as

calculate

And after pressing ‘Calculate’ the updated screen appears as:

screen

Wrap-Up

Now you have seen the culture properties in use you might like to expand upon its use into currency and language presentations. Culture demonstrates one of the many benefits of utilizing the .NET platform in your application.

A big “Thank You” to Alwyn for his input and suggestion to applying cultural information to be used with the DateTimePicker control. The ability to work in multiple cultures, locations and languages greatly enhances the usefulness and longevity of your COBOL applications.

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 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!

If you’d like to try out the samples and don’t have a copy of Micro Focus Visual COBOL, you can download the Personal Edition of Visual COBOL for free from this link.

Up Next
    Ebook Download
    View all
    Learn
    View all