?
Solved

C# Formatting a Control Globally

Posted on 2014-09-30
3
Medium Priority
?
236 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
[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 34

Assisted Solution

by:it_saige
it_saige earned 2000 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 34

Accepted Solution

by:
it_saige earned 2000 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

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

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

800 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