Solved

C# Formatting a Control Globally

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

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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.
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

696 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