Solved

C# Formatting a Control Globally

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

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 32

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Launch Process with NOT as Administrator 15 67
dispose of a DataTable created in another class 4 22
c# code 19 61
XML & .net 5 21
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
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.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

746 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

10 Experts available now in Live!

Get 1:1 Help Now