Navigation: TextEd > Extension and Scripts > Visual components >

TButton

 

 

 

TButton is a push button control. Use TButton to put a standard push button on a form.

 

In some examples we assume a button has been created and is named "btnOk".

 

TButton btnOk = TButton.Create(form);

 


Properties



 

property TAlign Align;

Determines how the control aligns within its container (parent control).

 

enum TAlign {

  alNone,

  alTop,

  alBottom,

  alLeft,

  alRight,

  alClient,

  alCustom

};


 

Ex.

btnOk.Align = alNone;


 

property Bool AlignWithMargins;

Indicates whether a control should be constrained by margins. If AlignWithMargins is true, use the Margins property of the control to govern the spacing relative to other controls that are aligned with this one.


 

property TAnchors Anchors;

Specifies how the control is anchored to its parent.

 

enum TAnchorKind {

  akLeft,

  akTop,

  akRight,

  akBottom

};

 

Ex.

btnOk.Anchors = akRight + akBottom;


 

property Bool Cancel;

Determines whether the button's OnClick event handler executes when the Escape key is pressed.


 

property String Caption;

Specifies a text string that identifies the control to the user.  It can be the text label of a radio button or the title text in a form. To underline a character in a Caption, include an ampersand (&) before the character.


 

property Bool Default;

Determines whether the button's OnClick event handler executes when the Enter key is pressed.


 

property Bool Enabled;

Controls whether the control responds to mouse, keyboard, and timer events.


 

property TFont Font;

Controls the attributes of text written on or in the control. To change to a new font, specify a new TFont object. To modify a font, change the value of the Charset, Color, Height, Name, Pitch, Size, or Style of the TFont object.

 

Ex.

btnOk.Font.Size = 12;


 

property int Height;

Specifies the vertical size of the control in pixels.

 

Ex.

btnOk.Height = 27;


 

property String Hint;

Contains the text string that can appear when the user moves the mouse over the control.


 

property int Left;

Specifies the horizontal coordinate of the left edge of a component relative to its parent.


 

property TMargins Margins;

Specifies the margins for the control. TMargins help define the relative position between components on a form, and between the edges of the form and the component. For example, when you set a left margin for a component to 10 pixels, the component will not come closer than 10 pixels to the edge of the container, or to another component on the left edge. The number of pixels by which two components are separated is the sum of the pixels of both components.

TMargins have the class members top, bottom, left and right.

 

Ex.

btnOk.Margins.Right = 4;


 

property TModalResult ModalResult;

Determines whether and how the button closes its (modal) parent form. Setting the TButton component's ModalResult property is an easy way to make clicking the button close a modal form. When a button is clicked, the ModalResult property of its parent form is set to the same value as the button's ModalResult property.

For example, if a dialog box has OK and Cancel buttons, their ModalResult properties could be set at design time to mrOk and mrCancel, respectively. At runtime, clicking the OK button then changes the dialog's ModalResult property to mrOk, and clicking the Cancel button changes the dialog's ModalResult property to mrCancel.


 

TModalResult = (mrNone, mrOk, mrCancel, mrAbort,

                mrRetry, mrIgnore, mrYes, mrNo, mrAll,

                mrNoToAll, mrYesToAll).


 

Ex.

btnOk.ModalResult = mrOk;


 

property String Name;

Specifies the name of the component as referenced in code.


 

property TPadding Padding;

Specifies the padding of a control. Padding works similar to margins.

 

Ex.

btnOk.Padding.Left = 4;


 

property TWinControl Parent;

Use the Parent property to get or set the parent of this control. The parent of a control is the control that contains the control. For example, if an application includes three radio buttons in a group box, the group box is the parent of the three radio buttons, and the radio buttons are the child controls of the group box.


Ex.

TButton btnOk = TButton.Create(form);

btnOk.Parent = form;

btnOk.Left = form.Width - btnOk.Width - 8;


 

property Bool ParentFont;

Determines where a control looks for its font information.

To have a control use the same font as its parent control, set ParentFont to true. If ParentFont is false, the control uses its own Font property.


 

property TPopupMenu PopupMenu;

Identifies the pop-up menu associated with the control.


 

property Bool ShowHint;

Determines whether the control displays a Help Hint when the mouse pointer rests momentarily on the control.


 

property TTabOrder TabOrder;

TabOrder is the order in which child windows are visited when the user presses the Tab key. The control with the TabOrder value of 0 is the control that has the focus when the form first appears.

 

Initially, the tab order is always the order in which the controls were added to the form. The first control added to the form has a TabOrder value of 0, the second is 1, the third is 2, and so on. Change this by changing the TabOrder property.  


 

property Bool TabStop;

Determines if the user can tab to a control.


 

property int Tag;

Stores an integer value as part of a component.


 

property int Top;

Specifies the Y coordinate of the top left corner of a control, relative to its parent or containing control in pixels.


 

property Boolean Visible;

Indicates whether the control is visible.


 

property int Width;

Specifies the horizontal size of the control or form in pixels.


 

property Bool WordWrap;

Specifies whether the button text wraps to fit the width of the control.

 

Set WordWrap to true to allow the display of multiple lines of text.


 


Methods



 

constructor Create(TComponent AOwner) 

Creates and initializes a new TButton object.


Ex.

TButton btnOk = TButton.Create(form);


 

procedure Free;

Destroys an object and frees its associated memory, if necessary. Visual controls added to a form is automatically destroyed when the form closes.


Ex.

TForm form = new TForm(nil);

TButton btnOk = TButton.Create(form);

...

btnOk.Free;


 

procedure Hide;

Hides the control.


 

procedure Show;

Shows the control. Use Show to set the control's Visible property to true.


 


