[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

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

Posted on 2004-10-27
5
Medium Priority
?
173 Views
Last Modified: 2010-04-24
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!
0
Comment
Question by:toddhd
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 6

Expert Comment

by:The_Biochemist
ID: 12439399
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
 
LVL 8

Author Comment

by:toddhd
ID: 12440350
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
 
LVL 6

Accepted Solution

by:
The_Biochemist earned 2000 total points
ID: 12445625
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
 
LVL 8

Author Comment

by:toddhd
ID: 12445708
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
 
LVL 6

Expert Comment

by:The_Biochemist
ID: 12448982
Thats fine
0

Featured Post

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question