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
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:


This the IDE complains that I'm not using a Const - ugh!
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

            Select Case Label1.BackColor.ToString
                Case "AliceBlue"
                    Return MyColors.AliceBlue
                Case "Red"
                    Return MyColors.Red
                Case "Purple"
                    Return MyColors.Purple
                Case "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
                    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.
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:

The compiler takes it just fine. But if I do this:

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.
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.

And again I apologise for the mix up!

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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.
Thats fine
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.