Events



 

OnClick 

Occurs when the user clicks the control.

 

Ex.

 

TForm form;

TButton btnOk;

void btnOkClick(TObject Sender)

{

  // Do some work here

}

 

{

  form = new TForm(nil);  

 

  btnOk = TButton.Create(form);

  btnOk.Parent = form;

  btnOk.OnClick = &btnOkClick;

}


 

OnEnter 

Occurs when a control receives the input focus. Use the OnEnter event handler to cause any special processing to occur when a control becomes active.


Ex.

 

TForm form;

TButton btnOk;

void btnOkEnter(TObject Sender)

{

  // Do some work here

}

 

{

  form = new TForm(nil);

 

  btnOk = TButton.Create(form);

  btnOk.Parent = form;

  btnOk.OnEnter = &btnOkEnter;

}


 

OnExit 

Occurs when the input focus shifts away from one control to another. Use the OnExit event handler to provide special processing when the control ceases to be active.


Ex.

 

TForm form;

TButton btnOk;

void btnOkExit(TObject Sender)

{

  // Do some work here

}

 

{

  form = new TForm(nil);

 

  btnOk = TButton.Create(form);

  btnOk.Parent = form;

  btnOk.OnExit = &btnOkExit;

}


 

OnKeyDown 

Occurs when a user presses any key while the control has focus.

 

(ssShift, ssAlt, ssCtrl, ssLeft, ssRight, ssMiddle, ssDouble) TShiftState;

 

Ex.

 

TForm form;

TButton btnOk;

void btnOkKeyDown(TObject Sender, WORD &Key, TShiftState Shift)

{

  // Do some work here

  if (Key == VK_F1) {

    // F1 was pressed, lets do something...

 

  }

}

 

{

  form = new TForm(nil);

 

  btnOk = TButton.Create(form);

  btnOk.Parent = form;

  btnOk.OnKeyDown = &btnOkKeyDown;

}


 

OnKeyPress 

Occurs when a key is pressed.

 

Ex.

 

TForm form;

TButton btnOk;

void btnOkKeyPress(TObject Sender, char &Key)

{

  // Do something

}

 

{

  form = new TForm(nil);

  btnOk = TButton.Create(form);

  btnOk.Parent = form;

  btnOk.OnKeyPress = &btnOkKeyPress;

}


 

OnKeyUp 

Occurs when the user releases a key that has been pressed.

 

(ssShift, ssAlt, ssCtrl, ssLeft, ssRight, ssMiddle, ssDouble) TShiftState;


 

Ex.

 

TForm form;

TButton btnOk;

void btnOkKeyUp(TObject Sender, WORD &Key, TShiftState Shift)

{

  // Do some work here

}

 

{

  form = new TForm(nil);

 

  btnOk = TButton.Create(form);

  btnOk.Parent = form;

  btnOk.OnKeyUp = &btnOkKeyUp;

}


 

OnMouseDown 

Occurs when the user presses a mouse button with the mouse pointer over a control.


 

(ssShift, ssAlt, ssCtrl, ssLeft, ssRight, ssMiddle, ssDouble) TShiftState;


 

enum TMouseButton {

  mbLeft,

  mbRight,

  mbMiddle

};


 

Ex.

 

TForm form;

TButton btnOk;

void btnOkMouseDown(TObject Sender, TMouseButton Button, TShiftState Shift, int X, int Y)

{

  // Do some work here if left mouse button was pressed

  if (Button == mbLeft) {

    // Left mouse button was pressed. Lets do something...

 

  }

}

 

{

  form = new TForm(nil);

 

  btnOk = TButton.Create(form);

  btnOk.Parent = form;

  btnOk.OnMouseDown = &btnOkMouseDown;

}


 

OnMouseEnter 

Occurs when the user moves the mouse into a control.


Ex.

 

TForm form;

TButton btnOk;

void btnOkMouseEnter(TObject Sender)

{

  // Do some work here

}

 

{

  form = new TForm(nil);

 

  btnOk = TButton.Create(form);

  btnOk.Parent = form;

  btnOk.OnMouseEnter = &btnOkMouseEnter;

}


 

OnMouseLeave 

Occurs when the user moves the mouse outside of a control.


Ex.


TForm form;

TButton btnOk;

void btnOkMouseLeave(TObject Sender)

{

  // Do some work here

}

 

{

  form = new TForm(nil);

 

  btnOk = TButton.Create(form);

  btnOk.Parent = form;

  btnOk.OnMouseLeave = &btnOkMouseLeave;

}


 

OnMouseMove 

Occurs when the user moves the mouse pointer while the mouse pointer is over a control.


Ex.

 

int FMouseX,FMouseY;

TForm form;

TButton btnOk;

void btnOkMouseMove(TObject Sender, TMouseButton Button, TShiftState Shift, int X, int Y)

{

  // Do some work here

  FMouseX = X;

  FMouseY = Y;

}

 

{

  form = new TForm(nil);

 

  btnOk = TButton.Create(form);

  btnOk.Parent = form;

  btnOk.OnMouseMove = &btnOkMouseMove;

}


 

OnMouseUp 

Occurs when the user releases a mouse button that was pressed with the mouse pointer over a component.

 

Ex.

 

TForm form;

TButton btnOk;

void btnOkMouseUp(TObject Sender, TMouseButton Button, TShiftState Shift, int X, int Y)

{

  // Do some work here

}

 

{

  form = new TForm(nil);

 

  btnOk = TButton.Create(form);

  btnOk.Parent = form;

  btnOk.OnMouseUp = &btnOkMouseUp;

}


 


 


 


 


 


 


 

 

 

 

Copyright © 2022 Rickard Johansson