1

单击树视图中的标签后,我希望焦点切换到我的网络浏览器,以便用户可以上下滚动而无需先单击浏览器。这是我现在拥有的代码,但滚动仍然会滚动树视图:

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

这样做的正确方法是什么?谢谢!

编辑:现在使用这个,仍然无法正常工作:

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()));
        }
4

2 回答 2

3

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()));
    }
于 2013-11-05T19:38:04.870 回答
1
if (e.Node.Text == "Sales Screen")
{
    var txt = Properties.Resources.SalesScreen;
    webBrowser1.DocumentText = txt;
    webBrowser1.Focus();
}
于 2013-11-05T18:43:22.187 回答