Solved

user control - public property

Posted on 2014-02-20
3
288 Views
Last Modified: 2014-02-20
Been away from Windows Form Controls dev for a while.

I have a control that displays the date and time in a label.

I want to create a property that is visible in the visual studio "Properties" window, such that I can grab the current contents of the label any time I want to.

I think attributes are used, but I don't remember how to do it.

Thanks, and please include small 'hello world" sample C# code for me.
0
Comment
Question by:knowlton
  • 2
3 Comments
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 39874505
I just added a Property to the UserControl and it automatically appeared in the Properties Pane when the control was selected.

For example:
    public partial class DateTimeControl : UserControl
    {
        public DateTimeControl()
        {
            InitializeComponent();
            CurrentDateTime = DateTime.Now;
        }

        private DateTime currentDateTime;

        public DateTime CurrentDateTime
        {
            get
            {
                return this.currentDateTime;
            }
            set
            {
                this.currentDateTime = value;
                this.label1.Text = this.currentDateTime.ToString();
            }
        }

    }

Open in new window


Here it is in the Properties Pane:
Property in the Property Pane
0
 
LVL 5

Author Closing Comment

by:knowlton
ID: 39874717
Thanks!
0
 
LVL 5

Author Comment

by:knowlton
ID: 39874722
My interim solution:

namespace DateTimeControl
{
    public partial class UserControlDateTime: UserControl
    {
        public UserControlDateTime()
        {
            InitializeComponent();
        }


        [Description("DateTime displayed by the label"), Category("Data")]
        public string LabelDateTimeText
        {
            get { return labelDateTime.Text; }            
        }
        private void timerTick_Tick(object sender, EventArgs e)
        {
            DateTime dt = new DateTime();
            dt = DateTime.Now;
            labelDateTime.Text = dt.ToLongDateString() + " " + dt.ToLongTimeString();
        }

      
    }

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
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…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

911 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