วันพุธที่ 17 กรกฎาคม พ.ศ. 2556

Visual Basic 6.0: 2 ออกกำลังกาย "สั่งซื้อ" Pizza (ภายใต้การก่อสร้างอยู่ระหว่างการจัดทำ)




Wednesday, July 17, 2013


Visual Basic 6.0 : Exercise 2 " Pizza Order"

(Under Construction อยู่ระหว่างการจัดทำ)     


     Teacher  Phaitoon  Yaemprasuan  :   occupatech@gmail.com
                                                                http://picta55.blogspot.com


Exercise 2

Pizza  Order

 We had just learned about programming with Microsoft Visual Basic 6.0 and tried to create two Windows applications (using Visual Basic 6.0), the first one was "Stop Watch" and the second was "Calendar/Time".  Some people have told me, the teacher, that it is interesting and challenging, while some other people have never said any word but have just thought that it is boring. Yes, the teacher realizes that programming is so valuable because it can develop anyone both systematic thinking and solving. Well, the teacher knows  that anybody can never hand on it if he is not interested in programming. But, now the teaching is not over, the show must go on! 
วกเราได้เรียนรู้เกี่ยวกับการโปรแกรมด้วยภาษาวิชวลเบสิก 6.0  และได้ลองพัฒนาแอ็ปปลิเคชันที่ทำงานในระบบวินโดวส์ 2 แอ็ป ด้วยกัน  โดยแอ็ปแรกเป็นนาฬิกาจับเวลา แอ็ปที่สองเป็นแอ็ปบอกวันเวลา  นักเรียนหลายคนก็บอกกับครูว่า "น่าสนใจและท้าทายมาก"  ขณะที่นักเรียนอีกหลายคนไม่พูดอะไรสักแอ่ะ...คิดอยู่อย่างเดียวว่า "มันน่าเบื่อ"   ใช่เลย..ครูตระหนักดีอยู่เสมอว่า ความจริงแล้วนะ.. การเรียนรู้เรื่องการโปรแกรมมันมีคุณค่าในตัวของมันเองอยู่แล้ว  เพราะมันเป็นการพัฒนาผู้เรียนให้รู้จักการคิด/การแก้ปัญหาอย่างเป็นระบบ  ครู..ทราบดีว่า.. นักเรียนคนไหนก็ตามที่รู้สึกว่าตนเองไม่สนใจในเรื่องของการโปรแกรมแล้วละก็ คงยากนะที่จะเอาดีในเรื่องนี้ได้ ... แต่ช้าก่อน... การเรียนการสอนก็คงหยุดไม่ได้ ... ก็ต้องว่ากันต่อไป..

 Pizza Order แอ็ปสั่งพิซซ่า

     SOURCE  :   www.kidwares.com

1. Start a new project.  We'll build a form where a pizza order can be entered by simply clicking on check boxes and option buttons.
ให้นักเรียนเข้าไปที่หน้าจอหลักของ Microsoft Visual Basic 6.0 เหมือนที่เราเคยทำ ด้วยการเลือก Standard EXE เพื่อสร้าง New project .... อย่าลืม Save นะ  ซึ่งการ Save ครั้งแรก จะเป็น Save As โดยนักเรียนต้อง Save ฟอร์มก่อน แล้วจึง save โปรเจ็กต์ ... เลือก Folder ให้เรียบร้อย  (เรื่องนี้ นักเรียนถูกย้ำมาจนเป็นทักษะดีอยู่แล้วจากครูผู้สอน)  ตอนนี้เรากำลังจะสร้างแอ็ปสั่งพิซซ่า  โดยผู้ใช้ก็แค่ทำการคลิกเลือกตัวเลือก และติ๊กรายการต่างๆ แล้วสั่งการผ่านปุ่มคำสั่ง..

Fig.1  The Form which is being designed by us.
นี่เป็นฟอร์มที่กำลังได้รับการออกแบบจากนักเรียน

2. Draw three frames.  In the first, draw three option buttons, in the second, draw two option buttons, and in the third, draw six check boxes.  Draw two option buttons on the form.  Add two command buttons.  Make things look something like this.








