Solved

user control - public property

Posted on 2014-02-20
3
293 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:Tom Knowlton
[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
  • 2
3 Comments
 
LVL 86

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:Tom Knowlton
ID: 39874717
Thanks!
0
 
LVL 5

Author Comment

by:Tom 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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Suggested Solutions

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
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…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

734 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