Solved

C# Formatting a Control Globally

Posted on 2014-09-30
3
232 Views
Last Modified: 2014-09-30
I have a program that will have a lot of datetime pickers on the interface. This is a time keeping application so I need a lot of these controls formatted as follows:

this.tpTimePicker1.CustomFormat = "hh:mm tt";
this.tpTimePicker1.Format = System.Windows.Forms.DateTimePickerFormat.Custom;
this.tpTimePicker1.ShowUpDown = true;

Open in new window


I would prefer to not have to put this code in every time I have one of these controls. I'm thinking there has to be a way to create a global control/formatting so I can just use instances of the modified control.

Anyone know how to do this?

C#/Winforms/Studio2013
0
Comment
Question by:jczander
  • 2
3 Comments
 
LVL 33

Assisted Solution

by:it_saige
it_saige earned 500 total points
ID: 40353399
There are really a lot of ways you could do this depended upon your specific needs.  One simple way is to just inherit the class you want to set default properties for.

Something like:

using System.Windows.Forms;

namespace UIControls
{
	class MyDateTimePicker : DateTimePicker
	{
		private string fCustomFormat = "hh:mm tt";
		public string CustomFormat
		{
			get { return fCustomFormat; }
			set
			{
				if (!string.IsNullOrEmpty(value) && !value.Equals(fCustomFormat))
					fCustomFormat = value;
			}
		}

		private DateTimePickerFormat fFormat = DateTimePickerFormat.Custom;
		public DateTimePickerFormat Format
		{
			get { return fFormat; }
			set
			{
				if (!value.Equals(fFormat))
					fFormat = value;
			}
		}

		private bool fShowUpDown = true;
		public bool ShowUpDown
		{
			get { return fShowUpDown; }
			set
			{
				if (!value.Equals(fShowUpDown))
					fShowUpDown = value;
			}
		}
	}
}

Open in new window


And then just implement your class when you need that control.  Another way would be to implement a user control.

Inheriting from existing controls:
http://msdn.microsoft.com/en-us/library/7h62478z(v=vs.90).aspx

Implementing user controls:
http://msdn.microsoft.com/en-us/library/aa302342.aspx

-saige-
0
 
LVL 33

Accepted Solution

by:
it_saige earned 500 total points
ID: 40353403
In the code above, if you don't want the option of changing the properties, you could always implement it as such:

using System.Windows.Forms;

namespace UIControls
{
	class MyDateTimePicker : DateTimePicker
	{
		private string fCustomFormat = "hh:mm tt";
		public string CustomFormat
		{
			get { return fCustomFormat; }
		}

		private DateTimePickerFormat fFormat = DateTimePickerFormat.Custom;
		public DateTimePickerFormat Format
		{
			get { return fFormat; }
		}

		private bool fShowUpDown = true;
		public bool ShowUpDown
		{
			get { return fShowUpDown; }
		}
	}
}

Open in new window


HTH,

-saige-
0
 

Author Closing Comment

by:jczander
ID: 40353732
Excellent. Thanks a bunhc
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

A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
This article will show, step by step, how to integrate R code into a R Sweave document
This video teaches viewers about errors in exception handling.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…

932 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

13 Experts available now in Live!

Get 1:1 Help Now