CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Dec 2008
    Posts
    25

    trying to add enum property to custom button

    What is the correct way to add an enumerated type to a custom control property so that the client can select a choice of enumerations in the control's Properties setting at Design Time?

    I have a custom button control and I want the client to select a button style from an enumerated range of values:

    Code:
           public enum BUTTONSTYLE { Style_Solid, Style_GradientA, Style_GradientB, Style_Image, Style_Liquid };
    But when I try to expose it thusly:
    Code:
            [DefaultValue( BUTTONSTYLE.Style_Solid)]
            [Category("Appearance")]
            [Browsable(true)]
            [Description("Sets style of the button")]
            public BUTTONSTYLE newbuttonstyle
            {
                get
                {
                    return newbuttonstyle;
                }
                set
                {
                    newbuttonstyle = value;
                }
            }
    Visual Studio 2005 crashes and disappears without any error messages as soon as I drag and drop the control onto a form! It leaves nothing in the Event Log and the control disappears from the toolbox! What an I doing wrong and how do I debug this?

    My button already has other (non-enum) custom properties that work just fine.

    Thanks in advance for any insights.

    (PS - I've been using Visual Studio 2005 since, well, 2005, and this is the fist time I've ever actually crashed it!)

  2. #2
    Join Date
    Jun 2008
    Posts
    2,477

    Re: trying to add enum property to custom button

    It has nothing to do with the Enumerated type, you have a recursive function. You forgot to capitalize the property name.

    Yours
    Code:
            public BUTTONSTYLE newbuttonstyle
            {
                get
                {
                    return newbuttonstyle;
                }
                set
                {
                    newbuttonstyle = value;
                }
            }
    Should be

    Code:
            public BUTTONSTYLE Newbuttonstyle
            {
                get
                {
                    return newbuttonstyle;
                }
                set
                {
                    newbuttonstyle = value;
                }
            }
    Assuming that 'newButtonStyle" is the name of the member variable behind the property.

  3. #3
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,538

    Re: trying to add enum property to custom button

    As BigED already mentione maybe an explanation is needed
    If you use the get Property it wants to return itself as both is written in small letters so it again calls the get Property and this again... endless you see
    Code:
            public BUTTONSTYLE newbuttonstyle
            {
                get
                {
                    return newbuttonstyle;
                }
                set
                {
                    newbuttonstyle = value;
                }
            }
    Basically I would suggest to use a style to write your code which prevents such errors.
    I myself use _newButtonStyle for the internal field and NewButtonStyle for the Property. The underscore in the beginning is also very useful in the intellisense. All private fields of a class are shown this way together in the top of the intellisense. This way they are not alphabetically mixed up between the methds and Properties, you can see them on one shot. Alos during programming
    reading your code you always see, when one of the private fields is accessed / used. But this is a hint only, Everyone has to find his own coding style.
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

  4. #4
    Join Date
    Dec 2008
    Posts
    25

    Re: trying to add enum property to custom button

    Basically I would suggest to use a style to write your code which prevents such errors.
    Thank you (to both posters)!

    Yes, I normally to use a intricate set of naming conventions - this was just a typo.

    I'm surprised that the compiler didn't catch this when the control was being built. I would have expected a CS0102 error or similar.

  5. #5
    Join Date
    Jun 2008
    Posts
    2,477

    Re: trying to add enum property to custom button

    Quote Originally Posted by plnelson View Post
    Thank you (to both posters)!

    Yes, I normally to use a intricate set of naming conventions - this was just a typo.

    I'm surprised that the compiler didn't catch this when the control was being built. I would have expected a CS0102 error or similar.
    Recursion is perfectly legal, so the compiler will not warn you about it.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured