Show / Hide Table of Contents

# Class Button

Button is a View that provides an item that invokes an System.Action when activated by the user.

##### Inheritance

System.Object
Responder
View
Button

##### Implements

System.Collections.IEnumerable

##### Inherited Members

View.Enter
View.Leave
View.MouseEnter
View.MouseLeave
View.MouseClick
View.Driver
View.Subviews
View.Id
View.IsCurrentTop
View.WantMousePositionReports
View.WantContinuousButtonPressed
View.Frame
View.GetEnumerator()
View.LayoutStyle
View.Bounds
View.X
View.Y
View.Width
View.Height
View.SuperView
View.SetNeedsDisplay()
View.SetNeedsDisplay(Rect)
View.ChildNeedsDisplay()
View.Add(View)
View.Add(View[])
View.RemoveAll()
View.Remove(View)
View.BringSubviewToFront(View)
View.SendSubviewToBack(View)
View.SendSubviewBackwards(View)
View.BringSubviewForward(View)
View.Clear()
View.Clear(Rect)
View.ScreenToView(Int32, Int32)
View.ClipToBounds()
View.SetClip(Rect)
View.DrawFrame(Rect, Int32, Boolean)
View.DrawHotString(ustring, Attribute, Attribute)
View.DrawHotString(ustring, Boolean, ColorScheme)
View.Move(Int32, Int32)
View.HasFocus
View.OnEnter()
View.OnLeave()
View.Focused
View.MostFocused
View.ColorScheme
View.AddRune(Int32, Int32, Rune)
View.ClearNeedsDisplay()
View.DrawContent
View.OnDrawContent(Rect)
View.SetFocus(View)
View.KeyPress
View.KeyDown
View.OnKeyDown(KeyEvent)
View.KeyUp
View.OnKeyUp(KeyEvent)
View.EnsureFocus()
View.FocusFirst()
View.FocusLast()
View.FocusPrev()
View.FocusNext()
View.LayoutComplete
View.LayoutSubviews()
View.ToString()
View.OnMouseEnter(MouseEvent)
View.OnMouseLeave(MouseEvent)
View.OnMouseEvent(MouseEvent)
Responder.CanFocus
System.Object.Equals(System.Object)
System.Object.Equals(System.Object, System.Object)
System.Object.GetHashCode()
System.Object.GetType()
System.Object.MemberwiseClone()
System.Object.ReferenceEquals(System.Object, System.Object)

###### Namespace: Terminal.Gui

###### Assembly: Terminal.Gui.dll

##### Syntax

    public class Button : View, IEnumerable

##### Remarks

Provides a button showing text invokes an System.Action when clicked on with a mouse or when the user presses SPACE, ENTER, or hotkey. The hotkey is specified by the first uppercase letter in the button.

When the button is configured as the default (IsDefault) and the user presses the ENTER key, if no other View processes the KeyEvent, the Button's System.Action will be invoked.

### Constructors

#### Button()

Initializes a new instance of Button using Computed layout.

##### Declaration

    public Button()

##### Remarks

The width of the Button is computed based on the text length. The height will always be 1.

#### Button(ustring, Boolean)

Initializes a new instance of Button using Computed layout.

##### Declaration

    public Button(ustring text, bool is_default = false)

##### Parameters

Type | Name | Description
--- | --- | ---
NStack.ustring | text | The button's text
System.Boolean | is_default | If `true`, a special decoration is used, and the user pressing the enter key in a Dialog will implicitly activate this button.

##### Remarks

The width of the Button is computed based on the text length. The height will always be 1.

#### Button(Int32, Int32, ustring)

Initializes a new instance of Button using Absolute layout, based on the given text

##### Declaration

    public Button(int x, int y, ustring text)

##### Parameters

Type | Name | Description
--- | --- | ---
System.Int32 | x | X position where the button will be shown.
System.Int32 | y | Y position where the button will be shown.
NStack.ustring | text | The button's text

##### Remarks

The width of the Button is computed based on the text length. The height will always be 1.

#### Button(Int32, Int32, ustring, Boolean)

Initializes a new instance of Button using Absolute layout, based on the given text.

##### Declaration

    public Button(int x, int y, ustring text, bool is_default)

##### Parameters

Type | Name | Description
--- | --- | ---
System.Int32 | x | X position where the button will be shown.
System.Int32 | y | Y position where the button will be shown.
NStack.ustring | text | The button's text
System.Boolean | is_default | If `true`, a special decoration is used, and the user pressing the enter key in a Dialog will implicitly activate this button.

##### Remarks

The width of the Button is computed based on the text length. The height will always be 1.

