1

I have urls in a listbox. I am trying to navigate to a url when it is selected.

private void lstURL_SelectedIndexChanged(object sender, EventArgs e)
{
    wbrBrowser.Navigate(lstURL.Text);
    lblUrl.Text = lstURL.Text;
    lblTitle.Text = "Loading...";

    System.Windows.Forms.HtmlDocument document = wbrBrowser.Document;
    document.MouseUp += new HtmlElementEventHandler(this.htmlDocument_Click);
}

private void wbrBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    lblTitle.Text = wbrBrowser.Document.Title;
}

private void htmlDocument_Click(object sender, HtmlElementEventArgs e)
{
    HtmlElement element = this.wbrBrowser.Document.GetElementFromPoint(e.ClientMousePosition);

    var savedId = element.Id;
    var uniqueId = Guid.NewGuid().ToString();
    element.Id = uniqueId;

    var doc = new HtmlAgilityPack.HtmlDocument();
    doc.LoadHtml(element.Document.GetElementsByTagName("html")[0].OuterHtml);
    element.Id = savedId;

    var node = doc.GetElementbyId(uniqueId);
    var xpath = node.XPath;

    lblXpath.Text = xpath;
}

It works the first time I load a page, after that it just freezes and lblTitle.Text just stays at "Loading..."

I have been searching for a while but I can't figure out why this is happening.

SAm
  • 2,154
  • 28
  • 28
Katp00p
  • 93
  • 12
  • Does it fire the DocumentCompleted event? – Shaharyar Aug 31 '14 at 06:45
  • Not the 2nd time. I put a messagebox as the first line in the method, but it doesnt fire after the 2nd attempt. – Katp00p Aug 31 '14 at 06:46
  • and the SelectedIndexChanged event fires every time you click on it? – Shaharyar Aug 31 '14 at 06:55
  • I just tested it, and it fires every time I click on it. – Katp00p Aug 31 '14 at 06:57
  • Have a loot at this problem it looks similar to yours: http://stackoverflow.com/questions/10981206/webbrowser-control-causes-whole-application-to-become-unresponsive – Shaharyar Aug 31 '14 at 07:01
  • I have a custom event in there as well... ive edited to reflect in the code. Maybe that is causing it, but I am unsure of how to turn it off. – Katp00p Aug 31 '14 at 07:09
  • The MouseUp event assignment is placed wrong, it must be done in the DocumentCompleted event handler. Only then do you have a stable Document object. Using lstURL.Text is wrong, you must use the lstURL.SelectedIndex property. – Hans Passant Aug 31 '14 at 10:38

0 Answers0