Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Beginner Question Regarding Checkbox #885

Open
daryl-williams opened this issue Sep 11, 2023 · 2 comments
Open

Beginner Question Regarding Checkbox #885

daryl-williams opened this issue Sep 11, 2023 · 2 comments

Comments

@daryl-williams
Copy link

daryl-williams commented Sep 11, 2023

I'm not sure if this is the right place to ask a newbie question, if there is a better place please let me know. I have a form with a number of checkboxes and I want to be able to set them all to checked when a button is clicked. I have some code as follows:

button := testForm.AddButton("Select all", func() {
    for i:=0; i<testForm.GetFormItemCount(); i++ {
      item := testForm.GetFormItem(i)
      //item.checked = true
      fmt.Printf("1. Item Type    = %T\n", item)
      fmt.Printf("2. Item Type    = %T\n", &item)
    }
}

I would like to be able to programmatically check each checkbox item. I have tried using item.SetChecked() but I am told:

item.SetChecked undefined (type tview.FormItem has no field or method SetChecked)

When I print out the item type of item I get *tview.CheckBox and when I print the type of &item I get *tview.FormItem and when I print the value (using %v) I get what appears to be a struct with 12 fields which is where I think the value I want to set resides.

Any help clearing this up would be appreciated, I have searched online and perused the demo code trying to find examples of how to work with checkboxes without much luck.

Regards,

Daryl

@digitallyserviced
Copy link
Contributor

digitallyserviced commented Sep 11, 2023

@daryl-williams

Thi is something that is a basic Go concept. You have to cast it to the interface/type that you want to invoke a method for.

FormItem is an interface type that the Checkboxes, TextView, Radio, Buttons, etc... all implement, so you just have to coerce back to the original Primitive/Widget *tview.Checkbox. Here is a demo that does it.

_, title := form.GetFormItem(0).(*tview.DropDown).GetCurrentOption()

This should do it

button := testForm.AddButton("Select all", func() {
    for i:=0; i<testForm.GetFormItemCount(); i++ {
      // your good old standard Go cast of a type, with a guard in there to make sure this item
      // will work
      if item, ok := testForm.GetFormItem(i).(*tview.Checkbox); ok {
        item.SetChecked(true)
      }
      
      fmt.Printf("1. Item Type    = %T\n", item)
      fmt.Printf("2. Item Type    = %T\n", &item)
    }
}

@daryl-williams
Copy link
Author

@digitallyserviced

Thanks for your reply, I appreciate the explanation and the example code, it really helped me to understand the Go language better and helped me get that code working.

Regards

Daryl

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants