Solved

How add attribute with enum to control?

Posted on 2009-04-06
5
346 Views
Last Modified: 2012-05-06
How do I add an attribute to a custom control that takes an enum as its argument.  For example (in psuedo code):

[SetProperty1(SomeClass.Enum3)]
class MyControl() : Control
{
  MyControl(){
    int attributeValue = passedInEnumValue;}
}

I'd like to do this for web and winform controls.
0
Comment
Question by:brettr
  • 3
  • 2
5 Comments
 
LVL 29

Expert Comment

by:anarki_jimbel
ID: 24083540
Shouldn't be a problem. See my example for a very primitive control. After I build a solution I can drag the control from the toolbox onto my form. In the properties window I can see the property MyCustomProperty and all enumm members in a dropdown list.


    public class MyEnums

    {
 

        public enum Enum1

        {

            Enum1Item1, 

            Enum1Item2,

            Enum1Item3 

        }

    }
 

===================================
 

        public MyUserControl()

        {

            InitializeComponent();

            this.BorderStyle = BorderStyle.FixedSingle;

            this.BackColor = System.Drawing.Color.Red;

        }
 
 

        private MyEnums.Enum1 _customProp;

        public MyEnums.Enum1 CustomProperty

        {

            get { return _customProp; }

            set { _customProp = value; }

        }

    }

Open in new window

0
 
LVL 29

Expert Comment

by:anarki_jimbel
ID: 24083548
if this is not what you want - explain what do you need this attribute for?
0
 

Author Comment

by:brettr
ID: 24083793
I need an attribute because a designer will rarely be used.

I want to provide the control with a role.  It will then decide to show/hide based on the user being in that role.
0
 
LVL 29

Accepted Solution

by:
anarki_jimbel earned 500 total points
ID: 24084042
Hmmm... Don't think there is anatrribute you need. Still, you may try to create a custom one:
http://aspalliance.com/778_Attributes_and_Reflection_in_C

I'd choose another way...
0
 

Author Comment

by:brettr
ID: 24084086
Yes - that's a bit involved just for an attribute.

I could overload the constructor and pass in the role value, since the control may not use roles in some cases.  But if it does, the call will supply it via a constructor.  Is there a better way for this?
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Icons and Colors for Terms 3 24
C# HTTP GET method sample code 3 41
Help with C#, MVC, razor. 6 23
Where to download and how to install sqldmo.dll 5 34
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

895 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

16 Experts available now in Live!

Get 1:1 Help Now