Thread Rating:
  • 496 Vote(s) - 2.75 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Page 8: Extra Practice
06-16-2011, 04:05 PM, (This post was last modified: 06-23-2011, 04:03 PM by ashimo.)
#1
Page 8: Extra Practice
Extra Practice

Practice Problem 1: Basics

Let’s create a program that uses a CommandButton to change the color of a Frame.



Hint: Give a name to the Frame, and then set the background option in the event handler.

Solution

Solution Program: c:\Curl\Try1\04_exercise1\start.curl

Code:
{value
    let f:Frame = {Frame
                      background = yellow,
                      height = 5cm,
                      width = 5cm
                  }
    let red-button:CommandButton = {CommandButton
                                       label = red,
                                       {on Action do
                                           set f.background = red
                                       }
                                   }
    let blue-button:CommandButton = {CommandButton
                                        label = blue,
                                        {on Action do
                                            set f.background = blue
                                        }
                                    }
    {VBox
        spacing = 5pt,
        halign = center,
        f,
        {HBox
            spacing = 10pt,
            red-button,
            blue-button
        }
    }
}

Practice Problem 2: Application

Let’s add a reset button (reset-btn) to the above solution. Set the initial state of start-btn to invalid, and then add the following processing depending on which of the following three buttons are pressed.

start-btn
  • Disable start-btn
  • Enable stop-btn

stop-btn
  • Enable start-btn
  • Disable stop-btn

reset-btn
  • Reset start-time to null
  • Enable start-btn
  • Disable stop-btn


Hint: Use the enabled? option to switch buttons between enabled and disabled.

Solution

Solution Program: c:\Curl\Try1\05_exercise2\start.curl

Code:
{value
    let start-time:#DateTime
    let start-btn:CommandButton = {CommandButton label = Start}
    let stop-btn:CommandButton = {CommandButton label = Stop, enabled? = false}
    let reset-btn:CommandButton = {CommandButton label = Reset}
    
    {start-btn.add-event-handler
        {on Action do
            set start-time = {DateTime}
            set start-btn.enabled? = false
            set stop-btn.enabled? = true
        }
    }
    
    {stop-btn.add-event-handler
        {on Action do
            set start-btn.enabled? = true
            set stop-btn.enabled? = false
            let elapsed-time:Time = {start-time.elapsed}
            let time-double:double = elapsed-time / 1s
            let score:int = {switch time-double using <=
                             case 10 do (time-double * 10) asa int
                             case 20 do {abs ((20 - time-double) * 10) asa int}
                             else 0
                            }
            
            {popup-message {VBox
                               {center font-size = 20pt, {value score} points!!},
                               {center {value {start-time.elapsed}}}
                           }
            }
        }
    }
    
    {reset-btn.add-event-handler
        {on Action do
            set start-time = null
            set start-btn.enabled? = true
            set stop-btn.enabled? = false
        }
    }
    
    {VBox
        margin = 10pt,
        spacing = 10pt,
        halign = center,
        {bold Try to stop in 10 seconds},
        start-btn,
        stop-btn,
        reset-btn
    }
}


Possibly Related Threads...
Thread Author Replies Views Last Post
  Page 7: Summary ashimo 0 3,056 06-16-2011, 03:57 PM
Last Post: ashimo
  Page 6: Let’s look at the make-up of this game ashimo 0 3,260 06-16-2011, 03:50 PM
Last Post: ashimo
  Page 5: Application... A “Push the Button in 10s” Game ashimo 0 3,213 06-16-2011, 03:33 PM
Last Post: ashimo
  Page 4: Structure of our program that uses events ashimo 0 2,906 06-16-2011, 03:30 PM
Last Post: ashimo
  Page 3: Using Different Events ashimo 0 3,127 06-16-2011, 03:25 PM
Last Post: ashimo
  Page 2: Structure of a Program that Displays Values input from Controls ashimo 0 3,015 06-16-2011, 02:18 PM
Last Post: ashimo
  Page 1: Making a Simple Action Game ashimo 0 2,815 06-16-2011, 02:03 PM
Last Post: ashimo
Forum Jump:


Users browsing this thread:
1 Guest(s)

MyBB SQL Error

MyBB has experienced an internal SQL error and cannot continue.

SQL Error:
1017 - Can't find file: 'mybb_threadviews' (errno: 2)
Query:
INSERT INTO mybb_threadviews (tid) VALUES('54')