Thread: Setting the Default() value of a user control property when its an Enum?

Hopefully this is an easy one, but I'm just not getting it...

Let's say I expose a BorderStyle property of my control, like this:

<Category("Appearance"), DefaultValue(BorderStyle.Fixed3D), Browsable(True), Description("Indicates whether or not the edit control should have a border.")> _
Public Property TextBorderStyle() As BorderStyle
Get
Return Me.BorderStyle
End Get
Set(ByVal Value As BorderStyle)
Me.BorderStyle = Value
End Set
End Property

You'll notice that the DefaultValue is "BorderStyle.Fixed3D". This is the borderstyle that is showing up in the property browser, but it still showing as bolded, as if it has changed. How do I set this correctly so that it shows as the default?

Also, how would I do this with Colors? For example, if I try to expose a color property, and do this:

DefaultValue(Color.White)

This the IDE complains that I'm not using a Const - ugh!
LVL 8
toddhdAsked:
Who is Participating?
 
The_BiochemistConnect With a Mentor Commented:
Sorry, I misinterpreted your question as a 'how to do enums' question, and yes colors are a bad choice for an example i know - but it had been a bad day!

had a look on the net and found this which explains how to use colors as constants by getting and setting the ARGB values rather than the enum value.

http://www.devcity.net/newsletter/archive/devcity/devcity20040225.htm#ni950

And again I apologise for the mix up!
0
 
The_BiochemistCommented:
1) you would create an enum such as this:

Public Enum MyColors
        AliceBlue = 0
        Red = 1
        Purple = 2
        black = 3
    End Enum


2) Create your property (in this case st get and set the back color of the textbox

Public Property ChangeColor(ByVal Clr As MyColors) As MyColors
        Get

            Select Case Label1.BackColor.ToString
                Case "AliceBlue"
                    Return MyColors.AliceBlue
                Case "Red"
                    Return MyColors.Red
                Case "Purple"
                    Return MyColors.Purple
                Case "Black"
                    Return MyColors.black
            End Select
        End Get
        Set(ByVal Value As MyColors)
            Select Case Value
                Case MyColors.AliceBlue
                    Label1.BackColor = System.Drawing.Color.AliceBlue
                Case MyColors.Red
                    Label1.BackColor = System.Drawing.Color.Red
                Case MyColors.Purple
                    Label1.BackColor = System.Drawing.Color.Purple
                Case MyColors.black
                    Label1.BackColor = System.Drawing.Color.Black
                case else
                   Label1.BackColor = System.Drawing.Color.Black
            End Select
        End Set
    End Property

Enums are nothing more than a way to force users to select a specific item and not have the choice of anything... They are a good way of error trapping methods as you know all of the possible inputs and can handle them accordingly.
0
 
toddhdAuthor Commented:
Thanks for your input. I'm already familiar with Enums and control prop's however, that wasn't really the question. The problem is with the the first line:
<Category("Appearance"), DefaultValue(BorderStyle.Fixed3D), Browsable(True), Description("Indicates whether or not the edit control should have a border.")> _

If no DefaultValue is specified, then the property shows up in the IDE as Bolded, as if has changed. In this case however, even though I specified the correct DefaultValue, it still shows in the IDE as Bolded. This should not happen.

Your example with colors above is a bit redundant, as the System.Colors are already an enum. So you just do this:

Public Property ChangeColor() As System.Drawing.Color
.. and so on

What you did was create an enum that was passed to a select case all in order to just point to an existing enum. There are a LOT of System.Drawing.Colors, and this would be an enormous amount of code to work around that.

The problem with Colors is that, for some reason, they are not considered a Const, whereas an Enum should always be a Const. For example, when I used this:
DefaultValue(BorderStyle.Fixed3D)

The compiler takes it just fine. But if I do this:
DefaultValue(Color.White)

Then the compiler complains that I didn't use a Const value. I assume that the System.Colors must change with the given OS host, and therefore aren't Const somehow. In that regard, your example may be one way around that problem, although I think there must be better one.

Someone on another forum told me that DefaultValue() works just fine with Enums in C#, that's just a VB thing. That may well be true, but I can't find any info on it.
0
 
toddhdAuthor Commented:
Thanks! I haven't tried it yet, but that looks like the right answer for the color issue. I'm gonna wait a while to accept answers, to see if anyone else has info on the DefaultValue() property, in which case I'll make sure you get credited as well.
0
 
The_BiochemistCommented:
Thats fine
0
All Courses

From novice to tech pro — start learning today.