### Fields

#### Clicked

Clicked System.Action, raised when the button is clicked.

##### Declaration

    public Action Clicked

##### Field Value

Type | Description
--- | ---
System.Action |

##### Remarks

Client code can hook up to this event, it is raised when the button is activated either with the mouse or the keyboard.

### Properties

#### IsDefault

Gets or sets whether the Button is the default action to activate in a dialog.

##### Declaration

    public bool IsDefault { get; set; }

##### Property Value

Type | Description
--- | ---
System.Boolean | `true` if is default; otherwise, `false`.

#### Text

The text displayed by this Button.

##### Declaration

    public ustring Text { get; set; }

##### Property Value

Type | Description
--- | ---
NStack.ustring |

#### TextAlignment

Sets or gets the text alignment for the Button.

##### Declaration

    public TextAlignment TextAlignment { get; set; }

##### Property Value

Type | Description
--- | ---
TextAlignment |

### Methods

#### MouseEvent(MouseEvent)

Method invoked when a mouse event is generated

##### Declaration

    public override bool MouseEvent(MouseEvent me)

##### Parameters

Type | Name | Description
--- | --- | ---
MouseEvent | me |

##### Returns

Type | Description
--- | ---
System.Boolean | `true`, if the event was handled, `false` otherwise.

##### Overrides

Responder.MouseEvent(MouseEvent)

#### PositionCursor()

Positions the cursor in the right position based on the currently focused view in the chain.

##### Declaration

    public override void PositionCursor()

##### Overrides

View.PositionCursor()

#### ProcessColdKey(KeyEvent)

This method can be overwritten by views that want to provide accelerator functionality (Alt-key for example), but without interefering with normal ProcessKey behavior.

##### Declaration

    public override bool ProcessColdKey(KeyEvent kb)

##### Parameters

Type | Name | Description
--- | --- | ---
KeyEvent | kb |

##### Returns

Type | Description
--- | ---
System.Boolean |

##### Overrides

View.ProcessColdKey(KeyEvent)

##### Remarks

After keys are sent to the subviews on the current view, all the view are processed and the key is passed to the views to allow some of them to process the keystroke as a cold-key.

This functionality is used, for example, by default buttons to act on the enter key. Processing this as a hot-key would prevent non-default buttons from consuming the enter keypress when they have the focus.

#### ProcessHotKey(KeyEvent)

This method can be overwritten by view that want to provide accelerator functionality (Alt-key for example).

##### Declaration

    public override bool ProcessHotKey(KeyEvent kb)

##### Parameters

Type | Name | Description
--- | --- | ---
KeyEvent | kb |

##### Returns

Type | Description
--- | ---
System.Boolean |

##### Overrides

View.ProcessHotKey(KeyEvent)

##### Remarks

Before keys are sent to the subview on the current view, all the views are processed and the key is passed to the widgets to allow some of them to process the keystroke as a hot-key.

For example, if you implement a button that has a hotkey ok "o", you would catch the combination Alt-o here. If the event is caught, you must return true to stop the keystroke from being dispatched to other views.

#### ProcessKey(KeyEvent)

If the view is focused, gives the view a chance to process the keystroke.

##### Declaration

    public override bool ProcessKey(KeyEvent kb)

##### Parameters

Type | Name | Description
--- | --- | ---
KeyEvent | kb |

##### Returns

Type | Description
--- | ---
System.Boolean |

##### Overrides

View.ProcessKey(KeyEvent)

##### Remarks

Views can override this method if they are interested in processing the given keystroke. If they consume the keystroke, they must return true to stop the keystroke from being processed by other widgets or consumed by the widget engine. If they return false, the keystroke will be passed using the ProcessColdKey method to other views to process.

The View implementation does nothing but return false, so it is not necessary to call base.ProcessKey if you derive directly from View, but you should if you derive other View subclasses.

#### Redraw(Rect)

Redraws this view and its subviews; only redraws the views that have been flagged for a re-display.

##### Declaration

    public override void Redraw(Rect bounds)

##### Parameters

Type | Name | Description
--- | --- | ---
Rect | bounds | The bounds (view-relative region) to redraw.

##### Overrides

View.Redraw(Rect)

##### Remarks

Always use Bounds (view-relative) when calling Redraw(Rect), NOT Frame (superview-relative).

Views should set the color that they want to use on entry, as otherwise this will inherit the last color that was set globaly on the driver.

Overrides of Redraw(Rect) must ensure they do not set `Driver.Clip` to a clip region larger than the `region` parameter.

### Implements

System.Collections.IEnumerable