TabControl 切换 内嵌页面直接滚动
事件背景:系统是个winfrom应用程序,程序只有一个主界面。界面中一个TabControl,每个TabPage其实是一个WebBrowser。
存在的问题:TabControl选择项切换时,每次都要在页面上点击下,才能滚动。
修复后的效果:不点击直接可以用鼠标滚动页面。
思考经过:
我首先想到是在页面上获取首个元素,直接激活或者设置焦点。结果没有反映。
之后我想模拟下点击事件,使用InvokeMember实现body的点击事件后,发现时灵时不灵。
最终在尝试性的设置body焦点竟然成功了!现在将代码记录如下:
1 private void tabMain_SelectedIndexChanged(object sender, EventArgs e)
2 {
3 Control [] controls = tabMain.SelectedTab.Controls.Find("WB_Page", false);
4 foreach (Control control in controls)
5 {
6 var webBrowser = control as WebBrowser;
7 if (webBrowser != null && webBrowser.Document != null)
8 {
9 HtmlElementCollection elements = webBrowser.Document.GetElementsByTagName("body");
10 if (elements != null && elements.Count > 0)
11 {
12 elements[0].Focus();
13 break;
14 }
15 }
16 }
17 }
2 {
3 Control [] controls = tabMain.SelectedTab.Controls.Find("WB_Page", false);
4 foreach (Control control in controls)
5 {
6 var webBrowser = control as WebBrowser;
7 if (webBrowser != null && webBrowser.Document != null)
8 {
9 HtmlElementCollection elements = webBrowser.Document.GetElementsByTagName("body");
10 if (elements != null && elements.Count > 0)
11 {
12 elements[0].Focus();
13 break;
14 }
15 }
16 }
17 }