Basically, (in my user control) I have a child class with properties and these will be expandable (like the Size property).
Now on the UC I have a second set of props which expose these props. would both sets of props need an ExpandableObjectConverter
or would the dummy UC props pass everything needed to the underlying class of expandable props?
Thanks
Edit: You can skip to post #36 since that is where the new part starts.
Last edited by RobDog888; Apr 6th, 2005 at 06:50 PM.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum.
You should only need to define the TypeConverter Once.
For example if you added a property of the type System.Drawing.Point to your user control (this is the same type as the Size Property). You dont need to do anything else because the TypeConverter was already made by M$ (System.Drawing.PointConverter)
--
Once a TypeConverter is defined you dont need to explicitly tell Visual Studio to use it.
Thanks, but would I pass a class from the dummy props defined in the UC to the converter or pass my
child class - gradientpanel? Also, these props are custom and not any default ones.
Here's an example of what I think you're trying to do, maybe you missed a step:
DrawStylesConverter:
VB Code:
Imports System.ComponentModel
Imports System.Globalization
' Expandable TypeConverter for the DrawStyles class
Public Class DrawStylesConverter
Inherits ExpandableObjectConverter
Private m_lastValue As DrawStyles = New DrawStyles
Public Overloads Overrides Function CanConvertFrom(ByVal context As ITypeDescriptorContext, ByVal t As Type) As Boolean
' Indicate which types this class can convert "From"
If t Is GetType(String) Then
Return True
End If
Return MyBase.CanConvertFrom(context, t)
End Function
Public Overloads Overrides Function ConvertFrom(ByVal context As ITypeDescriptorContext, ByVal culture As CultureInfo, ByVal value As Object) As Object
If value.GetType() Is GetType(String) Then
Try
' Attempt to parse the string representation
' into the constituent properties
Dim ds As New DrawStyles
Dim s As String = value
Dim c As String
Dim props As String() = s.Split(",".ToCharArray())
' Revert back to the last good value if an error is encountered
' trying to parse the entered "text" value for the constiuent properties
Return m_lastValue
End Try
End If
MyBase.ConvertFrom(context, culture, value)
End Function
Public Overloads Overrides Function CanConvertTo(ByVal context As ITypeDescriptorContext, ByVal t As Type) As Boolean
' Indicate which types this class can convert "To"
If t Is GetType(DrawStyles) Then Return True
Return MyBase.CanConvertTo(context, t)
End Function
Public Overloads Overrides Function ConvertTo(ByVal context As ITypeDescriptorContext, ByVal culture As CultureInfo, ByVal value As Object, ByVal destType As Type) As Object
If destType Is GetType(String) Then
' Attempt to convert the value into a string representation
Try
Dim styles As DrawStyles = CType(value, DrawStyles)
I think you got what I was trying to do exactly. I had all three properties on the control and in the child class.
Then I had the child class' converting the props to make them expandable.
Your demo looks great! Give me a few minutes to incorporate it into my uc for testing.
Thanks
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum.
If I take the Option Strict Off then it works (see bottom of message) but I would like
to get it converted correctly so I can leave it on.
I guess I mixed something up. It expands correctly finally but its attached to
my gradientpanel class which the properties are in.
So I get all the properties of a panel object and my two panelcolor1 and 2 props.
Cant I just have the assignment of the props and not have to use the class?
My issues are in the DrawStyles class (actually GradientPanel class).
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum.
Public Overloads Overrides Function ConvertFrom(ByVal context As ITypeDescriptorContext, ByVal culture As CultureInfo, ByVal value As Object) As Object
If value.GetType() Is GetType(String) Then
Try
' Attempt to parse the string representation
' into the constituent properties
Dim ds As New DrawStyles
Dim s As String = [B]CType(value, String)[/B]
Dim c As String
Dim props As String() = s.Split(",".ToCharArray())
I think part of the problem is the both DrawStyles and DrawMode areReserved enumerations and they are conflicting
with my property. Guess a naming change is in order.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum.
Defaults are not Resetting to the correct values. I have the default attributes on each of the
three properties but when I reset them it goes to Horizontal, 0, 0 and not ForwardDiagonal, White, Blue.
Do I need to supply something different as a default since the prop is expandable?
The gradient panel is not respondin at all to the property changes.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum.
Re: Expandable Properties - Almost Solved but still a few "issues"
Ok, now I get some kind of hex number for RGB colors?
"ForwardDiagonal, ff7ba2e7, ff6375d6"
I am trying to get the Reset right click context menu to function at the DrawStyles level just like it does
for the Font property. The Font property lets you reset all the child properties at the top level with one
click.
I have the GradientMode, PanelColor1, and PanelColor2 all with the DefaultValues attrib, so I dont know
what else to do to fix it?
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum.
Re: Expandable Properties - Almost Solved but still a few "issues"
VB Code:
Public Overloads Overrides Function ConvertTo(ByVal context As ITypeDescriptorContext, ByVal culture As CultureInfo, ByVal value As Object, ByVal destType As Type) As Object
If destType Is GetType(String) Then
' Attempt to convert the value into a string representation
Try
Dim styles As DrawStyles = CType(value, DrawStyles)
The problem with the colors displaying as hex should be on the bold line.
-----
Default values:
VB Code:
Public Overloads Overrides Function ConvertFrom(ByVal context As ITypeDescriptorContext, ByVal culture As CultureInfo, ByVal value As Object) As Object
If value.GetType() Is GetType(String) Then
[b]If IsNothing(Value) OrElse DirectCast(Value,String).Text.Length = 0 Then
Dim x as New DrawStyles
x.DrawMode = Vertical
x.Color1 = Color.White
x.Color2 = Color.Blue
Return x
End If[/b]
Try
' Attempt to parse the string representation
' into the constituent properties
Dim ds As New DrawStyles
Dim s As String = value
Dim c As String
Dim props As String() = s.Split(",".ToCharArray())
Re: Expandable Properties - Almost Solved but still a few "issues"
Cool getting even closer. But the top level prop only reset if I delete the concatinated string, but then it
reset correctly. When I reset the top level prop it resets to horizontal, 0, 0 and not my custom defaults I
added to each of the three child props. Do I need to add the defaults to the top level mailn prop too? Idf so
then how since you cant have more then one?
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum.
Re: Expandable Properties - Almost Solved but still a few "issues"
VB Code:
Color.FromArgb(Convert.ToInt32(c))
Returns a Color not a Integer, so I dont thin that would be the problem.
-----
Heres what I think is happening
VB Code:
Public Overloads Overrides Function ConvertTo(ByVal context As ITypeDescriptorContext, ByVal culture As CultureInfo, ByVal value As Object, ByVal destType As Type) As Object
If destType Is GetType(String) Then
' Attempt to convert the value into a string representation
Try
Dim styles As DrawStyles = CType(value, DrawStyles)
I think the problem is that when sytles.Color2 was assigned by Color.FromArgb(....) the integer does not represent a "KnownColor" so it can't name the color therefore it returns the hex value of the color.
Re: Expandable Properties - Almost Solved but still a few "issues"
Ok, I think I see what you mean. If its a RGB color there is no known color so the name will fail so it
stays as an hex value. So how do I fix that? Something like testing it for a known color name and if it
doesnt match then change it to a rgb value. How?
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum.
I just need to first get the color rgb conversion solved in the ConvertTo and ConvertFrom procedures.
Then I will work on the panel painting correclty issue.
Any help is appreciated.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum.
If IsKnownColor is True Then Use the Color.Name property
If IsKnownColor is False Then use ARGBColor.FromColor(Color).ToString ' or apply custom formatting
Re: Expandable Props - Color conversion issue - 6 Reps to the first who can solve it!!!
If the property is already a Color type, then you don't need to bother converting the ARGB value into it's components, instead you can access them via the R, G, B properties, here's an example using the DrawingStyles sample I posted previously:
VB Code:
Imports System.ComponentModel
Imports System.Globalization
' Expandable TypeConverter for the DrawStyles class
Public Class DrawStylesConverter
Inherits ExpandableObjectConverter
Private m_lastValue As DrawStyles = New DrawStyles
Public Overloads Overrides Function CanConvertFrom(ByVal context As ITypeDescriptorContext, ByVal t As Type) As Boolean
' Indicate which types this class can convert "From"
If t Is GetType(String) Then
Return True
End If
Return MyBase.CanConvertFrom(context, t)
End Function
Public Overloads Overrides Function ConvertFrom(ByVal context As ITypeDescriptorContext, ByVal culture As CultureInfo, ByVal value As Object) As Object
If value.GetType() Is GetType(String) Then
Try
' Attempt to parse the string representation
' into the constituent properties
Dim ds As New DrawStyles
Dim s As String = value
Dim c As String
Dim i As Integer
Dim props As String() = s.Split(",".ToCharArray())
' Revert back to the last good value if an error is encountered
' trying to parse the entered "text" value for the constiuent properties
Return m_lastValue
End Try
End If
MyBase.ConvertFrom(context, culture, value)
End Function
Public Overloads Overrides Function CanConvertTo(ByVal context As ITypeDescriptorContext, ByVal t As Type) As Boolean
' Indicate which types this class can convert "To"
If t Is GetType(DrawStyles) Then Return True
Return MyBase.CanConvertTo(context, t)
End Function
Public Overloads Overrides Function ConvertTo(ByVal context As ITypeDescriptorContext, ByVal culture As CultureInfo, ByVal value As Object, ByVal destType As Type) As Object
If destType Is GetType(String) Then
' Attempt to convert the value into a string representation
Try
Dim styles As DrawStyles = CType(value, DrawStyles)
This formats an unknow color in the format; "<R>, <G>, <B>" and returns the Name of a Known Color.
One thing to note; a possible drawback of formatting the RGB value with comma's is that you're using comma's as a sub-property delimiter, so it makes the parsing a little trickier and may not be as readable to the user.
Re: Expandable Props - Minor Property conversion issue
Ok, I can post this part since it parallels yours and is not specific. Here is the expandable prop class.
Its part of a namespace that I am importing into my UC class. Not too
sure if that makes a difference?
Also, I changed the name of the class to GradientStyle instead of DrawStyle
VB Code:
'...
'...
'...
End Class
' Expandable TypeConverter for the DrawStyles class
Public Class GradientStyleConverter
Inherits ExpandableObjectConverter
Private m_lastValue As GradientStyle = New GradientStyle
Public Overloads Overrides Function CanConvertFrom(ByVal context As ITypeDescriptorContext, ByVal t As Type) As Boolean
' Indicate which types this class can convert "From"
If t Is GetType(String) Then
Return True
End If
Return MyBase.CanConvertFrom(context, t)
End Function
Public Overloads Overrides Function ConvertFrom(ByVal context As ITypeDescriptorContext, ByVal culture As CultureInfo, ByVal value As Object) As Object
If value.GetType() Is GetType(String) Then
Try
' Attempt to parse the string representation
' into the constituent properties
Dim gs As New GradientStyle
Dim s As String = CType(value, String)
Dim c As String
Dim i As Integer
Dim props As String() = s.Split(",".ToCharArray())
' Revert back to the last good value if an error is encountered
' trying to parse the entered "text" value for the constiuent properties
Return m_lastValue
End Try
End If
MyBase.ConvertFrom(context, culture, value)
End Function
Public Overloads Overrides Function CanConvertTo(ByVal context As ITypeDescriptorContext, ByVal t As Type) As Boolean
' Indicate which types this class can convert "To"
If t Is GetType(GradientStyle) Then Return True
Return MyBase.CanConvertTo(context, t)
End Function
Public Overloads Overrides Function ConvertTo(ByVal context As ITypeDescriptorContext, ByVal culture As CultureInfo, ByVal value As Object, ByVal destType As Type) As Object
If destType Is GetType(String) Then
' Attempt to convert the value into a string representation
Try
Dim styles As GradientStyle = CType(value, GradientStyle)
Re: Expandable Props - Minor Property conversion issue
The problem lies in the ConvertFrom method where you're assigning the GradientMode.
The Enumeration is being passed the Gradient class type instead of the LinearGradientMode Enumeration type.
Re: Expandable Props - Minor Property conversion issue
w00t, w00t! Success You saved the day yet again. Thanks a million!
Now since I had taken a break from this project its time to move on to the next issue.
For some reason (probably someting that I missed again) the panel does not change graphics when the properties
are changed.
With the regular properties I had this working. Now that the expandable props are in place I need to hook
the actual drawing back up. I think it may be because there may be different instances of classes being created?
Thanks
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum.
Re: Expandable Props - Minor Drawing/Coloring Issue
Took a while to track down, but it looks like you need to implement a few more methods in the ExpandableObjectConverter,
in particular the CreateInstance method, which seems to be called whenever the sub-properties are changed,
triggering a change in the Expandable proeprty which can be intercepted and reacted to.
You'll need to tweak this a little, I didn't recognize the LinearGradientMode enum,
so I mocked up my own.
VB Code:
Imports System.ComponentModel
Imports System.Globalization
' Expandable TypeConverter for the DrawStyles class
Public Class GradientStyleConverter
Inherits ExpandableObjectConverter
Private m_lastValue As GradientStyle = New GradientStyle
Public Overloads Overrides Function CanConvertFrom(ByVal context As ITypeDescriptorContext, ByVal t As Type) As Boolean
' Indicate which types this class can convert "From"
If t Is GetType(String) Then
Return True
End If
Return MyBase.CanConvertFrom(context, t)
End Function
Public Overloads Overrides Function ConvertFrom(ByVal context As ITypeDescriptorContext, ByVal culture As CultureInfo, ByVal value As Object) As Object
If value.GetType() Is GetType(String) Then
Try
' Attempt to parse the string representation
' into the constituent properties
Dim gs As New GradientStyle
Dim s As String = CType(value, String)
Dim c As String
Dim i As Integer
Dim props As String() = s.Split(",".ToCharArray())
' Revert back to the last good value if an error is encountered
' trying to parse the entered "text" value for the constiuent properties
Return m_lastValue
End Try
End If
MyBase.ConvertFrom(context, culture, value)
End Function
Public Overloads Overrides Function CanConvertTo(ByVal context As ITypeDescriptorContext, ByVal t As Type) As Boolean
' Indicate which types this class can convert "To"
If t Is GetType(GradientStyle) Then Return True
Return MyBase.CanConvertTo(context, t)
End Function
Public Overloads Overrides Function ConvertTo(ByVal context As ITypeDescriptorContext, ByVal culture As CultureInfo, ByVal value As Object, ByVal destType As Type) As Object
If destType Is GetType(String) Then
' Attempt to convert the value into a string representation
Try
Dim styles As GradientStyle = CType(value, GradientStyle)
Public Overloads Overrides Function GetPropertiesSupported(ByVal context As ITypeDescriptorContext) As Boolean
' Indicate the we support returning a list
' of the properties this type contains
Return True
End Function
Public Overloads Overrides Function GetProperties(ByVal context As ITypeDescriptorContext, ByVal value As Object, ByVal attributes As Attribute()) As PropertyDescriptorCollection
' Return a collection of the Properties that make up this type
Dim collection As PropertyDescriptorCollection = TypeDescriptor.GetProperties(GetType(GradientStyle), attributes)
Dim props As String() = New String() {"GradientMode", "PanelColor1", "PanelColor2"}
Return collection.Sort(props)
End Function
Public Overloads Overrides Function GetCreateInstanceSupported(ByVal context As System.ComponentModel.ITypeDescriptorContext) As Boolean
' Indicate that creating an instance is supported
Return True
End Function
Public Overloads Overrides Function CreateInstance(ByVal context As System.ComponentModel.ITypeDescriptorContext, ByVal propertyValues As System.Collections.IDictionary) As Object
' Create a new instance of the Gradient Style
' with the supplied property values
Dim obj1 As Object = propertyValues("GradientMode")
Dim obj2 As Object = propertyValues("PanelColor1")
Dim obj3 As Object = propertyValues("PanelColor2")
If obj1 Is Nothing Then obj1 = LinearGradientMode.Mode1
If obj2 Is Nothing Then obj1 = Color.Red
If obj3 Is Nothing Then obj1 = Color.Blue
Dim gs As New GradientStyle
gs.GradientMode = obj1
gs.PanelColor1 = obj2
gs.PanelColor2 = obj3
Return gs
End Function
Private Function ColorToString(ByVal theColor As Color) As String
' Converts a Color into an appropriately formatted string
' For Known Colors the name is returned, otherwise the RGB value is returned
' in the format "R, G, B"
If Not [Enum].IsDefined(GetType(KnownColor), theColor.Name) Then
Re: Expandable Props - Minor Drawing/Coloring Issue
Ok, I think I am following you but for some reason I dont have anything available in my props.
No MyBase.Refresh, MyBase.Invalidate, MyBase.Update, Me.Refresh, Me.Invalidate, Me.Update.
Also, why do some of the procedures do nothing other then Return True?
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum.
Re: Expandable Props - Minor Drawing/Coloring Issue
If you're control is inheriting from UserControl or Control it
should have an Invalidate method which triggers a Paint event.
Either way, it doesn't matter, you must have something in your control
that gets called when you want it to draw itself, if so, then that's what you want
to call in the Set of the expandable property in your control.
Those methods that just return True are called by the IDE to see if your
class supports certain things, like supplying a list of the sub-properties and
creating instances of your class.
Regards,
- Aaron.
Edit:Re-reading your post makes me think that maybe you thought the Invalidate call would be in the converter class, it's not,
it's in your UserControl when you expose the property of type GradientStyle. - Aaron.
Last edited by Aaron Young; Apr 1st, 2005 at 11:26 PM.
Re: Expandable Props - Minor Drawing/Coloring Issue
Ok, I think I found whats wrong.
I have these three classes in one class file wrapped by a namespace and this namespace is being imported into the user control base.
The first class is the one (GradientPanel) that is inheriting the panel control and is where I am overridding the OnPaint event
to draw the gradient.
The second one (GradientStyle) is the one that has the 3 properties (DrawMode, PanelColor1, and PanelColor2). This one has the typeconverter (GradientStyleConverter) atribute.
The third one (GradientStyleConverter) is the one that inherits the ExpandableObjectConverter.
I think the first and second classes need to be in the same class and not separate?
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum.