Skip to content

XButton

Below is the user‑facing API for the XButton plugin class, including its properties, methods, and event handlers.


Class: XButton

Create a button control bound to a native window.

Dim btn As New XButton

Properties

All properties are thread‑safe and immediately update the native control once created.

Name Type Description Example
Left Integer X‑position (pixels) btn.Left = 50
Top Integer Y‑position (pixels) btn.Top = 20
Width Integer Width (pixels) btn.Width = 100
Height Integer Height (pixels) btn.Height = 30
Parent Integer Handle of parent window (from XWindow.GetHandle) btn.Parent = win.Handle
Caption String Button text btn.Caption = "Click Me!"
HasBorder Boolean Show a border around the button btn.HasBorder = True
Bold Boolean Use bold font btn.Bold = True
Underline Boolean Underline the caption btn.Underline = True
Italic Boolean Use italic font btn.Italic = True
FontName String Name of the font face btn.FontName = "Arial"
FontSize Integer Font size in points btn.FontSize = 12
Enabled Boolean Enable or disable user interaction btn.Enabled = False
Visible Boolean Show or hide the control btn.Visible = True

Methods

Open()

Creates the native control under its parent. Must set Parent first. Returns True on success, False otherwise.

If Not btn.Open() Then
  Print "Failed to create XButton!"
End If

Close()

Destroy the control and free resources.

btn.Close()

Invalidate()

Repaints the control.

Events

Pressed

Fired when the user clicks the button (mouse up inside).

How to hook: use the built‑in AddressOf and AddHandler

Sub OnBtnPressed()
  Print "Button clicked!"
End Sub

Dim btn As New XButton
btn.Parent = win.Handle
btn.Left   = 10
btn.Top    = 10
btn.Caption = "Press Me"
If btn.Open() Then
  ' Hook the Pressed event
  AddHandler(btn.Pressed, AddressOf(OnBtnPressed))
End If

Full Example

Sub Main()
  Dim win As New XWindow
  win.Title = "Demo"
  win.Open

  Dim btn As New XButton
  btn.Parent  = win.Handle
  btn.Left    = 20
  btn.Top     = 20
  btn.Width   = 120
  btn.Height  = 30
  btn.Caption = "Click Here"
  btn.Bold    = True

  If Not btn.Open() Then
    Print "Could not create button."
    Return
  End If

  ' Event handler
  Sub OnBtnPressed()
    Print "You pressed the button!"
  End Sub

  AddHandler(btn.Pressed, AddressOf(OnBtnPressed))

  ' Keep the window alive
  While True
    DoEvents()
    Sleep(1)
  Wend

  btn.Close()
  win.Close()
End Sub

With these properties, methods, and the Pressed event, you have full control over creating and responding to button clicks in your CrossBasic applications.