question

LoveToCode-0935 avatar image
0 Votes"
LoveToCode-0935 asked LoveToCode-0935 edited

Propertybox in vb.net

Hi, I would like to know how can I use the property box to display my project custom properties.

Thanks

dotnet-visual-basic
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

This depends on your definition of custom properties. Can you provide a sample description to what you would like to display in a PropertyGrid?

0 Votes 0 ·

Thank you for the reply. So for example Priority, State, Created by, Assigned to etc. just basic properties.

0 Votes 0 ·

1 Answer

karenpayneoregon avatar image
1 Vote"
karenpayneoregon answered LoveToCode-0935 edited

The following is a starter for you. Study to get acquainted with the basics then learn about TypeConverter for special property types. Note all code is in a form but each class should be in it's own file.

91209-figure1.png
91210-figure2.png

 Imports System.ComponentModel
 Imports System.Globalization
    
 Public Class Form1
     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
         Dim item As New AppSettings With {.Assigned = "Jim", .Description = "TODO", .Priority = Priority.High}
         PropertyGrid1.SelectedObject = item
     End Sub
    
     Private Sub DetailsButton_Click(sender As Object, e As EventArgs) Handles DetailsButton.Click
         Dim currentValues = CType(PropertyGrid1.SelectedObject, AppSettings)
         Console.WriteLine()
     End Sub
 End Class
 Public Class AppSettings
     <Category("Main"), DisplayName("About"), Description("Some text to say about this property")>
     Public Property Description() As String
     <Category("Section 1"), Description("Priority of something")>
     Public Property Priority() As Priority
     <Category("Section 2"), Description("Person name")>
     Public Property Assigned() As String
     <Description("Name of the thing")>
     Public Property Name() As String
    
     <Description("Whether activated or not")>
     Public Property Activated() As Boolean
    
     <Description("Rank of the thing")>
     Public Property Rank() As Integer
    
     <Description("extra free-form attributes on this thing.")>
     <Editor(
         "System.Windows.Forms.Design.StringCollectionEditor," &
         "System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a",
         GetType(Drawing.Design.UITypeEditor))>
     <TypeConverter(GetType(MyConverter))>
     Public ReadOnly Property ExtraStuff() As List(Of String)
         Get
             If _attributes Is Nothing Then
                 _attributes = New List(Of String)()
             End If
             Return _attributes
         End Get
     End Property
     Private _attributes As List(Of String)
 End Class
 Public Enum Priority
     High
     Medium
     Low
 End Enum
 Public Class MyConverter
     Inherits TypeConverter
    
     Public Overrides Function ConvertTo(
             context As ITypeDescriptorContext,
             culture As CultureInfo, value As Object,
             destinationType As Type) As Object
    
         Dim list As List(Of String) = TryCast(value, List(Of String))
    
         If destinationType Is GetType(String) Then
             Return String.Join(",", list.ToArray())
         End If
    
         Return MyBase.ConvertTo(context, culture, value, destinationType)
    
     End Function
 End Class

And for list drop-down
91189-figure3.png

 Public Class FormatStringConverter
     Inherits StringConverter
    
     Public Overrides Function GetStandardValuesSupported(ByVal context As ITypeDescriptorContext) As Boolean
         Return True
     End Function
     Public Overrides Function GetStandardValuesExclusive(ByVal context As ITypeDescriptorContext) As Boolean
         Return True
     End Function
     Public Overrides Function GetStandardValues(context As ITypeDescriptorContext) As StandardValuesCollection
         Dim list As New List(Of String) From {
             "",
             "Jim",
             "Mary",
             "Frank",
             "Karen"
         }
         Return New StandardValuesCollection(list)
     End Function
 End Class

Add this to the base class

 Private _formatString As String
    
 <TypeConverter(GetType(FormatStringConverter))>
 Public Property Names() As String
     Get
         Return _formatString
     End Get
     Set(ByVal value As String)
         _formatString = value
     End Set
 End Property



91277-kpmvp1.png



figure1.png (13.9 KiB)
figure2.png (21.5 KiB)
figure3.png (14.9 KiB)
kpmvp1.png (3.5 KiB)
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Your welcome, check out the following code sample which has some more cool stuff for a PropertyGrid that I slapped together. Check out the enum converter for instance.

Question property is a Boolean

91100-f1.png


1 Vote 1 ·
f1.png (19.3 KiB)

Thank you again. I will certainly go through these stuffs. Can you please take a look on my question
https://docs.microsoft.com/en-us/answers/questions/371664/how-to-add-a-related-child-workitem-in-vbnet.html

0 Votes 0 ·