如何在Web浏览器控件中选择文本框?

时间:2017-03-03 09:01:47

标签: c# .net windows forms

当我按下F1时,我制作了一个打开Goog​​le翻译窗口的程序,程序和源代码在这里:

sites.google.com/site/windowsprogramfortranslation/

我想要"来源"文本框被选中(专注于)

我试过了:

            formMain.Activate();

            formMain.panelMain.Enabled = false;
            formMain.panelMain.Focus();
            formMain.panelMain.Select();
            formMain.panelMain.Enabled = true;

           formMain.webBrowserMain.BringToFront();


            formMain.webBrowserMain.Select();
            formMain.webBrowserMain.Focus();

            formMain.ActiveControl = formMain.webBrowserMain;


 if (formMain. WindowState != FormWindowState.Minimized)
                {
                    Program.DoMouseClick((uint)formMain.PointToScreen(formMain. webBrowserMain.Location).X + 10, (uint)formMain.PointToScreen(formMain.webBrowserMain.Location).Y + 10);
                }




            HtmlElement textArea = formMain.webBrowserMain.Document.GetElementById("source");

            if (textArea != null)
            {
                textArea.Focus();
            }

但它有时只会被选中!

1 个答案:

答案 0 :(得分:0)

试试这段代码: 这包括在站点中注入自定义J并调用该函数,在类似情况下适用于我(在完成渲染/加载后始终在网页上执行操作):

private void webBrowser1_DocumentCompleted(object sender, 
         WebBrowserDocumentCompletedEventArgs e)
            {
                HtmlElement head = webBrowser1.Document.GetElementsByTagName("head")[0];
                HtmlElement scriptEl = webBrowser1.Document.CreateElement("script");
                IHTMLScriptElement element = (IHTMLScriptElement)scriptEl.DomElement;
                element.text = "function trig_focus() { document.getElementById(\"theIdOfInputBox\").focus(); }";
                head.AppendChild(scriptEl);
                webBrowser1.Document.InvokeScript("trig_focus");
            }