[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How add attribute with enum to control?

Posted on 2009-04-06
5
Medium Priority
?
353 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 30

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 30

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 30

Accepted Solution

by:
anarki_jimbel earned 2000 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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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.
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…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses
Course of the Month20 days, 8 hours left to enroll

868 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