lxn / walk

A Windows GUI toolkit for the Go Programming Language
Other
6.83k stars 884 forks source link

Setting focus to an element? #729

Closed Ghegs closed 3 years ago

Ghegs commented 3 years ago

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?

lxn commented 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.

Ghegs commented 3 years ago

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:      &currentText,
                        Text:          "Focus here",
                        StretchFactor: 2,
                    },
                },
            },
        },
    }.Create()); err != nil {
        fmt.Println(err)
    }

    mw.Run()

}

type MyMainWindow struct {
    *walk.MainWindow
}
lxn commented 3 years ago
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:      &currentText,
                        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
}
Ghegs commented 3 years ago

Ah, I see! Thank you very much, appreciate it.