如何使 WebBrowser 控件导航到特定地址?

发布于 2024-10-09 07:10:49 字数 1081 浏览 0 评论 0 原文

运行代码时如何制作代码,它转到 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();
                }
            }
        }
    }              
}

How can I make the code when run the code it go to example.com

private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)
{
    webBrowser1.Navigate("www.example.com");
}

Please correct it when run program it go to 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

街角卖回忆 2024-10-16 07:10:49

您可以在 Form.Load 事件中导航到 example.com 吗?它在我的机器上运行良好。

private void Form1_Load(object sender, EventArgs e)
{
    webBrowser1.Navigate("www.example.com"); 
}

Can you Navigate to example.com at Form.Load event? It's working fine in my machine.

private void Form1_Load(object sender, EventArgs e)
{
    webBrowser1.Navigate("www.example.com"); 
}
倦话 2024-10-16 07:10:49

您需要处理加载WebBrowser 控件在您的表单显示在屏幕上时自动导航到 www.example.com,则您的表单 (Form1) 的 code> 事件

正如现在所写,您处理 WebBrowser 控件的 Navigated 事件,并告诉它导航到其他地方。但是,仅当浏览器导航到并开始加载新页面时才会引发 Naviated 事件。即使你的代码能够工作,它也会永远追着自己的尾巴。

相反,请尝试以下操作:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        webBrowser1.Navigate("http://www.example.com"); 
    }
}

You need to handle the Load event of your form (Form1) if you want the WebBrowser 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 the WebBrowser control and tell it to navigate somewhere else. However, the Navigated 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:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        webBrowser1.Navigate("http://www.example.com"); 
    }
}
烟花肆意 2024-10-16 07:10:49
public Form1()
{
    InitializeComponent();
    webBrowser1.Navigate("http://www.example.com"); 
}

这会在应用程序初始化后执行导航方法。

public Form1()
{
    InitializeComponent();
    webBrowser1.Navigate("http://www.example.com"); 
}

This execute the navigate method after the app is initialized.

木有鱼丸 2024-10-16 07:10:49

我不确定我是否理解您的问题:webBrowser1_DocumentCompleted 方法中的 e 变量包含保存当前 Url 属性="http://msdn.microsoft.com/en-us/library/system.uri.aspx" rel="nofollow">Uri 对象,其中包含浏览器控件到达的 URL:

private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    WebBrowser browser = (WebBrowser)sender;
    if (e.Url.Host.EndsWith("example.com"))
    {
        // do something
    }
}

I'm not sure if I understand your question: The e variable in the webBrowser1_DocumentCompleted method contains the Url property that holds the current Uri object with the URL where the browser control has arrived:

private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    WebBrowser browser = (WebBrowser)sender;
    if (e.Url.Host.EndsWith("example.com"))
    {
        // do something
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文