As a quick fix i ended up using the MouseUp event of an extended WebBrowser control to get the cursor position. I used this to get the Element at the current clicked/selected text in the WebBrowser control.
Dim ScreenCoord As New Point(MousePosition.X, MousePosition.Y)
Dim BrowserCoord As Point = webBrowser1.PointToClient(ScreenCoord)
Dim elem As HtmlElement = webbrowser1.Document.GetElementFromPoint(BrowserCoord)
I used a helper function to get the index of the element at the cursor location.
Function getIndexforElement(elem As htmlElement, browser As webbrowser) As Integer
Dim page as mshtml.HTMLdocument
Dim Elements as mshtml.IHTMLElementCollection
Dim elemCount As Integer = 0
page = browser.document.Domdocument
elements = page.getElementsByTagName(elem.TagName)
For Each element As mshtml.IHTMLElement In elements
elemCount = elemCount + 1
If (elem.OffsetRectangle.Top = element.offsetTop) And (elem.OffsetRectangle.Left = element.offsetLeft) Then
Exit For
End If
Next
If elemCount > 0 Then
Dim matches as MatchCollection = regex.Matches(browser.DocumentText,"<" & elem.TagName,regexoptions.IgnoreCase Or regexoptions.Multiline)
Return matches(elemCount-1).Index + 1
Else
Return 0
End If
End Function
Having the index of the element a simple regular expression could be used to find the line number in the original html file.
Function getLineNumber(textIn As String, index As Integer) As Integer
textIn = textIn.Replace(VbCrLf,VbLf)
Dim line as Integer = regex.Matches(textIn.Substring(0,index),"\n",RegexOptions.Multiline).Count + 1
If line < 1 Then line = 1
Return line
End Function