From 384f577a620b9bf9fb08fee23a7733da9f9125fc Mon Sep 17 00:00:00 2001 From: Oliver Date: Sun, 2 Jun 2019 21:31:59 +0200 Subject: [PATCH] Added SetFocus() to Form which changes the focus to a specific form element. Resolves #286 --- form.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/form.go b/form.go index 332c721..cbf3c54 100644 --- a/form.go +++ b/form.go @@ -154,6 +154,20 @@ func (f *Form) SetButtonTextColor(color tcell.Color) *Form { return f } +// SetFocus shifts the focus to the form element with the given index, counting +// non-button items first and buttons last. Note that this index is only used +// when the form itself receives focus. +func (f *Form) SetFocus(index int) *Form { + if index < 0 { + f.focusedElement = 0 + } else if index >= len(f.items)+len(f.buttons) { + f.focusedElement = len(f.items) + len(f.buttons) + } else { + f.focusedElement = index + } + return f +} + // AddInputField adds an input field to the form. It has a label, an optional // initial value, a field width (a value of 0 extends it as far as possible), // an optional accept function to validate the item's value (set to nil to