Groups | Blog | Home
all groups > vb.net controls > october 2006 >

vb.net controls : property Grid


Lespaul36
10/10/2006 10:54:00 PM
I have a control that I have made that uses a custom class for storage of
some of the information. I want it to display in the VS.Net 2003 property
grid in the IDE like the Font property does, so a use can expand it and set
the properties. How do I do this? Thanks in advance.

JerryWEC
10/11/2006 10:20:23 AM
I'm not an expert in control development but I do have some information to
send your way...

You have to have public properties for each item you want displayed in the
IDE properties window. And you have to add a browseable attribute to each
property. Should be able to find some information on <Browsable> (VB.net)
or [Browseable] (C#).

Your control has to be a control you can drag and drop on to the IDE or
maybe a UserControl will allow you to do it as well.

JerryM

Lespaul36
10/12/2006 4:17:31 PM
Not quite what I meant. The control works and shows the properties in the
property windows, but I have one property that is set to a class that I
wrote...I would like to figure out a way to make it show in the property
window too. Right now, it shows the name of the class, but I can't expand
it or anything.

I figure that structures such as "Location" and classes such as "Font" can
do this, why can't I make one that does too?

[quoted text, click to view]

Lespaul36
10/14/2006 9:17:47 PM
I figured out how to do it. I had to make a type converter to do it. I
will post the code so that others who look at this can figured.

Here is the TypeConverter:


Imports System
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.ComponentModel
Imports System.Runtime.InteropServices
Imports System.ComponentModel.Design.Serialization
Imports System.Text

Public Class GradientTypeConverter
Inherits ExpandableObjectConverter

Public Overloads Overrides Function CanConvertTo(ByVal context As
System.ComponentModel.ITypeDescriptorContext, ByVal destinationType As
System.Type) As Boolean
If destinationType Is GetType(Gradient) Then
Return True
End If
MyBase.CanConvertFrom(context, destinationType)
End Function

Public Overloads Overrides Function CanConvertFrom(ByVal context As
System.ComponentModel.ITypeDescriptorContext, ByVal sourceType As
System.Type) As Boolean
If sourceType Is GetType(String) Then
Return True
End If

Return MyBase.CanConvertFrom(context, sourceType)
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
Dim s As String = CType(value, String)
Dim tempProp As String()
Dim _Gradient As New Gradient
If s.Length > 0 Then
tempProp = Split(s, ";")
If Not IsNothing(tempProp) AndAlso tempProp.Length > 0
Then
Dim i As Short
Dim tempValues() As String
For i = 0 To CShort(tempProp.Length - 1)
tempValues = Split(tempProp(i), "=")
tempValues(0) = LCase(Trim(tempValues(0)))
If IsNothing(tempValues(1)) Then tempValues(1) =
""
Select Case tempValues(0)
Case "bar color 1"
_Gradient.BarColor1 =
Color.FromArgb(CInt(tempValues(1)))
Case "bar color 2"
_Gradient.BarColor2 =
Color.FromArgb(CInt(tempValues(1)))
Case "gradient mode"
_Gradient.GradientMode =
CType(System.Enum.Parse(GetType(System.Drawing.Drawing2D.LinearGradientMode)
, (tempValues(1))), LinearGradientMode)
Case "sigma focus"
_Gradient.SigmaFocus =
CInt(tempValues(1))
Case "sigma scale"
_Gradient.SigmaScale =
CInt(tempValues(1))
Case "use sigmabell"
_Gradient.UseSigmaBell =
CBool(tempValues(1))
End Select
Next
End If
End If
Catch ex As Exception
' if we got this far, complain that we
' couldn't parse the string
'
Throw New ArgumentException("Can not convert '" &
value.ToString & "' to type Person")
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 Is GetType(System.String) AndAlso TypeOf value
Is Gradient) Then
Dim _Gradient As Gradient = CType(value, Gradient)
Dim _GradientBuilder As New StringBuilder
With _GradientBuilder
.Append("Bar Color 1=" & _Gradient.BarColor1.ToArgb.ToString
& ";")
.Append("Bar Color 2=" & _Gradient.BarColor2.ToArgb.ToString
& ";")
.Append("Gradient Mode=" & _Gradient.GradientMode & ";")
.Append("Sigma Focus=" & _Gradient.SigmaFocus & ";")
.Append("Sigma Scale=" & _Gradient.SigmaScale & ";")
.Append("Use SigmaBell=" & _Gradient.UseSigmaBell & ";")
End With
Return _GradientBuilder.ToString
End If

Return MyBase.ConvertTo(context, culture, value, destinationType)

End Function
End Class



Here is the class that I am converting:

Imports System
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.ComponentModel
Imports System.Runtime.InteropServices
Imports System.ComponentModel.Design.Serialization

''' ------------------------------------------------------------------------
-----
''' <summary>
''' Stores information for making a gradient style ProgressBar.
''' </summary>
''' <remarks>
''' Stores information for making a gradient style ProgressBar.
''' </remarks>
''' <history>
''' [David] 10/10/2006 Created
''' </history>
''' ------------------------------------------------------------------------
-----
<Serializable(), ComVisible(True),
TypeConverter(GetType(GradientTypeConverter))> _
Public Class Gradient
Implements IDisposable

Private mCol1 As Color = Color.FromKnownColor(KnownColor.ActiveCaption)
Private mCol2 As Color = Color.Red
Private mGradientStyle As LinearGradientMode =
LinearGradientMode.Horizontal
Private mblnIsSigmaBell As Boolean = False
Private msngSigmaScale As Single = 0.75
Private msngSigmaFocus As Single = 0.5
Private mintSigmaLocation As Integer = 50
Private mintSigmaScale As Integer = 75





''' ------------------------------------------------------------------------
-----
''' <summary>
''' Creates a new Gradient Class.
''' </summary>
''' <remarks>
''' Creates a new Gradient Class.
''' </remarks>
''' <history>
''' [David] 10/10/2006 Created
''' </history>




''' ------------------------------------------------------------------------
-----
Public Sub New()
'do nothing
End Sub





''' ------------------------------------------------------------------------
-----
''' <summary>
''' Creates a new Gradient Class.
''' </summary>
''' <param name="BarColor1">
''' Sets the color of the first bar of the ProgressBar.
''' </param>
''' <param name="BarColor2">
''' Sets the color of the second bar of the ProgressBar.
''' </param>
''' <param name="GradientStyle">
''' Sets the gradient style of the ProgressBar.
''' </param>
''' <remarks>
''' </remarks>
AddThis Social Bookmark Button