3.5.3. Button Example

This example shows how the cx-create-button macro is used to create a new button, and how the callback argument works. In the cx-create-button statement below, the 'activate-callback button-cb argument ensures that the button-cb procedure is called each time that the button is clicked. The button-cb function is set up with the line (define (button-cb . args), which is just like how the apply-cb and update-cb functions are set up in a fully functional dialog box (see Comprehensive Examples). Once you open the button-cb procedure with this line you can then write the code to give it functionality.

In this example, the variable counter is incremented by 1 each time the button is clicked and the number of times that the button has been clicked is output to a text entry field txtField. This dialog box does not do anything when the OK button is clicked because we have substituted Boolean values for the apply-cb and update-cb arguments, which would normally be function calls like the button-cb argument is in the cx-create-button line. For more information on the apply-cb and update-cb functions, see cx-create-panel.

(define apply-cb #t)
(define update-cb #f)

(define table)
(define txtField)

(define counter 0)
(define (button-cb . args)
  (set! counter (+ counter 1))
  (cx-set-text-entry txtField (string-append "Times Clicked: " (number->string counter)))
)

(define my-dialog-box (cx-create-panel "My Dialog Box" apply-cb update-cb))

(set! table (cx-create-table my-dialog-box "This is an example Dialog Box"))

(set! txtField (cx-create-text-entry table "" 'row 0 'col 0))
(cx-create-button table "Button" 'activate-callback button-cb 'row 1 'col 0)

(cx-show-panel my-dialog-box)

To view additional examples of buttons, see Comprehensive Examples.


Release 18.2 - © ANSYS, Inc. All rights reserved.