简体   繁体   中英

Switch focus from one control to another

After I click a label in my treeview, I want the focus to switch to my web browser so the user can scroll up and down without having to click the browser first. This is the code I have right now, but scrolling still scrolls the treeview:

if (e.Node.Text == "Sales Screen")
{
    var txt = Properties.Resources.SalesScreen;
    webBrowser1.DocumentText = txt;
    this.ActiveControl = webBrowser1;
}

Whats the proper way to do this? Thanks!

Edit: now using this, still not working:

private void treeView1_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
    {
        if (e.Node.Text == "Sales Screen")
        {
            var txt = Properties.Resources.SalesScreen;
            webBrowser1.DocumentText = txt;
            this.BeginInvoke(new Action(() => webBrowser1.Focus()));
        }

The TreeView event you use matters a great deal, it isn't clear which one you use. TV does not like losing the focus when it fires its events, it will move it back by itself.

Only use the AfterSelect event to execute this code.


Yes, the NodeMouseDoubleClick event has this problem. The workaround is to delay the action of the event handler until after the TV has processed the event. Elegantly one with the Control.BeginInvoke() method. Like this:

    private void treeView1_MouseDoubleClick(object sender, MouseEventArgs e) {
        this.BeginInvoke(new Action(() => webBrowser1.Focus()));
    }
if (e.Node.Text == "Sales Screen")
{
    var txt = Properties.Resources.SalesScreen;
    webBrowser1.DocumentText = txt;
    webBrowser1.Focus();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM