|
|||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionFor my last project, I had a requirement where I can use So, I decided to put all the information I collected, with fully functional working examples in VB.NET (which is my favorite) and let me tell you, it is the most complex control of all controls in VS.NET, yet very powerful and extremely useful, particularly in Custom Controls, and for Application Configuration settings. Visual Studio .NET property browser is also based upon the Its so complex that I have to split this article into two parts. In this Part-I, I will explain how to use the basic properties and DescriptionOpen a new Windows application project using VB.NET. By default Adding the control, and dropping it on the Designer is pretty much the same like any other control. Fun starts after that. In order to use the Imports System.ComponentModel
'''
<DefaultPropertyAttribute("Title")> _
Public Class SimpleProperties
'''
Private _Title As String
Private _Show As Boolean
Private _Number As Short
'''
<CategoryAttribute("Application"), _
Browsable(True), _
[ReadOnly](False), _
BindableAttribute(False), _
DefaultValueAttribute(""), _
DesignOnly(False), _
DescriptionAttribute("Enter Title for the application")> _
Public Property Title() As String
Get
Return _Title
End Get
Set(ByVal Value As String)
_Title = Value
End Set
End Property
'''
<CategoryAttribute("Application"), _
Browsable(True), _
[ReadOnly](False), _
BindableAttribute(False), _
DefaultValueAttribute("True"), _
DesignOnly(False), _
DescriptionAttribute("Show option")> _
Public Property Show() As Boolean
Get
Return _Show
End Get
Set(ByVal Value As Boolean)
_Show = Value
End Set
End Property
'''
<CategoryAttribute("Application"), _
Browsable(True), _
[ReadOnly](False), _
BindableAttribute(False), _
DefaultValueAttribute("0"), _
DesignOnly(False), _
DescriptionAttribute("Enter a number")> _
Public Property Number() As Short
Get
Return _Number
End Get
Set(ByVal Value As Short)
_Number = Value
End Set
End Property
'''
End Class
To your PropertyGrid1.SelectedObject = New SimpleProperties()
Press F5 to run the project. Let's see the elements in the SimpleProperties.vb class
Then add the properties that you want to see in the
Let's add some Private _ApplicationSize As Size
Private _ApplicationLocation As Point
Private _ApplicationFont As System.Drawing.Font
Private _FontColor As System.Drawing.Color
Private _ApplicationIcon As System.Drawing.Icon
Private _ApplicationCursor As System.Windows.Forms.Cursor
Private _ApplicationLangugae As System.Globalization.CultureInfo
'''
<CategoryAttribute("Design"), DefaultValueAttribute(""), _
DescriptionAttribute("Enter Application size")> _
Public Property ApplicationSize() As Size
Get
Return _ApplicationSize
End Get
Set(ByVal Value As Size)
_ApplicationSize = Value
End Set
End Property
<CategoryAttribute("Design"), DefaultValueAttribute(""), _
DescriptionAttribute("Enter Application Location")> _
Public Property ApplicationLocation() As Point
Get
Return ApplicationLocation
End Get
Set(ByVal Value As Point)
_ApplicationLocation = Value
End Set
End Property
<CategoryAttribute("Design"), DefaultValueAttribute(""), _
DescriptionAttribute("Select font for the application")> _
Public Property ApplicationFont() As System.Drawing.Font
Get
Return _ApplicationFont
End Get
Set(ByVal Value As System.Drawing.Font)
_ApplicationFont = Value
End Set
End Property
<CategoryAttribute("Design"), DefaultValueAttribute(""), _
DescriptionAttribute("Select font color")> _
Public Property FontColor() As System.Drawing.Color
Get
Return _FontColor
End Get
Set(ByVal Value As System.Drawing.Color)
_FontColor = Value
End Set
End Property
<CategoryAttribute("Design"), DefaultValueAttribute(""), _
DescriptionAttribute("Select icon for application")> _
Public Property ApplicationIcon() As System.Drawing.Icon
Get
Return _ApplicationIcon
End Get
Set(ByVal Value As System.Drawing.Icon)
_ApplicationIcon = Value
End Set
End Property
<CategoryAttribute("Design"), DefaultValueAttribute(""), _
DescriptionAttribute("Select application's cursor")> _
Public Property ApplicationCursor() As System.Windows.Forms.Cursor
Get
Return _ApplicationCursor
End Get
Set(ByVal Value As System.Windows.Forms.Cursor)
_ApplicationCursor = Value
End Set
End Property
<CategoryAttribute("Design"), DefaultValueAttribute(""), _
DescriptionAttribute("Select User language")> _
Public Property ApplicationLangugae() As System.Globalization.CultureInfo
Get
Return _ApplicationLangugae
End Get
Set(ByVal Value As System.Globalization.CultureInfo)
_ApplicationLangugae = Value
End Set
End Property
Again,
Adding custom dropdown listNow let's add a custom dropdown list which shows all states. In order to add a custom dropdown list, create a class that inherits from Public Class StatesList : Inherits System.ComponentModel.StringConverter
Override the Public Overloads Overrides Function _
GetStandardValues(ByVal context As _
System.ComponentModel.ITypeDescriptorContext) _
As System.ComponentModel.TypeConverter.StandardValuesCollection
Return New StandardValuesCollection(_States)
End Function
Override the Dim _States As String() = New String() {"Alabama", "Alaska",_
"Arizona", "Arkansas", _
"California", "Colorado", "Connecticut", "Delaware",_
"Florida", "Georgia", _
"Hawaii", "Idaho", "Illinois", "Indiana", "Iowa", "Kansas", _
"Kentucky", "Louisiana", "Maine", "Maryland", "Massachusetts", _
"Michigan", "Minnesota", "Mississippi", "Missouri", "Montana", _
"Nebraska", "Nevada", "New Hampshire", _
"New Jersey", "New Mexico", _
"New York", "North Carolina", "North Dakota",_
"Ohio", "Oklahoma", _
"Oregon", "Pennsylvania", "Rhode Island", "South Carolina", _
"South Dakota", "Tennessee", "Texas", "Utah",_
"Vermont", "Virginia",_
"Washington", "West Virginia", "Wisconsin", "Wyoming"}
Public Overloads Overrides Function _
GetStandardValues(ByVal context As _
System.ComponentModel.ITypeDescriptorContext) _
As System.ComponentModel.TypeConverter.StandardValuesCollection
Return New StandardValuesCollection(_States)
End Function
Finally override the Public Overloads Overrides Function _
GetStandardValuesExclusive(ByVal context _
As System.ComponentModel.ITypeDescriptorContext) _
As Boolean
Return True
End Function
Then apply the Private _State As String
'''
<TypeConverter(GetType(StatesList)), _
CategoryAttribute("Custom List"), DefaultValueAttribute(""), _
DescriptionAttribute("Select a state from the list")> _
Public Property State() As String
Get
Return _State
End Get
Set(ByVal Value As String)
_State = Value
End Set
End Property
and here is the output.
Displaying custom data types with expandable properties.NET framework data types like Imports System.ComponentModel
Imports System.ComponentModel.Design.Serialization
'''
<TypeConverter(GetType(ApplicationVersionConverter))> _
Public Class ApplicationVersion
'''
Private _Major As Short
Private _Minor As Short
Private _Build As Short
Private _Private As Short
'''
<DescriptionAttribute("Set the major part of version number")> _
Public Property Major() As Short
Get
Return _Major
End Get
Set(ByVal Value As Short)
_Major = Value
End Set
End Property
'''
<DescriptionAttribute("Set the minor part of version number")> _
Public Property Minor() As Short
Get
Return _Minor
End Get
Set(ByVal Value As Short)
Me._Minor = Value
End Set
End Property
'''
<DescriptionAttribute("Set the build part of version number")> _
Public Property Build() As Short
Get
Return _Build
End Get
Set(ByVal Value As Short)
Me._Build = Value
End Set
End Property
'''
<DescriptionAttribute("Set the private part of version number")> _
Public Property [Private]() As Short
Get
Return _Private
End Get
Set(ByVal Value As Short)
Me._Private = Value
End Set
End Property
'''
End Class
All the above code is like the other examples, except Friend Class ApplicationVersionConverter : Inherits ExpandableObjectConverter
Override the Public Overloads Overrides Function _
CanConvertTo(ByVal context As _
System.ComponentModel.ITypeDescriptorContext, _
ByVal destinationType As System.Type) As Boolean
If (destinationType Is GetType(ApplicationVersion)) Then
Return True
End If
Return MyBase.CanConvertFrom(context, destinationType)
End Function
Override the 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
Override the 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 versionParts() As String
Dim VersionString As String = ""
versionParts = Split(s, ".")
If Not IsNothing(versionParts) Then
Dim _ApplicationVersion As _
ApplicationVersion = New ApplicationVersion()
If Not IsNothing(versionParts(0)) Then
_ApplicationVersion.Major = versionParts(0)
If Not IsNothing(versionParts(1)) Then
_ApplicationVersion.Minor = versionParts(1)
If Not IsNothing(versionParts(2)) Then
_ApplicationVersion.Build = versionParts(2)
If Not IsNothing(versionParts(3)) Then
_ApplicationVersion.Private = versionParts(3)
End If
Catch ex As Exception
Throw New ArgumentException("Can not convert '" + _
value + "' to type ApplicationVersion")
End Try
End If
Return MyBase.ConvertFrom(context, culture, value)
End Function
Override the 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 ApplicationVersion) Then
Dim _ApplicationVersion As _
ApplicationVersion = CType(value, ApplicationVersion)
' build the string as "Major.Minor.Build.Private"
Return _ApplicationVersion.Major & "."_
& _ApplicationVersion.Minor & "." & _
_ApplicationVersion.Build & "." _
& _ApplicationVersion.Private
End If
Return MyBase.ConvertTo(context, culture, value, destinationType)
End Function
and here is the final output for this article.
In the source code I have a much elaborated example, which mimic's the Notes & To-Do featuresIn the next article I will explain about using custom editors to display properties like the docking, Graphics and tabs. | ||||||||||||||||||||||||||||||||||||||