logo

Home page
Articles for Windows, Linux, OS X
Mac tips and articles
Mac tips
Windows 8 tips and articles
Windows 7 tips and articles
Vista Tips
XP Tips
Linux tips and articles
Read the blog
Online store
Windows, Linux, OS X programs
Links
About

Add a RealBasic calendar control

RealBasic is a great programming language and it has lots of useful functions, but it also has a few omissions too. You might not notice them if you are new to programming, but if you have ever used Visual Basic on a Windows PC, you might be frustrated when you come to write software using RealBasic or convert VB code.

A really useful feature in Visual Basic is the calendar control and this displays a calendar on a form that the user can access. They can browse through the months and years and select a date with no effort on the part of the programmer.

RealBasic doesn't have a calendar control and you will either have to write your own or try to find an add-on or plug-in that does it for you. It looks difficult to write, so you might discount this route and and add-on aren't always free, which means it might cost you money you would rather not spend.

Writing your own calendar control isn't as difficult as you might imagine. In fact, it's really easy and very little code is required. In fact, you can download and use the calendar control we will develop here for free.

Download the calendar RealBasic source code.

OK, you've got the calendar, how do you use it? Start RealBasic and load the project that you want to use it with. Slect File, Import and import the calendar into your project. When you want to display a calendar, just use:

frmCalendar.showmodal

if the user clicks a date, then the date selected is stored in frmCalendar.now, but if the user doesn't pick a date and just clicks Cancel then frmCalendar.now.year is set to 1900.

This is an arbitrary number and in my programs its a date no-one will ever want to pick, so it seemed a good idea to use it to indicate that the Cancel button was clicked. You will also find that the earliest date that the calendar will show is 1980, but again this is just because people don't need to pick earlier dates in my programs.

When you see how the calendar works you'll see how to change the date used to indicate Cancel and the range of dates that can be displayed. The form is also an Apple Mac sheet window that slides in from the top (Windows doesn't have them), but this can be changed too.

How the calendar control works
The form is very simple and there are 37 text boxes called StaticText1(0) to StaticText1(36). This is necessary because if a month starts on the last day of the week, a Saturday, and there are 31 days in the month, day 31 will end up in the 37th text box - StaticText(36).

There is a scroolbar called ScrollBar1 and this is actually used to store the date. Remember I said that the earliest date that could be displayed was 1980? ScrollBar1 stores the number of months since then. So when the user uses the scrollbar we can just look at ScrollBar1.Value and work out the date. If ScrollBar1.Value=27, that's 1980 + 2 years 3 months, or March 1982.

I've set the minimum ScrollBar1 value to 0 and the maximum to 600 because that's 50 years (50x12). Feel free to set whatever maximum you want and 6000 would be 500 years. No-ones going to need to pick dates 500 years into the future, but you may want to change the 1980 base date to something earlier so users can pick historical dates. We'll see how to do that in a minute.

The code on the Cancel button is very simple and it just sets the now property to 1900 and hides the form:

now.year=1900    'calendar cancelled
self.hide

When the user selects a date the calendar goes away and it's job is done. This is achieved by adding this code to the StaticText1() MouseDown event:

if StaticText1(index).Text>"" then  'a valid day?
  now.day=val(StaticText1(index).Text)   'set date
  self.hide          'hide window
end if

When the scrollbar is used to change the month or year we need to update the display. Therefore we put this code in the ValueChanged event for ScrollBar1:

ShowMonth    'change month/year

ShowMonth is a method that displays the month and it is called every time the user accesses the scrollbar. Here's the code:

dim i as integer

'clear day numbers
for i=0 to 36
  StaticText1(i).Caption=""
next

'set date from scrollbar value
now.day=1
now.Month=1+ScrollBar1.Value mod 12
now.Year=1980+ScrollBar1.Value\12

'show month/year
GroupBox1.Caption=months(now.month) + " " + str(now.year)

'display calendar
i=now.DayOfWeek-1
do
  StaticText1(i).Caption=str(now.Day)     'set day caption
  now.Day=now.Day+1      'next day
  i=i+1      'next text label
loop until now.day=1

'we've gone on a month, so step back
now.month=now.month-1

First all the text boxes are cleared, then we set the date by adding the number of months specified by ScrollBar1.value to the base year, 1980. You can change 1980 to any year you want and it is simply the earliest year that you want the user to be able to view. The GroupBox1 caption is set to show the month and year.

The calendar is displayed by working out which day of the week the month starts on - now.day is set to 1 and now.DayOfWeek is the day. The first day of the month is 1, but the first StaticText1() box is StaticText(0), so we subtract 1 to get the right text box to start in: i=now.DayOfWeek-1. We set the caption to the day number and increment the day until we end up back at the first of the month - actually the first of the next month, which is why we step back a month at the end: now.month=now.month-1

That's just about it. The only other code is in the form's Open event just to set up the months() array and set the calendar to show the current month:

dim i as integer

'set the month names
months=Array("","January","February","March","April","May","June",
            "July","August","September","October","November","December")

'get today's date
now=new date

'show the month
ScrollBar1.Value=12*(now.year-1980) + now.month-1

Feel free to use this calendar in your own programs.


copyright