Navigation:  thinBasic Modules > UI (User Interface) > CONTROLs >

CONTROL ADD BUTTON

Previous pageReturn to chapter overviewNext page

 

Description

 

Add a command button to a dialog.

A command button is a button that causes an action to occur when the button is clicked. A common example of a command button is the "OK" button on a message box dialog.

 

Syntax

 

hndl = CONTROL ADD BUTTON, hwnd, ctrlID, txt, xPos, yPos, Width, Height [, [Style] [, [ExStyle]]] [[,] CALL CallBack]

 

Returns

 

Number

Control window handler.

 

Parameters

 

Name

Type

Optional

Meaning

hwnd

Number

No

Handle of the dialog containing the control

ctrlID

Number

No

Control identifier

txt

String

No

A text expression representing the text you want to assign to the control.

xPos

Number

No

Horizontal position of the control inside the dialog

yPos

Number

No

Vertical position of the control inside the dialog

Width

Number

No

Control width

Height

Number

No

Control height

Style

Number

Yes

Primary style of the control. See BUTTON Style equates

If no Style is specified, the following expression is assumed as default:

%BS_CENTER OR %BS_VCENTER OR %WS_TABSTOP

ExStyle

Number

Yes

Extended style of the control. See BUTTON ExStyle equates

If no ExStyle is specified, the following expression is assumed as default:

%WS_EX_LEFT

CallBack

Function

Yes

Optional name of a Callback Function that receives all %WM_COMMAND and %WM_NOTIFY messages for the control.

 

If a callback for the control is not designated, you must create a dialog Callback Function to process messages from your control.

 

If the Callback Function processes a message, it should return %TRUE (non-zero) to prevent the message being passed unnecessarily to the dialog callback (if one exists). The dialog callback should also return %TRUE if the notification message is processed by that Callback Function.

 

Remarks

 

In general, if the control Callback Function processes a message, it should return %TRUE (non-zero) to prevent the message being passed unnecessarily to the dialog callback (if one exists). The dialog callback should also return %TRUE, if the notification message is processed by that Callback Function.

 

Notification messages are sent to the Callback Function, with CBMSG = %WM_COMMAND, CBCTL holding the ID (ctrlID) of the control, and CBCTLMSG holding the following values:

%BN_CLICKED        Sent when the user clicks a mouse button, or activates the button with the hot-key (unless the button has been disabled).

%BN_DISABLE        Sent when a button is disabled.

%BN_KILLFOCUS        Sent when a button loses the keyboard focus. The button must include the %BS_NOTIFY style.

%BN_SETFOCUS        Sent when a button receives the keyboard focus. The button must include the %BS_NOTIFY style.

 

When a Callback Function receives a %WM_COMMAND message, it should explicitly test the value of CBCTL and CBCTLMSG to guarantee it is responding appropriately to the notification message.

 

Restrictions

 

See also

 

Examples

 

'---Add 2 buttons

control add buttoncbhndl, %ID_Button_01, "I'm the button 1", 110,  90, 70, 20, %BS_NOTIFY | %WS_TABSTOP | %BS_DEFAULTCALL cbButton

control add buttoncbhndl, %ID_Button_02, "I'm the button 2", 110, 120, 70, 20, %BS_NOTIFY | %WS_TABSTOPCALL cbButton

 

'...

 

'------------------------------------------------------------------------------

' Callback procedure for button control

'------------------------------------------------------------------------------

callback Function cbButton() As Long

 

  '---Always check CBMSG = %WM_COMMAND

  '---Also %WM_NOTIFY messages are sent to button callback

  If CBMSG = %WM_COMMAND Then

 

    '---Check control specific message

    Select Case CBCTLMSG

      Case %BN_CLICKED

 

        '---Check the control ID

        Select Case CBCTL

          Case %ID_Button_01

            '...Do something when button is clicked

          Case %ID_Button_02

            '...Do something when button is clicked

        End Select

      Case %BN_DISABLE

        Select Case CBCTL

          Case %ID_Button_01

            '...

          Case %ID_Button_02

            '...

        End Select

      Case %BN_KILLFOCUS

        Select Case CBCTL

          Case %ID_Button_01

            '...

          Case %ID_Button_02

            '...

         End Select

      Case %BN_SETFOCUS

        Select Case CBCTL

          Case %ID_Button_01

             '...

          Case %ID_Button_02

            '...

        End Select

    End Select

 

    Function = %True

 

  End If

    

End Function