Solved

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

Posted on 2004-10-27
168 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
Question by:toddhd
    5 Comments
     
    LVL 6

    Expert Comment

    by:The_Biochemist
    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
    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:
    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
    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
    Thats fine
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
    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…
    Want to pick and choose which updates you receive? Feel free to check out this quick video on how to manage your email notifications.
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    934 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

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now