Problem with MSHTML.

Joined
Jun 23, 2006
Messages
14
Programming Experience
1-3
Hi.
I have been given this code, which should give the properties of a HTML Element, but it WILL NOT WORK for me! Try it. Create a new form, with a webbrowser & a propertygrid, and here is the complete code:
Imports System.ComponentModel

VB.NET:
Public Class Form1 

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 
        Me.WebBrowser1.Navigate("[URL="http://www.vbcity.net%22/"]http://www.vbcity.net"[/URL]) 
    End Sub 

    Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted 
        ' get body element... 
        Dim clsElement As HtmlElement = WebBrowser1.Document.GetElementsByTagName("body")(0) 
        Me.PropertyGrid1.SelectedObject = New HTMLElementWrapper(clsElement) 
    End Sub 

    Private Sub PropertyGrid1_PropertyValueChanged(ByVal s As System.Object, ByVal e As System.Windows.Forms.PropertyValueChangedEventArgs) Handles PropertyGrid1.PropertyValueChanged 
        CType(PropertyGrid1.SelectedObject, HTMLElementWrapper).SetAttributeValue(e.ChangedItem.Label, e.ChangedItem.Value.ToString) 
    End Sub 

End Class 

Public Class HTMLElementWrapperProperties 
    Inherits System.ComponentModel.PropertyDescriptorCollection 

    Public Sub New() 
        MyBase.New(Nothing) 
    End Sub 

End Class 

Public Class HTMLElementWrapperPropertyDescriptor 
    Inherits System.ComponentModel.PropertyDescriptor 

    Friend Sub New(ByVal Name As String, ByVal Attributes() As System.Attribute) 
        MyBase.New(Name, Attributes) 
    End Sub 

    Public Overrides Function CanResetValue(ByVal component As Object) As Boolean 
        Return False 
    End Function 

    Public Overrides ReadOnly Property ComponentType() As System.Type 
        Get 
            Return GetType(HTMLElementWrapper) 
        End Get 
    End Property 

    Public Overrides Function GetValue(ByVal component As Object) As Object 
        Return CType(component, HTMLElementWrapper).GetAttributeValue(Me.Name) 
    End Function 

    Public Overrides ReadOnly Property IsReadOnly() As Boolean 
        Get 
            Return False 
        End Get 
    End Property 

    Public Overrides ReadOnly Property PropertyType() As System.Type 
        Get 
            Return GetType(System.String) 
        End Get 
    End Property 

    Public Overrides Sub ResetValue(ByVal component As Object) 
        CType(component, HTMLElementWrapper).SetAttributeValue(Me.Name, "") 
    End Sub 

    Public Overrides Sub SetValue(ByVal component As Object, ByVal value As Object) 
        CType(component, HTMLElementWrapper).SetAttributeValue(Me.Name, value.ToString) 
    End Sub 

    Public Overrides Function ShouldSerializeValue(ByVal component As Object) As Boolean 
        Return True 
    End Function 

End Class 

<TypeConverter(GetType(HTMLElementWrapperConverter))> _ 
Public Class HTMLElementWrapper 

    Private m_clsUnderlyingValue As Object 

    Public Sub New(ByVal UnderlyingValue As Object) 
        m_clsUnderlyingValue = UnderlyingValue 
    End Sub 

    Public Function GetAttributes() As HTMLElementWrapperProperties 

        Dim clsRet As New HTMLElementWrapperProperties 

        For Each clsProperty As System.Reflection.PropertyInfo In _ 
            CType(m_clsUnderlyingValue, HtmlElement).DomElement.GetType().GetProperties( _ 
                System.Reflection.BindingFlags.Public Or _ 
                System.Reflection.BindingFlags.Instance Or _ 
                System.Reflection.BindingFlags.Static) 

            If Not clsProperty.Name.Contains("_") _ 
                And Not Me.GetAttributeValue(clsProperty.Name).Contains("ComObject") _ 
                And Not Me.GetAttributeValue(clsProperty.Name).StartsWith("mshtml.") Then 

                clsRet.Add(New HTMLElementWrapperPropertyDescriptor(clsProperty.Name, Nothing)) 

            End If 

        Next 

        Return clsRet 

    End Function 

    Public Sub SetAttributeValue(ByVal Name As String, ByVal Value As String) %BR>
            If Not clsProperty.Name.Contains("_") _ 
                And Not Me.GetAttributeValue(clsProperty.Name).Contains("ComObject") _ 
         &nbssp;  End Function 

End Class 

Public Class HTMLElementWrapperConverter : Inherits System.ComponentModel.ExpandableObjectConverter 

    Public Overloads Overrides Function GetPropertiesSupported(ByVal context As System.ComponentModel.ITypeDescriptorContext) As Boolean 
        Return True 
    End Function 

    Public Overloads Overrides Function GetProperties(ByVal context As System.ComponentModel.ITypeDescriptorContext, ByVal value As Object, ByVal attributes As Attribute()) As System.ComponentModel.PropertyDescriptorCollection 
        Return CType(value, HTMLElementWrapper).GetAttributes() 
    End Function 

End Class
 
That is some quite nice code, but you copied the methods SetAttributeValue and GetAttributeValue from HTMLElementWrapper class wrong, try to copy it again from the source: http://vbcity.com/forums/topic.asp?tid=137277
You also missed to import the System.ComponentModel namespace (or didn't post it here)
 
Thanks!
I tried that, and the person who gave me this code even gave me his entire project via email. But it works on his computer, not mine. if you read the last page or so of that vbCity thread you will see that no conclusion was reached on why this is happening. I was hoping that with the combined efforts of me & the members of 2 programming forums I could solve this once and for all. it is the one thing holding me back from adding a few REALLY useful features to a HTML Editor I am creating.
 
I noticed that, but I don't understand why you are discussing MSHTML reference since it is not used anywhere in that code? The underlying code of .Net 2.0 Webbrowser control is using mshtml, but this code is not using a direct reference to that library.
 
Actually you're right - I'll re-read that post to refresh my memory & get back to you. But for now, can you work out any reason why the code at that vbCity thread is not working for me? If the WebBrowser control uses MSHTML, and I am trying to get the properties of a WebBrowser element, surely it is a MSHTML problem?
Basically, all want is a complete list of all properties, methods & events for any HTML element, including ones specific to that element.
 
It does sound Mshtml related, but not related to if you make a reference to it or not, and not to any interop that may create. But if it was Mshtml related then you should have other problems with also the .Net 2.0 WebBrowser and also general use of Internet Explorer, the app where this library belongs. If you want to compare versions I ran this with Express, Mshtml (4.0) file version is 7.00.6000.16414 installed by IE 7.0 English.
 
Back
Top