conor.odriscoll@o2.ie
Member
- 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
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