[GH-ISSUE #582] How to do button handling in forms? #426

Closed
opened 2026-03-04 01:04:52 +03:00 by kerem · 1 comment
Owner

Originally created by @kudoakiyama on GitHub (Mar 21, 2021).
Original GitHub issue: https://github.com/rivo/tview/issues/582

I have this code

form := tview.NewForm().
  AddInputField("Search", "", 20, nil, nil).
  AddButton("Go", func() {
    textView1.SetText(form.GetFormItem(0).GetText)
  }
./main.go:20:4: undefined: textView1
./main.go:20:16: undefined: form

but it gives out an error because it doesn't know where form is. I'm actually new to how Go works with methods anonymous functions so please bear with me here. Thank you.

Edit: added the error message.

Originally created by @kudoakiyama on GitHub (Mar 21, 2021). Original GitHub issue: https://github.com/rivo/tview/issues/582 I have this code ```go form := tview.NewForm(). AddInputField("Search", "", 20, nil, nil). AddButton("Go", func() { textView1.SetText(form.GetFormItem(0).GetText) } ``` ``` ./main.go:20:4: undefined: textView1 ./main.go:20:16: undefined: form ``` but it gives out an error because it doesn't know where `form` is. I'm actually new to how Go works with methods anonymous functions so please bear with me here. Thank you. Edit: added the error message.
kerem closed this issue 2026-03-04 01:04:52 +03:00
Author
Owner

@rivo commented on GitHub (Apr 26, 2021):

This is the way closures work in Go. The variable is not yet known to functions on the same line. (I think this could be changed. It bit me a couple of times in the past, too.)

You'll want to declare form first:

var form tview.Form
form = tview.NewForm()...
<!-- gh-comment-id:826831613 --> @rivo commented on GitHub (Apr 26, 2021): This is the way closures work in Go. The variable is not yet known to functions on the same line. (I think this could be changed. It bit me a couple of times in the past, too.) You'll want to declare `form` first: ```go var form tview.Form form = tview.NewForm()... ```
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/tview#426
No description provided.