Solved

user control - public property

Posted on 2014-02-20
3
285 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
Comment Utility
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
Comment Utility
Thanks!
0
 
LVL 5

Author Comment

by:knowlton
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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 …
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

743 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

14 Experts available now in Live!

Get 1:1 Help Now