3. Set the properties of the form and each control.

Form1:
    BorderStyle    1-Fixed Single
    Caption    Pizza Order
    Name    frmPizza

Frame1:
    Caption    Size

Frame2:
    Caption    Crust Type

Frame3
    Caption    Toppings


Option1:
    Caption    Small
    Name    optSize
    Value    True

Option2:
    Caption    Medium
    Name    optSize (yes, create a control array)

Option3:
    Caption    Large
    Name    optSize

Option4:
    Caption    Thin Crust
    Name    optCrust
    Value    True

Option5:
    Caption    Thick Crust
    Name    optCrust (yes, create a control array)

Option6:
    Caption    Eat In
    Name    optWhere
    Value    True

Option7:
    Caption    Take Out
    Name    optWhere (yes, create a control array)

Check1:
    Caption    Extra Cheese
    Name    chkTop

Check2:
    Caption    Mushrooms
    Name    chkTop (yes, create a control array)

Check3:
    Caption    Black Olives
    Name    chkTop


Check4:
    Caption    Onions
    Name    chkTop

Check5:
    Caption    Green Peppers
    Name    chkTop

Check6:
    Caption    Tomatoes
    Name    chkTop

Command1:
    Caption    &Build Pizza
    Name    cmdBuild

Command2:
    Caption    E&xit
    Name    cmdExit

The form should look like this now:



4. Declare the following variables in the general declarations area:

Option Explicit
Dim PizzaSize As String
Dim PizzaCrust As String
Dim PizzaWhere As String

This makes the size, crust, and location variables global to the form.
5.
Attach this code to the Form_Load procedure.  This initializes the pizza size, crust, and eating location.

Private Sub Form_Load()
'Initialize pizza parameters
PizzaSize = "Small"
PizzaCrust = "Thin Crust"
PizzaWhere = "Eat In"
End Sub

Here, the global variables are initialized to their default values, corresponding to the default option buttons.

6. Attach this code to the three option button array Click events.  Note the use of the Index variable:

Private Sub optSize_Click(Index As Integer)
‘Read pizza size
PizzaSize = optSize(Index).Caption
End Sub

Private Sub optCrust_Click(Index As Integer)
‘Read crust type
PizzaCrust = optCrust(Index).Caption
End Sub

Private Sub optWhere_Click(Index As Integer)
‘Read pizza eating location
PizzaWhere = optWhere(Index).Caption
End Sub

In each of these routines, when an option button is clicked, the value of the corresponding button’s caption is loaded into the respective variable.

7.
Attach this code to the cmdBuild_Click event.

Private Sub cmdBuild_Click()
'This procedure builds a message box that displays your pizza type
Dim Message As String
Dim I As Integer
Message = PizzaWhere + vbCr
Message = Message + PizzaSize + " Pizza" + vbCr
Message = Message + PizzaCrust + vbCr
For I = 0 To 5
  If chkTop(I).Value = vbChecked Then Message = Message + chkTop(I).Caption + vbCr
Next I
MsgBox Message, vbOKOnly, "Your Pizza"
End Sub

This code forms the first part of a message for a message box by concatenating the pizza size, crust type, and eating location (vbCr is a symbolic constant representing a ‘carriage return’ that puts each piece of ordering information on a separate line).  Next, the code cycles through the six topping check boxes and adds any checked information to the message.  The code then displays the pizza order in a message box.

8. Attach this code to the cmdExit_Click event.

Private Sub cmdExit_Click()
End
End Sub

9. Get the application working.  Notice how the different selection buttons work in their individual groups.  Save your project.

10. If you have time, try these modifications:

A. Add a new program button that resets the order form to the initial default values.  You’ll have to reinitialize the three global variables, reset all check boxes to unchecked, and reset all three option button groups to their default values.

B. Modify the code so that if no toppings are selected, the message “Cheese Only” appears on the order form.  You’ll need to figure out a way to see if no check boxes were checked.




ไม่มีความคิดเห็น:

แสดงความคิดเห็น