Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

user control - public property

Posted on 2014-02-20
3
Medium Priority
?
299 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 2000 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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

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…
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…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

610 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