Resources For IT Professionals

# Small Basic Curriculum: Lesson 2.5: Clock, Desktop, and Dictionary Objects

Small BasicOnline > Lesson 2.5: Clock, Desktop, and Dictionary Objects

Estimated time to complete this lesson: 1 hour

### Clock, Desktop, and Dictionary Objects

In this lesson, you will learn how to:

• Use different properties of the Clock object.
• Use different properties and operations of the Desktop object.
• Use different operations of the Dictionary object.

### The Clock Object

As you program, you may encounter situations where you need to calculate time or perform certain actions based on the date and time.

By using the Clock object, you can include this logic in your programs and write programs that use the system clock. This object’s properties include Date, Hour, Time, and Year.

Let’s explore some properties of the Clock object…

### Date, Weekday, and Time

You can get the current system date, day of the week, and time by using the Date property, the WeekDay property, and the Time property, respectively.

Let’s look at an example that displays this information in the graphics window…

``````date = Clock.Date

weekday = Clock.WeekDay

time = Clock.Time

display = ("Current Date: " + date + "," + "Current Weekday: " +

weekday + "," + " Current Time: " + time)

GraphicsWindow.DrawBoundText(40, 40, GraphicsWindow.Width, display)``````

This is the output you will see:

In this example, we have used the DrawBoundText operation of the GraphicsWindow object. This operation displays a line of text at a location that you indicate by specifying an x-coordinate and a y-coordinate. The text for this example starts at the location that we indicated by specifying an x-coordinate of 40 and and a y-coordinate of 40.

### The Year Property

You can use the Year property of the Clock object to get the current system year.

Let’s take an example that uses this property to calculate the user’s age…

``````TextWindow.WriteLine("Enter the year of your birth:")

age = Clock.Year - birthyear

TextWindow.WriteLine("Your age is " + age + ".")``````

Clickon the toolbar.

### Month and Day

You can use the Month and the Day property of the Clock object to get the current month and current day, respectively.

``````TextWindow.Write("Enter the month of your birthday:")

TextWindow.Write("Enter the date of your birthday:")

If (birthdaymonth = Clock.month And birthdaydate =Clock.Day) Then

TextWindow.WriteLine("Hey! Wish You A Very Happy Birthday!")

EndIf``````

In this example, the text window displays birthday wishes only if user specifies the same month and day as the current system month and day.

### Hour, Minute, and Second

You can get the current hour, minute, and second of the day by using the Hour, Minute, and Second properties of the Clock object.

Let’s combine these properties to display a digital clock in the graphics window…

``````GraphicsWindow.FontSize = 26

GraphicsWindow.BrushColor = "Green"

up:

If Clock.Hour >= 12 Then

status = "PM"

Else

status = "AM"

EndIf

CurrentTime = Clock.Hour + " : " + Clock.Minute + " : " + Clock.Second + " " + status

GraphicsWindow.DrawBoundText(100, 100, 400, "Time: " + CurrentTime)

Program.Delay(1000)

GraphicsWindow.Clear()

Goto up``````

This is the output you will see:

In this example, we used the Hour, Minute, and Second properties of the Clock object to display a digital clock. We defined the clock status as PM if the current system hour is greater than or equal to 12; otherwise, the clock status is AM. To make the digital clock run continuously, we have used the Goto statement to jump to up: after 1000 milliseconds.

### The Desktop Object

You can set a desktop wallpaper of your choice by using the the SetWallPaper operation of the Desktop object.

You can also retrieve the size of your current desktop and use the information to specify the location of objects on the screen.

When you use the SetWallPaper operation, you can choose your wallpaper image from any local file, network file, or Internet URL.

``````ImagePath = Program.Directory + "\Blue.jpg"

Desktop.SetWallPaper(ImagePath)``````

You can get the screen height and width of the primary desktop by using the Height and Width properties, respectively, of the Desktop object.

Let’s apply these properties to the graphics window.

``````GraphicsWindow.Height = 400

GraphicsWindow.Width = 500

GraphicsWindow.Top = (Desktop.Height - GraphicsWindow.Height) / 2

GraphicsWindow.Left = (Desktop.Width - GraphicsWindow.Width) / 2``````

This is the output you will see:

The Height and Width properties of the Desktop object are very useful when you want to display a text or graphics window in the same position relative to the top and left side of the desktop at various resolutions. In this example, the graphics window is 400 pixels high and 500 pixels wide. To display this window in the center of the desktop, subtract the window’s height and width from the height and width of the primary desktop, and then divide the results by 2.

### The Dictionary Object

The Dictionary object is a useful resource that you can include as part of your Small Basic program.

You can use this object to retrieve the meaning of a specified word from the online Dictionary service.

The Dictionary object has two operations—GetDefinition and GetDefinitionInFrench

You must be connected to the Internet to use the Dictionary object in Small Basic.

You can obtain the meaning of an English word by using the GetDefinition operation. For a definition in French, use the GetDefinitionInFrench operation.

For example, let’s use the GetDefinition operation to find the meaning of “magnanimous.”

``````txt = "Magnanimous"

GraphicsWindow.FontName = "verdana"

GraphicsWindow.FontSize = 24

GraphicsWindow.BrushColor = "Black"

GraphicsWindow.DrawText(10, 10, txt)

defn = Dictionary.GetDefinition(txt)

GraphicsWindow.FontSize = 12

GraphicsWindow.BrushColor = "Black"

GraphicsWindow.DrawText(10, 60, defn)``````

This is the output you will see:

### Let’s Summarize…

Congratulations! Now you know how to:

• Use different properties of the Clock object.
• Use different properties and operations of the Desktop object.
• Use different operations of the Dictionary object.

### Show What You Know

Create a program to set an alarm that plays a sound of a ringing bell and changes the wallpaper of your desktop.

To see the answers to these questions, go to the Answer Key page.

Next Lesson