Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Properties
#1
Properties are essential if you are creating a custom object. Essentially, properties are the things that can be edited in the "visual designer" - all of the things that are accessible via the properties window in the lower right hand corner of Visual Studio.

Adding basic properties to your form is easy. All you need is an instance variable and a property block. Then you're all set. For example, here is a sample property arrangemenet:
[code2=vbnet]Private _messageString As String
Public Property MessageString As String
Get
Return _messageString
End Get
Set(value As String)
_messageString = value
End Set
End Property[/code2]

Of course, in this example we use a String object. In the real world, any object can be used. After this is added to your object, the user will be able to edit the properties via the properties Window and give this variable a specific value.

What if you wanted to give the user a list of options to choose from, though? This can be doing by combining properties with enumerations. Thus:
[code2=vbnet]Private _messageString As messageIdentifier
Public Property MessageString As messageIdentifier
Get
Return _messageString
End Get
Set(value As messageIdentifier)
_messageString = value
End Set
End Property

Enum messageIdentifier As Integer
Greetings = 1
Goodbyes = 2
Warning = 3
Declaration = 4
End Enum[/code2]

Although the above code snippet may seem rather ridiculous since the Enumerations don't actually correspond to anything, this is a way of offering the user a list of options to choose from. In this case, the list will contain "Greetings", "Goodbyes", "Warning", and "Declaration". You can then work with this choice by processing the enumeration in some way, as such:
[code2=vbnet]Private Function GetMessageString() As String
If (_messageString = messageIdentifier.Declaration) Then
Return "Of Independence"
ElseIf (_messageString = messageIdentifier.Goodbyes) Then
Return "It has been a fun time, but I must leave you."
ElseIf (_messageString = messageIdentifier.Greetings) Then
Return "Salutations, my good man/woman"
ElseIf (_messageString = messageIdentifier.Warning) Then
Return "Back off! You have no business in this area."
Else
Return ""
End If
End Function[/code2]


Thus, it is really easy to give the user the ability to edit properties when they add your custom control to their form. The only downside occurs when you update a property to be a different object, but do not change the value of the property. Massive errors ensue.

View the video here: <!-- m --><a class="postlink" href="http://www.youtube.com/watch?v=JAK6veamu8o&feature=youtu.be">http://www.youtube.com/watch?v=JAK6veam ... e=youtu.be</a><!-- m -->

Feel free to download a sample project:
[attachment=0]<!-- ia0 -->PropertyTutorial.zip<!-- ia0 -->[/attachment]


Attached Files
.zip   PropertyTutorial.zip (Size: 74.13 KB / Downloads: 609)
My Blog | My Setup | My Videos | Have a wonderful day.
#2
I have some additional code for properties:

1] Create a description for the property

Code:
<System.ComponentModel.Description("your_description")>
You need to put this code above
Code:
Public Property YourProperty() As String
so it'll eventually look like this:

Code:
Public _YourProperty
    <System.ComponentModel.Description("your_description")>
    Public Property YourProperty() As String
        Get
            Return _YourProperty
        End Get
        Set(ByVal value As String)
            _YourProperty= value
        End Set
    End Property
If you click the property you will see a description your_description.

2] Define a category where the property has to be listed
If you don't define a category Visual Studio will create a new one that is called 'Misc'.

To add your property to a category or create a new one enter this following line
Code:
<Category("Data")>
above the
Code:
Public Property YourPropery() As String
Your property will now be displayed in the category Data.


Forum Jump:


Users browsing this thread: 1 Guest(s)