You are on page 1of 8

376

จากรูป เลือก popup menus: general_pop ที่เราไดสรางไวแลวมาผูกกับ canvas: employee_can เมื่อผูกแลวสามารถใชงาน popup
menus บน canvas: employee_can ไดโดยคลิกเมาสดานขวาเพื่อเรียกใชงาน

การเพิ่มปฎิทิน (Calendar) ใน Form


การทํางานกับ Form ที่มี field เพื่อใหบันทึกวันที่ โดยปกติจะแสดงวันที่ปจจุบัน (system date) หรือใหผูใชพิมพวันที่เขาไปโดยตรง
หรือแกไขวันที่ที่แสดงอยูกอนแลว ถาตองการแกไขวันที่และทําใหการทํางานของผูใชสะดวกขึ้นเราสามารถสราง calendar เพื่อใหผูใช
เลือกวันที่จาก calendar แทน

รูปที่ 20-54 Calendar ที่แสดงใน Developer Suite 10g

จากรูป ผูใชเลือกวันที่ที่ตองการไดโดยใชเมาสคลิกเลือก หรือใชปุมบน calendar ที่มีเครื่องหมาย <<, <, >, >> เลื่อนไปยังเดือน
หรือปกอนหนานี้ หรือถัดไป เมื่อไดวันที่ที่ตองการแลวใหคลิกปุม Ok ถาตองการออกจาก calendar ใหคลิกปุม Cancel

ความหมายของปุมบน calendar
<< : เลื่อนไปปกอนหนานี้ 1 ป
< : เลื่อนไปเดือนกอนหนานี้ 1 เดือน
> : เลื่อนไปเดือนถัดไป 1 เดือน
>> : เลื่อนไปปถัดไป 1 ป

สวนประกอบของ Calendar
เมื่อเพิ่ม calendar ใน Form เราตองเพิ่มสวนประกอบที่สําคัญ 2 สวนใน Form กอนคือ calendar object group และ calendar.pll
- Calendar object group
Object นี้ประกอบดวย data blocks, buttons และ canvases ที่จําเปนตองใชเพื่อแสดง calendar เราถือวา calendar object group
เปนเพียงสวนหนึ่งที่ถูกเลือกใชงาน มันถูกเก็บใน object library ชื่อ stndrd20.olb
- Calendar90.pll เปน PL/SQL Library ที่กําหนดใหมีกลไกการทํางานของ calendar

เมื่อ calendar object group ถูกยายไปไวใน Form จะมี form objects จํานวนมากถูกเพิ่มเขาไปใน Form ถาเราตองการเปลี่ยน
default format ของ calendar ใหแกไขที่ objects ตามรายการดานลางดังนี้
- Control block ชือ
่ date_control_block เปนที่เก็บ buttons
- Control block ชือ่ date_button_block มี buttons สําหรับแตละวันในเดือน
- Content canvas ชื่อ date_lov_canvas ใชแสดง calendar
- Visual attribute ชื่อ date_normal_va เปน format ของ date ที่ไมถูกเลือก
- Visual attribute ชื่อ date_selected_va เปน format ของ date ที่ถูกเลือก
- Visual attribute ชื่อ date_weekend_va เปน format ของ weekend date คือวันเสาร และอาทิตย

Calendar90.pll มี package ชื่อ date_lov ใชแสดง calendar และทํางานดานอื่นๆ และมี procedure ที่ใชงานรวมกันคือ get_date
เราสามารถนํา procedure นี้ไปไวใน when-button-pressed trigger หรือ key-listval trigger เพื่อใหแสดง calendar

รูปแบบของ procedure: get_date ที่ใชแสดง calendar

Date_lov.get_date(calendar date,
form item,
X position,
Y position,
window title,
ok button text,
cancel button text,
weekend,
autoconfirm,
autoskip);

บทที่ 20 List of Values, Radio Button, Item List และ Popup Menus
377

Keywords และ Parameters


