[GH-ISSUE #765] Differentiate FormItems by type #559

Closed
opened 2026-03-04 01:06:01 +03:00 by kerem · 2 comments
Owner

Originally created by @drsag-aha on GitHub (Oct 27, 2022).
Original GitHub issue: https://github.com/rivo/tview/issues/765

Hi,

I have a form with dropdowns and inputfields and want automatically write a config file out of the inputs.

With form.GetFormItem(1).(*tview.DropDown).GetCurrentOption() I get the current option of my dropdown.

My Problem is to differntiate between the FormItems to get the content of them.
How can I manage to do something like in the pseudocode?

Pseudocode:

for _, formitem =: range form { 

       if formitem.getType() == "dropdown" { 

                contentToWrite := formitem.(*tview.InputField).GetText()

       } else {

                contentToWrite := formitem.(*tview.DropDown).GetCurrentOption()

      }
}
Originally created by @drsag-aha on GitHub (Oct 27, 2022). Original GitHub issue: https://github.com/rivo/tview/issues/765 Hi, I have a form with dropdowns and inputfields and want automatically write a config file out of the inputs. With ```form.GetFormItem(1).(*tview.DropDown).GetCurrentOption()``` I get the current option of my dropdown. My Problem is to differntiate between the FormItems to get the content of them. How can I manage to do something like in the pseudocode? Pseudocode: ``` for _, formitem =: range form { if formitem.getType() == "dropdown" { contentToWrite := formitem.(*tview.InputField).GetText() } else { contentToWrite := formitem.(*tview.DropDown).GetCurrentOption() } } ```
kerem closed this issue 2026-03-04 01:06:01 +03:00
Author
Owner

@drsag-aha commented on GitHub (Oct 28, 2022):

Found the solution by using the reflect package.

reflect.TypeOf(form.GetFormItem(i))

<!-- gh-comment-id:1294922617 --> @drsag-aha commented on GitHub (Oct 28, 2022): Found the solution by using the reflect package. ```reflect.TypeOf(form.GetFormItem(i))```
Author
Owner

@rivo commented on GitHub (Oct 28, 2022):

You can also use type switches. That's probably faster than using reflect.

<!-- gh-comment-id:1294993035 --> @rivo commented on GitHub (Oct 28, 2022): You can also use [type switches](https://go.dev/tour/methods/16). That's probably faster than using `reflect`.
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#559
No description provided.