Solved

How add attribute with enum to control?

Posted on 2009-04-06
5
349 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
[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 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 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

695 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