Calendar date : วันที่ที่ถูกแสดง และใหเลือกเมื่อ calendar แสดงขึ้นมาใหเห็น
Form item : item บน Form ที่ใชรับคา date ที่ถูกเลือก
X position : ตําแหนงตามแนวนอน นับจากมุมบนซายของ calendar
Y position : ตําแหนงตามแนวตั้ง นับจากมุมบนซายของ calendar
Window title : ขอความ (title) ที่แสดงบน calendar
Ok button text : ขอความที่แสดงบนปุม Ok
Cancel button text: ขอความที่แสดงบนปุม Cancel
Weekend : ใหเลือกวาตองการใหสีของวันสุดสัปดาห (weekend dates) แตกตางจากสีของวันอื่นๆ คาที่ใหเลือกคือ true
และ false
Autoconfirm : ตองการให date ที่เลือกจาก calendar ไปปรากฎใน Form item ทุกครั้งที่มีการเลือก date หรือไม คาที่ใหเลือก
คือ true และ false
Autoskip : เมื่อเลือก date จาก calendar แลว ตองการใหเคอเซอรเลื่อนไปยัง field ถัดไปทันทีที่ปด calendar หรือไม คาที่
ใหเลือกคือ true และ false

สําหรับ Autoconfirm ถากําหนดเปน True คา year ที่ return จะเปนป 0007 แทนที่จะเปน 2007 ถาเปน False คาที่ return จะถูกตอง
คือเปนป 2007 ซึ่งผูเขียนยังไมทราบสาเหตุวาเกิดจากอะไร

เมื่อระบุ X และ Y position ใน date_lov.get_dae สิ่งที่ควรระวังคือหามใช parameter: position ในคําสั่ง set_window_property ที่
เราใชเพื่อกําหนดต่ําแหนงของ runtime Form ใน windows ตัวอยางเชน

SET_WINDOW_PROPERTY(:SYSTEM.EVENT_WINDOW,POSITION,20,15);

ถาใชคําสั่งนี้มีผลทําใหการแสดง calendar บน Form จะแสดงที่ต่ําแหนง 0,0 และถือวาคาที่กําหนดใน X, Y position ไมมีความหมาย


ปกติเราใชคําสั่ง set_window_property ใน when-window-activated trigger

ขั้นตอนการเพิ่ม Calendar ใน Form


การเพิ่ม calendar ใน Form เราตองใชไฟลสําคัญ 2 ไฟลคือ stndrd20.olb และ calendar90.pll ทั้ง 2 ไฟลเปนของ Oracle ผูเขียน
เตรียมไวใหแลวเก็บไวที่ c:\dev10g\library\chap_20

1) สราง Employee master form ดังรูป

รูปที่ 20-55 Employee master form

Table ที่เกี่ยวของกับการสราง Form


- tbl_m_emp_calendar

สราง table นี้ไดโดย run script file: chap_20_02.sql จาก SQL*Plus ใหพิมพคําสั่ง

SQL>@c:\dev10g\script\chap_20\chap_20_02.sql

Script file : Chap_20_02.sql


Table Name : tbl_m_emp_calendar

บทที่ 20 List of Values, Radio Button, Item List และ Popup Menus
378

Desc : ทะเบียนพนักงาน เก็บ Information ของพนักงาน

Column Name Data Type Size Attributes


emp_code NUMBER 5
emp_name VARCHAR2 10
birth_date DATE
birth_place VARCHAR2 20
salary NUMBER 10,2

ขอมูลตัวอยาง
Emp_code Emp_name Birth_date Birth_place Salary
10001 JOHNSON SYSDATE BANGKOK 25000
10002 SMITH SYSDATE BANGKOK 78000
10003 MARRY SYSDATE BANGKOK 11000
10004 WILLIAM SYSDATE BANGKOK 8000
10005 LINDA SYSDATE BANGKOK 43000

จากขอมูลตัวอยาง ผูเขียนกําหนดให birth_date คือวันที่เดียวกันกับ sysdate ทั้งนี้เพื่อใหงายตอการสรางตัวอยางขอมูล

2) นํา PL/SQL Library ชื่อ calendar90.pll ไปผูก (attach) กับ Form ที่สรางไวในขั้นตอนแรก

รูปที่ 20-56 Attached Libraries node ใน Object navigator

