Imports System.ComponentModel
Imports System.Drawing.Design
Imports System.Windows.Forms.Design
Public Class MultiLineString
' this is the custom type we'll want to edit in the propertygrid
' it's just a class with 1 property, a string, but we want a multiline editor for it
Private mString As String
Private mS As String
Public Sub New()
End Sub
Public Sub New(ByVal s As String)
mString = s
End Sub
Public Property Value() As String
Get
Return mString
End Get
Set(ByVal Value As String)
mString = Value
End Set
End Property
Public Overrides Function ToString() As String
Return IIf(mString = "", "", "<...>").ToString
End Function
End Class
Public Class MultiLineStringConverter
Inherits System.ComponentModel.TypeConverter
Public Overloads Overrides Function CanConvertFrom(ByVal context As System.ComponentModel.ITypeDescriptorContext, ByVal sourceType As System.Type) As Boolean
' if you return true here (if sourceType Is GetType(String) Then Return True),
' users will be able to edit the multiline property in the
' propertygrid without using the editor
Return MyBase.CanConvertFrom(context, sourceType)
End Function
Public Overloads Overrides Function CanConvertTo(ByVal context As System.ComponentModel.ITypeDescriptorContext, ByVal destinationType As System.Type) As Boolean
If destinationType Is GetType(String) Then Return True
Return MyBase.CanConvertFrom(context, destinationType)
End Function
Public Overloads Overrides Function ConvertFrom(ByVal context As System.ComponentModel.ITypeDescriptorContext, ByVal culture As System.Globalization.CultureInfo, ByVal value As Object) As Object
If TypeOf value Is String Then
Try
Return New MultiLineString(value.ToString)
Catch
Throw New InvalidCastException(value.ToString)
End Try
End If
Return MyBase.ConvertFrom(context, culture, value)
End Function
Public Overloads Overrides Function ConvertTo(ByVal context As System.ComponentModel.ITypeDescriptorContext, ByVal culture As System.Globalization.CultureInfo, ByVal value As Object, ByVal destinationType As System.Type) As Object
If (destinationType.Equals(GetType(String))) Then
Return value.ToString()
Else
Return MyBase.ConvertTo(context, culture, value, destinationType)
End If
End Function
End Class
Public Class MultiLineStringEditor
Inherits UITypeEditor
Private service As IWindowsFormsEditorService
Public Overloads Overrides Function EditValue(ByVal context As ITypeDescriptorContext, ByVal provider As IServiceProvider, ByVal value As Object) As Object
If (Not context Is Nothing And Not context.Instance Is Nothing And Not provider Is Nothing) Then
service = CType(provider.GetService(GetType(IWindowsFormsEditorService)), IWindowsFormsEditorService)
If (Not service Is Nothing) Then
' the Edit method gets the value ByRef,
' and changes it to the new value when user clicks ok
' I don't know why ... but it took me ages to get this part of the project working
' I didn't find how i could change the property in the propertybag ...
' until i found out that i just needed to change "value" to the new version
fEditorLongString.Edit(CType(value, MultiLineString), "Edit " & context.PropertyDescriptor.Name)
End If
End If
Return value
End Function
Public Overloads Overrides Function GetEditStyle(ByVal context As ITypeDescriptorContext) As UITypeEditorEditStyle
If (Not context Is Nothing And Not context.Instance Is Nothing) Then
' we'll show a modal form
Return UITypeEditorEditStyle.Modal
End If
Return MyBase.GetEditStyle(context)
End Function
End Class