งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

Lecturer: Kanjana Thongsanit

งานนำเสนอที่คล้ายกัน


งานนำเสนอเรื่อง: "Lecturer: Kanjana Thongsanit"— ใบสำเนางานนำเสนอ:

1 Lecturer: Kanjana Thongsanit
User Forms Lecturer: Kanjana Thongsanit

2 สร้าง UserForm

3

4 Toolbox แถวแรกประกอบด้วย Label, TextBox , ComboBox , ListBox
แถวที่สองประกอบด้วย CheckBox, OptionButton, ToggleButton, Frame, Command Button แถวที่สามประกอบด้วย TabStrip, MultiPage, ScrollBar , SpinButton, Image แถวที่สี่ประกอบด้วย RefEdit

5 Toolbox Command Button ใช้สำหรับ run program
Label ใช้เขียนคำอธิบายต่างๆ Text Box เพื่อรับค่าป้อนข้อมูลของผู้ใช้ List Box เพื่อให้ผู้ใช้เลือกค่า 1 ค่า หรือมากกว่า 1 ค่า Combo Box คล้ายกับ list Box แต่ว่าต่างกันตรงที่ ผู้ใช้สามารถพิมพ์ ค่า เพิ่มเติมลงไปได้

6 Toolbox Check Box เลือก option ที่พอใจ (เลือกได้มากกว่า 1 )
Option Button หรือเรียกว่า radio button เลือก option ที่ พอใจ โดยเลือกได้ทางเลือกเดียว Frame ใช้เพื่อจัดกลุ่ม set ของ option button RefEdit ใช้สำหรับให้ผู้ใช้ เลือก Range ใน worksheet เมื่อเราเลือก Tool Box จะมี property ที่เปลี่ยนแปลงไปขึ้นกับว่าเรา เลือก property อะไร

7 ออกแบบหน้าจอ

8 Tools Name Property Caption Property User form frmInputs Product Inputs Command btnOk (default property: True ) OK btnCancel Cancel Label This is for practice only, to see how controls on user forms work. Product TextBox txtProduct Frames Region of Origin Shipping Method Option Button optEast East optWest West Check box ChkPerish Perichable ChkFragile Fragile ListBox lbCustomer (เพิ่ม label ด้านบน Customers)

9 ออกแบบหน้าจอ

10 Tab order การที่ผู้ใช้มีการกรอกข้อมูลหลายจุด ผู้ใช้มักเคยชินกับการใช้ Tab เพื่อเลื่อน เปลี่ยนเพื่อกรอกข้อมูลแต่ละช่อง เช่น Tab order เราสามารถตรวจสอบตำแหน่งปัจจุบันได้ จาก View  Tab order ที่ VBE ซึ่งผู้ใช้สามารถเลือกเปลี่ยนลำดับของ Tab order ได้

11 Testing the form   กด Run

12 Writing Event Handlers
การ click ที่ปุ่ม command การป้อนข้อมูล ใน Textbox การเลือกที่ option button

13 Writing Event Handlers
List of Controls List of Events for a command button

14 property และ event ของ tools ต่างๆ ได้ที่ object Brower
เลือก Msform เลือก tool ที่ต้องการ กด help

15

16 ตัวอย่าง 3 เหตุการณ์ UserForm_Initialize( ) btnOK_Click( )
btnCancel_click( )

17 เหตุการณ์ UserForm_Initialize( ) code
เป็นเหตุการณ์เริ่มต้นที่อยากให้เกิดขึ้น

18 สิ่งต้องการกำหนดเริ่มต้น

19 เหตุการณ์ UserForm_Initialize( ) code
ต้องการให้ East and Truck ถูกเช็ค การกำหนดว่า optionButton ถูกเช็ค สามารถ ใช้คำสั่ง ตัวอย่างเช่น optEast.value = True ใน Text box ต้องมีการกำหนด เริ่มต้นว่าข้อมูลที่มีว่างเปล่า txtProduct.Text = ""

20 เหตุการณ์ UserForm_Initialize( ) code
รายชื่อ ใน list box กำหนดให้ เป็นรายชื่อของ ลูกค้า สำหรับ list box จะมี method ชื่อ add item ในการเพิ่มรายชื่อ เช่นถ้าเราตั้งชื่อ range ว่า Customers สามารถกำหนดได้ดังนี้ For Each cell In Range("Customers") lbCustomer.AddItem cell.Value Next

