如何使 WebBrowser 控件导航到特定地址?
运行代码时如何制作代码,它转到 example.com
private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)
{
webBrowser1.Navigate("www.example.com");
}
请在运行程序时更正它转到 example.com
private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)
{
webBrowser1.Navigate("www.example.com");
}
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
if (webBrowser1.Document != null)
{
IHTMLDocument2 document = webBrowser1.Document.DomDocument as IHTMLDocument2;
if (document != null)
{
IHTMLSelectionObject currentSelection = document.selection;
IHTMLTxtRange range = currentSelection.createRange() as IHTMLTxtRange;
if (range != null)
{
const String search = "ant";
if (range.findText(search, search.Length, 2))
{
range.select();
}
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以在
Form.Load
事件中导航到 example.com 吗?它在我的机器上运行良好。Can you Navigate to example.com at
Form.Load
event? It's working fine in my machine.您需要处理
加载WebBrowser
控件在您的表单显示在屏幕上时自动导航到 www.example.com,则您的表单 (Form1
) 的 code> 事件 。正如现在所写,您处理
事件,并告诉它导航到其他地方。但是,仅当浏览器导航到并开始加载新页面时才会引发
WebBrowser
控件的 NavigatedNaviated
事件。即使你的代码能够工作,它也会永远追着自己的尾巴。相反,请尝试以下操作:
You need to handle the
Load
event of your form (Form1
) if you want theWebBrowser
control to automatically navigate to www.example.com whenever your form is shown on the screen.As it's written now, you handle the
Navigated
event of theWebBrowser
control and tell it to navigate somewhere else. However, theNavigated
event is only raised when the browser has navigated to and begun loading a new page. Even if you get your code to work, it will be perpetually chasing its own tail.Instead, try the following:
这会在应用程序初始化后执行导航方法。
This execute the navigate method after the app is initialized.
我不确定我是否理解您的问题:
webBrowser1_DocumentCompleted
方法中的e
变量包含保存当前 Url 属性="http://msdn.microsoft.com/en-us/library/system.uri.aspx" rel="nofollow">Uri 对象,其中包含浏览器控件到达的 URL:I'm not sure if I understand your question: The
e
variable in thewebBrowser1_DocumentCompleted
method contains theUrl
property that holds the current Uri object with the URL where the browser control has arrived: