Gxui. Get text from TextBox

I try to work with the GUI in Go. Chose this library. Want at the touch of a button to process and display information entered into the list of the textbox in the file. The problem is that I cannot access the textBox from outside creator function. Here’s what I did on the basis of examples:

package main

import (
	"fmt"
	"github.com/google/gxui"
	"github.com/google/gxui/drivers/gl"
	"github.com/google/gxui/math"
	"github.com/google/gxui/samples/flags"
)

type customAdapter struct {
	gxui.AdapterBase
}

func (a *customAdapter) Count() int {
	return 20
}

func (a *customAdapter) ItemAt(index int) gxui.AdapterItem {
	return index
}

func (a *customAdapter) ItemIndex(item gxui.AdapterItem) int {
	return item.(int)
}

func (a *customAdapter) Size(theme gxui.Theme) math.Size {
	return math.Size{W: 150, H: 30}
}

func (a *customAdapter) Create(theme gxui.Theme, index int) gxui.Control {

	layout1 := theme.CreateLinearLayout()
	layout1.SetDirection(gxui.LeftToRight)
	cell := theme.CreateTextBox()
	layout1.AddChild(cell)
	return layout1
}

func appMain(driver gxui.Driver) {
	theme := flags.CreateTheme(driver)
	adapter := &customAdapter{}
	layout := theme.CreateLinearLayout()
	layout.SetDirection(gxui.LeftToRight)

	list := theme.CreateList()
	list.SetAdapter(adapter)
	list.SetOrientation(gxui.Vertical)
	layout.AddChild(list)
	button2 := theme.CreateButton()
	button2.SetText("Show")
	button2.OnClick(func(gxui.MouseEvent) { my_func(list) })
	layout.AddChild(button2)
	window := theme.CreateWindow(250, 250, "Test")
	window.SetScale(flags.DefaultScaleFactor)
	window.AddChild(layout)
	window.OnClose(driver.Terminate)
}

func main() {
	gl.StartDriver(appMain)
}

func my_func(list gxui.List) {
	fmt.Println(list.Adapter().Count())
	Size := list.Adapter().Count()
	for i := 0; i < Size; i++ {
		el := list.Adapter().ItemAt(i)
		// do_something(el)
		fmt.Println(el)
	}
}

My questions:

  1. How do I get a text from textbox?
  2. How to retrieve an item from this list using ItemAt
  3. Other ways to solve the problem

I would be grateful for any help :slight_smile:

I found answer on first from my questions:

package main

import (
	"fmt"
	"github.com/google/gxui"
	"github.com/google/gxui/drivers/gl"
	"github.com/google/gxui/math"
	"github.com/google/gxui/samples/flags"
)

var Size = 20

type customAdapter struct {
	gxui.AdapterBase
	TextBoxs []gxui.TextBox
}

func (a *customAdapter) Count() int {
	return Size
}

func (a *customAdapter) ItemAt(index int) gxui.AdapterItem {
	return index
}

func (a *customAdapter) ItemIndex(item gxui.AdapterItem) int {
	return item.(int)
}

func (a *customAdapter) Size(theme gxui.Theme) math.Size {
	return math.Size{W: 150, H: 30}
}

func (a *customAdapter) Create(theme gxui.Theme, index int) gxui.Control {

	layout1 := theme.CreateLinearLayout()
	layout1.SetDirection(gxui.LeftToRight)
	cell := theme.CreateTextBox()
	cell.SetText(fmt.Sprint(index))
	a.TextBoxs[index] = cell
	layout1.AddChild(cell)
	return layout1
}

func appMain(driver gxui.Driver) {
	theme := flags.CreateTheme(driver)
	TSlice := make([]gxui.TextBox, Size)
	adapter := &customAdapter{TextBoxs: TSlice}
	layout := theme.CreateLinearLayout()
	layout.SetDirection(gxui.LeftToRight)

	list := theme.CreateList()
	list.SetAdapter(adapter)
	list.SetOrientation(gxui.Vertical)
	layout.AddChild(list)
	button2 := theme.CreateButton()
	button2.SetText("Show")
	button2.OnClick(func(gxui.MouseEvent) { my_func(adapter) })
	layout.AddChild(button2)
	window := theme.CreateWindow(250, 250, "Test")
	window.SetScale(flags.DefaultScaleFactor)
	window.AddChild(layout)
	window.OnClose(driver.Terminate)
}

func main() {
	gl.StartDriver(appMain)
}

func my_func(a *customAdapter) {
	for i := 0; i < Size; i++ {
		temp := a.TextBoxs[i]
		if temp == nil {
			break
		}
		//do_something
	}
}

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.