Solved

C# Formatting a Control Globally

Posted on 2014-09-30
3
233 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

827 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