จากรูป คลิกที่ Attached Libraries node แลวคลิกเครื่องหมายบวกสีเขียวมุมบนซายของ Object navigator หรือจาก menu bar
เลือก Edit->Create จะปรากฎหนาจอใหใส Library ใหคลิกปุม Browse เลือก calendar90.pll ที่เก็บใน c:\dev10g\library\chap_20
แลวคลิกปุม Attach จะมีหนาจอ dialog box แสดงขอความให remove path ใหเลือกเปน No

รูปที่ 20-57 PL/SQL Library ใน Attached Libraries node

จากรูป แสดง PL/SQL Library ชื่อ calendar90.pll ที่ถูก attach เขากับ Form แลว

บทที่ 20 List of Values, Radio Button, Item List และ Popup Menus
379

3) Compile ไฟล calendar90.pll ใหเปน calendar90.plx


ใหเปดไฟล PL/SQL Library โดยจาก menu bar เลือก File->Open เลือก file type เปน PL/SQL Libraries (*.pll) เลือกไฟล
calendar90.pll ที่เก็บไวใน c:\dev10g\library\chap_20

รูปที่ 20-58 เลือก PL/SQL Library ที่ตองการ compile

จากรูป คลิกเลือก library ที่ตองการ complie คือ calendar90 ที่อยูภายใต PL/SQL Libraries node แลวกดปุม Ctrl+T หรือจาก
menu bar เลือก Program->Compile Module ก็จะไดไฟล calendar90.plx เก็บไวใน folder ที่เดียวกันกับ calendar90.pll

ถาไม compile เมื่อคลิกปุมใหแสดง calendar จะไมสามารถแสดงไดโดยจะมี error message แจงกลับมาคือ

FRM-40735: WHEN-BUTTON-PRESSED trigger raised unhandled exception ORA-06508.

เมื่อ compile ไดไฟล calendar90.plx แลว ใหนําไฟล calendar90.pll, calendar90.plx และ stndrd20.olb ไปไวในไวใน default
directory ของ Form คือ C:\DevSuiteHome_1\forms ถาไปไวที่อื่น เชน c:\dev10g\lib\chap_20 เราตองสราง path ใหมิฉะนั้นเมื่อ
สั่ง run Form จะทําให calendar ไมถูกแสดงออกมาเนื่องจากหา calendar90.plx ไมพบ

การสราง path ใหเขาไปแกไขคาในไฟล default.env ไฟลนี้เก็บไวที่ C:\DevSuiteHome_1\forms\server ใหเพิ่ม path ตอทายเปน

Forms path กอนแกไข


FORMS_PATH=C:\DevSuiteHome_1\forms

Forms path หลังแกไข


FORMS_PATH=C:\DevSuiteHome_1\forms;C:\Dev10g\Library\Chap_20

เมื่อแกไขเรียบรอยแลวให save ไฟล และปดโปรแกรม Forms Builder แลวเปดขึ้นมาใหม

4) นํา object group ชื่อ calendar มาไวใน Object groups node


ใหหา object library ชื่อ standards ซึ่งถูกเก็บไวใน Object libraries node แตกอนอื่นตองเปด object library ชื่อ strndrd20.olb ใน
Object navigator กอนโดยใชคําสั่ง File->Open แลวเลือก file type เปน Object Libraries (*.olb) ใหหาไฟล stndrd20.olb (เก็บ
ไวใน c:\dev10g\lib\chap_20) แลวคลิกเลือก

บทที่ 20 List of Values, Radio Button, Item List และ Popup Menus
380

รูปที่ 20-59 Object library: standards ใน Object Libraries node

จากรูป เมื่อเปดไฟลไดแลว ภายใต standares ใหคลิกที่ Library Tabs จะปรากฎ objects ยอยดังรูป

2) ใชเมาสลาก calendar ไป
ไวที่ Object Groups node

1) ดับเบิลคลิกที่
components

รูปที่ 20-60 Component: calendar กอนยายไปไวใน Object groups node

จากรูป ใหดับเบิลคลิกที่ components จะปรากฎหนาจอใหเลือก object ยอย ใหคลิกเลือก calendar แลวยายโดยใชเมาสลากไปไวใน


Object groups node ในขณะที่ยายจะมีขอความใหเลือก copy หรือ subclass ใหเลือกเปน copy

บทที่ 20 List of Values, Radio Button, Item List และ Popup Menus
381

รูปที่ 20-61 Component: calendar ใน Object Groups node

