Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Setting BackColor on DateTimePicker control. 4

Status
Not open for further replies.

bernardmanning

Programmer
Oct 14, 2003
61
GB

hopefully a really simple one...

Can anybody tell me how to set the background colour of the datetimepicker control?

I don't mean the calendar itself but the control as it appears as you drop it on the form.

I don't see forecolor or backcolor properties on the property sheet as you would with say a text box control.

Many Thanks, Bernard....
 
According to the help the date time picker overloads the Control.BackColor

Unfortunatly there is no documentation on that overload. But it does pop up on intellisence. You may just have to set it in the constructor or in InitializeComponent.

-Rick

----------------------
 
hi, i've tried doing it in the new method in my class as below without success..

Public Class BaseDateTimePicker
Inherits System.Windows.Forms.DateTimePicker
Public Sub New()
Me.Format = DateTimePickerFormat.Short
Me.Width = 88
Me.FontHeight = 25
Me.BackColor = Color.LightCyan
End Sub
End Class

Anybody got any ideas how i do this?

Many thanks, Berny...




 
Okay, got it to work, but it's a bit of work.

First, uber thanks to Claudio Ganahl who actually made the working C# code (My C is getting really rusty). Second, if someone could translate the C# code to VB, that would rock. ATM, I don't have the time to beat on it more.

So, start up a fresh copy of Visual Studio .Net. Create a C# class library project. Call it "DateTimePicker2" or something along those lines.

In the class file, paste the following:

Code:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;



namespace DateTimePicker2
{
	public class DateTimePicker2 : DateTimePicker
	{
		private Color _BackColor = SystemColors.Window;
		public override Color BackColor
		{
			get
			{
				return _BackColor;
			}
			set
			{
				_BackColor = value;
				Invalidate();
			}
		}

		protected override void WndProc(ref Message m)
		{
			if (m.Msg == (int) 0x0014 /* WM_ERASEBKGND */)
			{
				Graphics g = Graphics.FromHdc(m.WParam);
				g.FillRectangle(new SolidBrush(_BackColor), 
					ClientRectangle);
				g.Dispose();
				return;
			}
			base.WndProc(ref m);
		}
	}
}

you'll get a bunch of unknown type errors. We need to add the references. The cheap way is to add a windows form to the project, and then delete it.

Build the project. Copy the DateTimePicker2.dll file that is created and copy it to your application's directory.

In your application, add a .Net reference to the DateTimePicker2.dll. Add a DateTimePicker to your form. go into the windows generated code and change the System.Windows.Form.DateTimePicker reference to DateTimePicker2.DateTimePicker2. There are 2 of these (one for declaring, the other for instantiating).

Voalia! A date time picker w/ back color.

-Rick





----------------------
 
Hi, that works great....

I've converted the code to vb.net and it works fine...

I didn't have to go into the windows generated code and change any references just dropping it on the form worked great...

Many thanks,

here's the vb version......
Code:
Public Class BaseDateTimePicker
    Inherits DateTimePicker
    Private _BackColor As Color = SystemColors.Window

    Public Sub New()
        MyBase.New()
        Me.Format = DateTimePickerFormat.Short
        Me.Width = 88
        Me.FontHeight = 25
        Me.BackColor = Color.LightCyan
    End Sub

    Public Overrides Property BackColor() As Color
        Get
            Return _BackColor
        End Get
        Set(ByVal Value As Color)
            _BackColor = Value
            Invalidate()
        End Set
    End Property

    Protected Overrides Sub WndProc(ByRef m As Message)
        If m.Msg = CInt(&H14) Then ' WM_ERASEBKGND 
            Dim g As Graphics = Graphics.FromHdc(m.WParam)
            g.FillRectangle(New SolidBrush(_BackColor), ClientRectangle)
            g.Dispose()
            Return
        End If
        MyBase.WndProc(m)
    End Sub
End Class
 
I played around for hours with this and stumbled onto a low-tech solution. Since you can't modify the backcolor of the datetimepicker, I just dynamically create a textbox at runtime and set all of his properties to the corresponding properties on the datetimepicker. I then disable and hide the datetimepicker. I also set the ReadOnly property of the textbox to True and reset his backcolor.

This only works if you are trying to display the value and won't allow you to change it, but I assume the whole reason you are doing this is because you have disabled the control and hate the ugly gray!

Here's my code:

objDateTimePicker.Enabled = False
objDateTimePicker.Visible = False
Dim txtTest As New TextBox
'set all of the properties of the new textbox to match the datetimepicker control...
txtTest.Top = objDateTimePicker.Top
txtTest.Left = objDateTimePicker.Left
txtTest.Height = objDateTimePicker.Height
txtTest.Width = objDateTimePicker.Width
txtTest.Parent = objDateTimePicker.Parent
Call SetupBoundDateOnlyTextbox(txtTest, CType(objDateTimePicker.DataBindings.Item(0).DataSource, DataSet), objDateTimePicker.DataBindings.Item(0).BindingMemberInfo.BindingMember.ToString)
txtTest.ReadOnly = True
txtTest.BackColor = System.Drawing.SystemColors.Window

Hope this helps,
Rich
 
Thanks Guys for both these solutions...

It's difficult to believe that, something, on the face of it, that appears so simple you have to jump through so many hoops to achieve...

This is especially true when you think about how much .net is being pushed as a fully OOP'ed system....

Again, Many thanks for the time you have spent on this..

Bernard
 
It is fully OO, that's why we can make a custom datetimepicker that inherits from the original datetimepicker and have a back color. The problem isn't with the object orientated style, but with the implimintation of a control that does not follow the same standards as other controls. We've added the above listed class to our genlib here at work.

-Rick

----------------------
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top