21 Row Source ใน list box หรือ อีกวิธีหนึ่งคือ
กำหนด Row Source property ของ list box ว่า customers

22 เหตุการณ์ btnCancel_Click( )
Private Sub btnCancel_Click() Unload Me End End Sub คำว่า unload Me หมายถึง unload userform

23 เหตุการณ์ btnOK_Click()
โดยปกติ หลังจากที่ผู้ใช้ได้กรอกข้อมูลใน form จะต้องมีการตรวจสอบก่อนจะมี error ที่เกิดขึ้น เช่น ผู้ใช้อาจลืมกรอกข้อมูล ใน Text box ผู้ใช้อาจกรอกข้อมูลไม่ถูกต้อง ใน Text box เช่นถ้าใน Text box ต้องการ ให้กรอก เลข แต่ผู้ใช้กรอกเป็นตัวหนังสือ จะทำให้ โปรแกรมเกิด error ได้ เราสามารถตรวจสอบว่า ข้อมูลเป็นตัวเลขหรือเปล่า ด้วย IsNumeric(ระบุข้อมูลที่ต้องการเช็ค ) ผู้ใช้อาจไม่ได้เลือกข้อมูลใน list box

24 Check สร้าง code สำหรับเมื่อกดปุ่ม ok
สิ่งที่ต้องการคือ 1. ต้องการให้ผู้ใช้กรอก เลข ระหว่าง 1 – 1000 ในช่อง text box ถ้าพบกรณีเหล่านี้ ผู้ใช้ลืมกรอกข้อมูลใน text box (ให้เขียน code เพื่อตรวจสอบ) ผู้ใช้กรอกข้อมูลเป็น ข้อความ ผู้ใช้กรอกเป็นตัวเลขแต่ ไม่ได้อยู่ในช่วง ถ้าพบกรณีเหล่านี้ ให้แสดง MsgBox ว่า “Enter a product number from 1 to 1000”

25 สร้าง code สำหรับเมื่อกดปุ่ม ok
2. เราต้องการแสดงผล โดยการแสดงผล โดย ตัวอย่างด้านล่าง ดังนั้นต้อง ส่งค่าจากที่ผู้ใช้เช็คหรือกรอกข้อมูลมาที่ตัวแปรที่กำหนดใน module เช่น If optEast.value Then region = “East” Then ….

26 Private Sub btnOK_Click() ' Capture the value in the text box, but make sure it is from 1 to ' Capture region (in a string variable). ' Capture shipping method (in a string variable). ' Capture check box settings (in Boolean variables). isPerishable = …. ' Capture customer (in a string variable). With lbCustomer If .ListIndex <> -1 Then customer = .Value Else MsgBox "Select a customer from the list." .SetFocus Exit Sub End If End With ' Unload the form. Unload Me End Sub

27 สร้าง module เพื่อแสดงผล
Public productIndex As Integer Public region As String Public customer As String Public shipping As String Public isPerishable As Boolean Public isFragile As Boolean

28 สร้าง module เพื่อแสดงผล
Sub Main() frmInputs.Show MsgBox "The user chose the following:" & vbCrLf _ & "Product index: " & productIndex & vbCrLf _ & "Region of origin: " & region & vbCrLf _ & "Shipping method: " & shipping & vbCrLf _ & "Perishable? " & isPerishable & vbCrLf _ & "Fragile? " & isFragile & vbCrLf _ & "Customer: " & customer, vbInformation, "User inputs" ' The rest of the program would then act on the user's inputs. End Sub

29 กรณีที่เรามี List หลาย ๆ อัน (multi list box)
วิธีการเขียน code เพื่อดึงค่า list box ที่ถูกเลือกได้โดย ใช้ selected property เพื่อหาว่า list อันไหนถูกเลือก เช่น (ListCount คือ จำนวน item ใน list ทั้งหมด) For i = 1 To lbproducts.ListCount isChosen(i) = lbProducts.Selected(i-1) Next

30 เปิด file Homeloan.xls


ดาวน์โหลด ppt Lecturer: Kanjana Thongsanit

งานนำเสนอที่คล้ายกัน


Ads by Google