Closed Ghegs closed 3 years ago
You will have to call the SetFocus
method on the desired widget. There is a Starting
event on MainWindow
and Dialog
, which you could handle to do that.
Sorry, I don't understand. TextEdit doesn't seem to have a SetFocus method. Can you show here:
package main
import (
"github.com/lxn/walk"
. "github.com/lxn/walk/declarative"
"fmt"
)
func main() {
var currentText *walk.TextEdit
mw := new(MyMainWindow)
if err := (MainWindow{
AssignTo: &mw.MainWindow,
Title: "Title",
Size: Size{200, 400},
MenuItems: []MenuItem{
Menu{
Text: "File",
Items: []MenuItem{
Action{
Text: "Open",
},
Action{
Text: "Exit",
OnTriggered: func() { mw.Close() },
},
},
},
Menu{
Text: "Help",
Items: []MenuItem{
Action{
Text: "About",
},
},
},
},
Layout: HBox{MarginsZero: true},
Children: []Widget{
HSplitter{
Children: []Widget{
ListBox{
StretchFactor: 1,
},
TextEdit{
AssignTo: ¤tText,
Text: "Focus here",
StretchFactor: 2,
},
},
},
},
}.Create()); err != nil {
fmt.Println(err)
}
mw.Run()
}
type MyMainWindow struct {
*walk.MainWindow
}
package main
import (
"github.com/lxn/walk"
. "github.com/lxn/walk/declarative"
"fmt"
)
func main() {
var currentText *walk.TextEdit
mw := new(MyMainWindow)
if err := (MainWindow{
AssignTo: &mw.MainWindow,
Title: "Title",
Size: Size{200, 400},
MenuItems: []MenuItem{
Menu{
Text: "File",
Items: []MenuItem{
Action{
Text: "Open",
},
Action{
Text: "Exit",
OnTriggered: func() { mw.Close() },
},
},
},
Menu{
Text: "Help",
Items: []MenuItem{
Action{
Text: "About",
},
},
},
},
Layout: HBox{MarginsZero: true},
Children: []Widget{
HSplitter{
Children: []Widget{
ListBox{
StretchFactor: 1,
},
TextEdit{
AssignTo: ¤tText,
Text: "Focus here",
StretchFactor: 2,
},
},
},
},
}.Create()); err != nil {
fmt.Println(err)
}
// Set focus in Starting event.
mw.Starting().Attach(func() {
currentText.SetFocus()
})
mw.Run()
}
type MyMainWindow struct {
*walk.MainWindow
}
Ah, I see! Thank you very much, appreciate it.
It seems the cursor focus is always set on the first element available. I'm trying to make a simple application with a ListBox on the left and a TextEdit on the right, which makes the cursor focus on the ListBox when the program starts, but I'd like it to be on the TextEdit. So is there a way to choose which element the focus goes to regardless of the element's place?