11
FSB23103 1 UNIVERSITI KUALA LUMPUR Malaysia France Institute Lecture 18 Date and time processing Multiple Forms Mdm Ratnawati Ibrahim

Lecture (17)Date and Time Processing Multiple Forms

Embed Size (px)

Citation preview

Page 1: Lecture (17)Date and Time Processing Multiple Forms

FSB23103 1

UN

IVE

RSIT

I K

UA

LA L

UM

PU

RM

ala

ysia

Fra

nce I

nsti

tute

Lecture 18Date and time processing

Multiple Forms

Mdm Ratnawati Ibrahim

Page 2: Lecture (17)Date and Time Processing Multiple Forms

FSB23103 2

UN

IVE

RSIT

I K

UA

LA L

UM

PU

RM

ala

ysia

Fra

nce I

nsti

tute

Date and Time Processing

VB provides several date- and time-related functions in addition to the methods of the Date class. The function

• TimeOfDay( ) provides the current system date and time• Today( ) produces the current system date• Day applied to a date returns the day of the month as an

Integer in the range 1 to 31• WeekDay produces the number of the day of the week• Month provides the number of the month in the year• WeekdayName and MonthName identify the

corresponding day and month respectively• Year applied to a date yields the year as an Integer

Page 3: Lecture (17)Date and Time Processing Multiple Forms

FSB23103 3

UN

IVE

RSIT

I K

UA

LA L

UM

PU

RM

ala

ysia

Fra

nce I

nsti

tute

Date/ Time Processing –cont’d • DateDiff returns the length of the interval in the units

indicated from the start date to the finish date. It takes three arguments – the first is a method of DateInterval that indicates the part of the date/time to consider:

Year indicates the year Month – the month as a number from 1 to 12 DayOfYear – the day of the year, a number between 1

and 365(6) Day – the number of the day of the month Weekday – the number of the weekday WeekOfYear – the number of the week of the year Hour, Minute, Second – the hours, minute and second

respectively of the time followed by a start date expression and a finish date

expression.

Page 4: Lecture (17)Date and Time Processing Multiple Forms

FSB23103 4

UN

IVE

RSIT

I K

UA

LA L

UM

PU

RM

ala

ysia

Fra

nce I

nsti

tute

Date and Time Formatting

VB provides several predefined date and time formats using the Format function. Suppose Now yields 26/8/2004 5:14:12 PM then:

Alternatively the methods ToString, ToLongDateString, ToShortDateString, ToLongTimeString, ToShortTimeString of Date may be used to the same effect.

Function command Output

Format(Now, “General Date”) 26/8/2004 5:14:12 PM

Format(Now, “Long Date”) 26 August 2004

Format(Now, “Short Date”) 26/8/2004

Format(Now, “Long Time”) 17:14:12

Format(Now, “Short Time”) 17:14

Run

Page 5: Lecture (17)Date and Time Processing Multiple Forms

FSB23103 5

UN

IVE

RSIT

I K

UA

LA L

UM

PU

RM

ala

ysia

Fra

nce I

nsti

tute

Multiple forms

Additional windows are produced by adding forms. This can be done in two ways:

• With the second and subsequent forms opening outside the main form. These are then dependent on the main form. Consequently if the main form is closed, the application ends.

• With additional forms opening inside the main form, as document windows. These are MDI (Multiple Document Interface) forms. (Not to be covered here)

To add a form:1. From File menu, select Add New Item …., or

From Project menu, select Add Windows Form…., or, etc.

2. In Add New Item, ensure Windows form is selected.3. Provide an appropriate name.4. Click open.

Page 6: Lecture (17)Date and Time Processing Multiple Forms

FSB23103 6

UN

IVE

RSIT

I K

UA

LA L

UM

PU

RM

ala

ysia

Fra

nce I

nsti

tute

Multiple form code

• Secondary forms are brought into the main by declaring them as variables. Accordingly a new instance of the form is created.E.g. Dim anotherForm As New Form2where Form2 is the forename given when added to the project.A instance of the form is created each time the routine is activated. Consequently,depending on the routine, several instances may be inadvertently be created. To restrict the number of instances to one use Static in the declaration.E.g. Static Dim anotherForm As New Form2

• The variable name can be then used to access the forms properties.E.g. anotherForm.Text = “The new form”

Page 7: Lecture (17)Date and Time Processing Multiple Forms

FSB23103 7

UN

IVE

RSIT

I K

UA

LA L

UM

PU

RM

ala

ysia

Fra

nce I

nsti

tute

Multiple form code – cont’d

• The new form will not be visible until the Show() or ShowDialog() methods are invoked.E.g anotherForm.Show()This may be located within a button_Click method.While the window is active, the user can switch between the forms by clicking on them.

• A secondary form is closed using the methods Hide() or Close().

• If there are several secondary forms and data is to be passed between them or if methods are to be accessible from the different form, then such variables and code should be stored in a module.

Page 8: Lecture (17)Date and Time Processing Multiple Forms

FSB23103 8

UN

IVE

RSIT

I K

UA

LA L

UM

PU

RM

ala

ysia

Fra

nce I

nsti

tute

Example: Multiple Forms

Visual programming:

Run

Page 9: Lecture (17)Date and Time Processing Multiple Forms

FSB23103 9

UN

IVE

RSIT

I K

UA

LA L

UM

PU

RM

ala

ysia

Fra

nce I

nsti

tute

MessageBox

• The MessageBox dialog displays a String that may incorporate the result of a calculation.E.g.

MessageBox.Show(“Sum is: “ & CStr(sum))This may be shortened to

MsgBox(“Sum is: “ & CStr(sum))

The pop-up MessageBox displays a custom dialog that provides the user with information that must be acknowledged before the program moves on.

Page 10: Lecture (17)Date and Time Processing Multiple Forms

FSB23103 10

UN

IVE

RSIT

I K

UA

LA L

UM

PU

RM

ala

ysia

Fra

nce I

nsti

tute

Function InputBox

• This is similar to MessageBox , in that it does not occupy screen space permanently – it pops up when required.

• InputBox takes a single String argument which appears in the dialog and may used to prompt.E.g.

Typically the value entered is assigned to a variable, in the process performing any conversion appropriate.

num1 = CInt(InputBox("Enter first integer"))num2 = CInt(InputBox("Enter second integer"))

Page 11: Lecture (17)Date and Time Processing Multiple Forms

FSB23103 11

UN

IVE

RSIT

I K

UA

LA L

UM

PU

RM

ala

ysia

Fra

nce I

nsti

tute

Example: Input validation

Run