จากรูป component: calendar ถูกยายไปไวใน Object Groups node แลว หลังยาย component: calendar สังเกตไดวามี objects
ตางๆเปนจํานวนมากถูกเพิ่มเขามาใน Object navigator

5) สราง trigger code เพื่อใชเรียก calendar


เราอาจสราง trigger code ไวใน button, item หรือ menu ก็ไดเพื่อใหเปด calendar แตการเรียกใช calendar ตองใช procedure:
date_lov.get_date

Button แสดง
calendar

รูปที่ 20-62 Form หลังสราง button เพื่อใชเรียก calendar

จากรูป ผูเขียนสราง button ไวดานขวาของ item: ฺBirth date และใน button ไดเขียน trigger code ใน when-button-pressed
trigger ดังนี้

บทที่ 20 List of Values, Radio Button, Item List และ Popup Menus
382

Trigger Name: WHEN-BUTTON-PRESSED


Form : F_EMP_CALENDAR
Block : BUTTON_PALETTE_BLK
Item : SHOW_CALENDAR_BUT
Trigger Level : ITEM LEVEL
Function : แสดง calendar เพื่อใหเลือก date
Code :
BEGIN
date_lov.get_date (sysdate,
'birth_date',
200,
20,
'Date List of Values',
'OK',
'Cancel',
TRUE,
FALSE,
TRUE);
END;
Desc :

นอกจากเขียน trigger code ไวใน button แลว ถาเราไมตองการใชวิธีคลิกที่ button เพื่อใหแสดง calendar (ไมตองการใหแสดง
button บน Form) แตตองการใชวิธีกดปุม Ctrl+L แทนเหมือนกับการเรียก List of Values ออกมาใชงานก็สามารถทําได

ใหนํา trigger code ทั้งหมดที่เขียนใน when-button-pressed trigger ไปไวใน key-listval trigger ของ item: birth_date แทนก็จะ
ไดผลเชนเดียวกันกับที่เขียนไวใน button การเรียกใชงานเมื่อเคอเซอรอยูที่ field: Birth date บน Form ใหเรากดปุม Ctrl+L จะเปน
การประมวลผลคําสั่งที่เขียนใน key-listval trigger ใหแสดง calendar ออกมา

รูปที่ 20-63 Form หลังเพิ่ม calendar

จากรูป เมื่อผูใชคลิกที่ปุมขาง field: Birth date หรือเมื่อเคอเซอรอยูที่ field: Birth date ใหกดปุม Ctrl+L จะปรากฎหนาจอ calendar
ใหคลิกเลือกวันที่ที่ตองการแลววันที่นั้นจะถูกนําไปแสดงใน field: Birth date และเคอเซอรจะเลื่อนไปยัง field ถัดไปคือ Birth place
ทันทีโดยอัตโนมัติ

ดูตัวอยางการสราง LOV และ Display item ไดที่


File Name : F_SALES_ORDER_V1.FMB
Location : C:\dev10g\Form\chap_20

ดูตัวอยางการสราง LOV บนปุม (pushbutton) และ Display item ไดที่


File Name : F_SALES_ORDER_V2.FMB
Location : C:\dev10g\Form\chap_20

ดูตัวอยางการสราง Radio button group ไดที่


File Name : F_EMP_MASTER.FMB
Location : C:\dev10g\Form\chap_20

บทที่ 20 List of Values, Radio Button, Item List และ Popup Menus
383

ดูตัวอยางการสราง Item List ไดที่


File Name : F_SHOW_ITEM_LIST.FMB
Location : C:\dev10g\Form\chap_20

ดูตัวอยางการสราง List item แบบ Poplist เพื่อแสดงรหัสสินคา (Item code) ไดที่


File Name : F_SALES_ORDER_V3.FMB
Location : C:\dev10g\Form\chap_20

ดูตัวอยางการสราง Popup menus ไดที่


File Name : F_EMP_POPUP.FMB
Location : C:\dev10g\Form\chap_20

ดูตัวอยางการเพิ่ม Calendar ใน Form ไดที่


File Name : F_EMP_CALENDAR.FMB
Location : C:\dev10g\Form\chap_20

บทที่ 20 List of Values, Radio Button, Item List และ Popup Menus

You might also like