none
Change Background color & Font of DateTimePicker

    Question

  • Dear Friends,

    I want to change the Forecolor and the Font color of the DateTimePicker. (The one Narrow white window with Black font which appears on dragging "D.T.Picker" control onto the "Form").

    So far, I couldn't do it.

    I found the same question (by someone on 31st Aug 2007) and answers by Figo Fei in the forum.
    Figo fei said, "It is truly not possible to directly modify the forecolor of the DateTimePicker which is concealed in its implementation. However, there is a workaround to change its forecolor through own-draw mode in a custom control inherited from dateTimePicker.". And he gave the code which looks like - for me - either VC++ or VC#.

    Code Snippet
    
    public partial class MyDateTimePicker : DateTimePicker
    
        {
    
            protected override void OnPaint(PaintEventArgs e)
    
            {
    
                base.OnPaint(e);
    
                e.Graphics.FillRectangle(new SolidBrush(Color.Red), this.ClientRectangle);
    
                e.Graphics.DrawString(this.Text, this.Font, new SolidBrush(Color.Blue), 0, 0);
    
                e.Graphics.DrawImage(Properties.Resources.DropDownTriangle, new Point(this.ClientRectangle.X + this.ClientRectangle.Width - 16, this.ClientRectangle.Y));
    
            }
    
     
    
            public MyDateTimePicker()
    
            {
    
                this.SetStyle(ControlStyles.UserPaint, true);
    
                InitializeComponent();
    
            }
    
        }



    What I want to know whether it is still impossible to modify the forecolor in MS Visual Studio 2008 or in MS VS 2010?

    So far I've not tried to convert this code to VB.Net, but I'll try.

    Can you tell me if there is any direct technique to modify it?

    Thanks
    Kind regards

    VKSBK




     
    A Real Novice Programmer !
    Monday, February 15, 2010 3:01 PM

Answers

  • Dear Friends,

    I want to change the Forecolor and the Font color of the DateTimePicker. (The one Narrow white window with Black font which appears on dragging "D.T.Picker" control onto the "Form").

    So far, I couldn't do it.

    I found the same question (by someone on 31st Aug 2007) and answers by Figo Fei in the forum.
    Figo fei said, "It is truly not possible to directly modify the forecolor of the DateTimePicker which is concealed in its implementation. However, there is a workaround to change its forecolor through own-draw mode in a custom control inherited from dateTimePicker.". And he gave the code which looks like - for me - either VC++ or VC#.

    Code Snippet
    
    public partial class MyDateTimePicker : DateTimePicker
    
        {
    
            protected override void OnPaint(PaintEventArgs e)
    
            {
    
                base.OnPaint(e);
    
                e.Graphics.FillRectangle(new SolidBrush(Color.Red), this.ClientRectangle);
    
                e.Graphics.DrawString(this.Text, this.Font, new SolidBrush(Color.Blue), 0, 0);
    
                e.Graphics.DrawImage(Properties.Resources.DropDownTriangle, new Point(this.ClientRectangle.X + this.ClientRectangle.Width - 16, this.ClientRectangle.Y));
    
            }
    
     
    
            public MyDateTimePicker()
    
            {
    
                this.SetStyle(ControlStyles.UserPaint, true);
    
                InitializeComponent();
    
            }
    
        }



    What I want to know whether it is still impossible to modify the forecolor in MS Visual Studio 2008 or in MS VS 2010?

    So far I've not tried to convert this code to VB.Net, but I'll try.

    Can you tell me if there is any direct technique to modify it?

    Thanks
    Kind regards

    VKSBK




     
    A Real Novice Programmer !

    It's C# code....here's a website that will do the conversion from C# to VB.NET for you:

    http://www.developerfusion.com/tools/convert/csharp-to-vb/

    :)


    Doug

    SEARCH ... then ask
    • Marked as answer by V.K.S.B.K Tuesday, February 16, 2010 4:25 AM
    Monday, February 15, 2010 3:43 PM
  • Hi V.K.S.B.K,

    If you change the FONT it appears it only changes the FONT before you drop-down the DateTimePicker.

    Are you wanting to change the FONT in the drop-down view?


    I tried a manual translation based on what I know about .Net but I have trouble with this line.>>

    e.Graphics.DrawImage(Properties.Resources.DropDownTriangle, New Point(Me.ClientRectangle.X + Me.ClientRectangle.Width - 16, Me.ClientRectangle.Y))

    In particular.>>
    Properties.Resources.DropDownTriangle

    So instead I have this code.


    1) Select ADD CLASS from the PROJECT menu, type in the name box MyDateTimePicker.Vb then click on ADD .

    2) Then PASTE the following code in please.

    3) Then select BUILD from the BUILD menu. The new control will appear in your TOOLBOX. :-)

    4) Drag one onto your FORM then hit the F4 key and you will see 4 new properties you can change.

    TriangleLineColor , TriangleLineWidth which refer to the drop down triangle and the InitialColor for the MyDateTimePicker .

    The 4th property you can change is the FontColor .

    Public Class MyDateTimePicker
        Inherits System.Windows.Forms.DateTimePicker
    
        Private mTriangleLineColor As Color = Color.Blue
        Public Property TriangleLineColor() As Color
            Get
                Return Me.mTriangleLineColor
            End Get
            Set(ByVal value As Color)
                Me.mTriangleLineColor = value
                Me.Refresh()
            End Set
        End Property
    
        Private mTriangleLineWidth As Integer = 2
        Public Property TriangleLineWidth() As Integer
            Get
                Return Me.mTriangleLineWidth
            End Get
            Set(ByVal value As Integer)
                Me.mTriangleLineWidth = value
                Me.Refresh()
            End Set
        End Property
    
        Private mInitialColor As Color = Color.LightBlue
        Public Property InitialColor() As Color
            Get
                Return Me.mInitialColor
            End Get
            Set(ByVal value As Color)
                Me.mInitialColor = value
                Me.Refresh()
            End Set
        End Property
    
        Private mFontColor As Color = Color.Blue
        Public Property FontColor() As Color
            Get
                Return Me.mFontColor
            End Get
            Set(ByVal value As Color)
                Me.mFontColor = value
                Me.Refresh()
            End Set
        End Property
    
        Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
            MyBase.OnPaint(e)
            e.Graphics.FillRectangle(New SolidBrush(mInitialColor), 0, 0, Me.Width - 16, Me.Height)
            e.Graphics.DrawString(Me.Text, Me.Font, New SolidBrush(FontColor), 0, 0)
    
            e.Graphics.DrawLine(New Pen(Me.mTriangleLineColor, mTriangleLineWidth), Me.Width - 16, 0, Me.Width - 8, Me.Height)
            e.Graphics.DrawLine(New Pen(Me.mTriangleLineColor, mTriangleLineWidth), Me.Width - 8, Me.Height, Me.Width, 0)
    
        End Sub
    
        Public Sub New()
            Me.SetStyle(ControlStyles.UserPaint, True)
        End Sub
    
    End Class



    Regards,

    John


    By default MyDateTimePicker looks like this.>>



    • Edited by John Anthony Oliver Tuesday, February 16, 2010 12:11 AM Edited as a result of this thread.>> http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/4e0f2206-5c3d-4e20-9dc0-3534832756e0
    • Marked as answer by V.K.S.B.K Tuesday, February 16, 2010 4:24 AM
    Monday, February 15, 2010 5:32 PM

  • How did you paste the picture here?

    I wanted to paste the picture of that window  (The one you showed here) - to make my question easily understood - so, I took a screen-shot of the Form with DTPicker and pasted that in the "Paint" then cut the DTPicker window part only and tried to paste that in the Page. I couldn't paste that on this page.
    I don't know why?
    I even tried to paste it in the "Insert Code Block" window also, failed!!

    Can you explain how did you do that?

    Thanks
    Regards
    VKSBK
    A Real Novice Programmer !

    Hi again V.K.B.S.K_,

    Please see my 3rd post in this thread which is still a sticky thread at the moment.>>

    http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/33bf2119-5f56-40b2-a689-d437ec09e550


    You could read it all if you want to. LOL!!  It has had over 7000 views!!    :-D    ;-)


    Regards,

    John

    • Marked as answer by V.K.S.B.K Tuesday, February 16, 2010 8:14 AM
    Tuesday, February 16, 2010 4:47 AM

All replies

  • Dear Friends,

    I want to change the Forecolor and the Font color of the DateTimePicker. (The one Narrow white window with Black font which appears on dragging "D.T.Picker" control onto the "Form").

    So far, I couldn't do it.

    I found the same question (by someone on 31st Aug 2007) and answers by Figo Fei in the forum.
    Figo fei said, "It is truly not possible to directly modify the forecolor of the DateTimePicker which is concealed in its implementation. However, there is a workaround to change its forecolor through own-draw mode in a custom control inherited from dateTimePicker.". And he gave the code which looks like - for me - either VC++ or VC#.

    Code Snippet
    
    public partial class MyDateTimePicker : DateTimePicker
    
        {
    
            protected override void OnPaint(PaintEventArgs e)
    
            {
    
                base.OnPaint(e);
    
                e.Graphics.FillRectangle(new SolidBrush(Color.Red), this.ClientRectangle);
    
                e.Graphics.DrawString(this.Text, this.Font, new SolidBrush(Color.Blue), 0, 0);
    
                e.Graphics.DrawImage(Properties.Resources.DropDownTriangle, new Point(this.ClientRectangle.X + this.ClientRectangle.Width - 16, this.ClientRectangle.Y));
    
            }
    
     
    
            public MyDateTimePicker()
    
            {
    
                this.SetStyle(ControlStyles.UserPaint, true);
    
                InitializeComponent();
    
            }
    
        }



    What I want to know whether it is still impossible to modify the forecolor in MS Visual Studio 2008 or in MS VS 2010?

    So far I've not tried to convert this code to VB.Net, but I'll try.

    Can you tell me if there is any direct technique to modify it?

    Thanks
    Kind regards

    VKSBK




     
    A Real Novice Programmer !

    It's C# code....here's a website that will do the conversion from C# to VB.NET for you:

    http://www.developerfusion.com/tools/convert/csharp-to-vb/

    :)


    Doug

    SEARCH ... then ask
    • Marked as answer by V.K.S.B.K Tuesday, February 16, 2010 4:25 AM
    Monday, February 15, 2010 3:43 PM
  • Hi V.K.S.B.K,

    If you change the FONT it appears it only changes the FONT before you drop-down the DateTimePicker.

    Are you wanting to change the FONT in the drop-down view?


    I tried a manual translation based on what I know about .Net but I have trouble with this line.>>

    e.Graphics.DrawImage(Properties.Resources.DropDownTriangle, New Point(Me.ClientRectangle.X + Me.ClientRectangle.Width - 16, Me.ClientRectangle.Y))

    In particular.>>
    Properties.Resources.DropDownTriangle

    So instead I have this code.


    1) Select ADD CLASS from the PROJECT menu, type in the name box MyDateTimePicker.Vb then click on ADD .

    2) Then PASTE the following code in please.

    3) Then select BUILD from the BUILD menu. The new control will appear in your TOOLBOX. :-)

    4) Drag one onto your FORM then hit the F4 key and you will see 4 new properties you can change.

    TriangleLineColor , TriangleLineWidth which refer to the drop down triangle and the InitialColor for the MyDateTimePicker .

    The 4th property you can change is the FontColor .

    Public Class MyDateTimePicker
        Inherits System.Windows.Forms.DateTimePicker
    
        Private mTriangleLineColor As Color = Color.Blue
        Public Property TriangleLineColor() As Color
            Get
                Return Me.mTriangleLineColor
            End Get
            Set(ByVal value As Color)
                Me.mTriangleLineColor = value
                Me.Refresh()
            End Set
        End Property
    
        Private mTriangleLineWidth As Integer = 2
        Public Property TriangleLineWidth() As Integer
            Get
                Return Me.mTriangleLineWidth
            End Get
            Set(ByVal value As Integer)
                Me.mTriangleLineWidth = value
                Me.Refresh()
            End Set
        End Property
    
        Private mInitialColor As Color = Color.LightBlue
        Public Property InitialColor() As Color
            Get
                Return Me.mInitialColor
            End Get
            Set(ByVal value As Color)
                Me.mInitialColor = value
                Me.Refresh()
            End Set
        End Property
    
        Private mFontColor As Color = Color.Blue
        Public Property FontColor() As Color
            Get
                Return Me.mFontColor
            End Get
            Set(ByVal value As Color)
                Me.mFontColor = value
                Me.Refresh()
            End Set
        End Property
    
        Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
            MyBase.OnPaint(e)
            e.Graphics.FillRectangle(New SolidBrush(mInitialColor), 0, 0, Me.Width - 16, Me.Height)
            e.Graphics.DrawString(Me.Text, Me.Font, New SolidBrush(FontColor), 0, 0)
    
            e.Graphics.DrawLine(New Pen(Me.mTriangleLineColor, mTriangleLineWidth), Me.Width - 16, 0, Me.Width - 8, Me.Height)
            e.Graphics.DrawLine(New Pen(Me.mTriangleLineColor, mTriangleLineWidth), Me.Width - 8, Me.Height, Me.Width, 0)
    
        End Sub
    
        Public Sub New()
            Me.SetStyle(ControlStyles.UserPaint, True)
        End Sub
    
    End Class



    Regards,

    John


    By default MyDateTimePicker looks like this.>>



    • Edited by John Anthony Oliver Tuesday, February 16, 2010 12:11 AM Edited as a result of this thread.>> http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/4e0f2206-5c3d-4e20-9dc0-3534832756e0
    • Marked as answer by V.K.S.B.K Tuesday, February 16, 2010 4:24 AM
    Monday, February 15, 2010 5:32 PM
  • Dear John Anthony Oliver,

    Thanks for the reply.
    Yes, this is what I want to do (What you showed in the picture)

    I wish there was a direct & easy way of doing it like changing the Font in TextBox etc.

    Why can't / didn't Microsoft do it?? Something like their Internet Explorer doesn't have a "Download Manager" which can restart downloading from the point where it got interrupted!!

    How did you paste the picture here?
    I wanted to paste the picture of that window  (The one you showed here) - to make my question easily understood - so, I took a screen-shot of the Form with DTPicker and pasted that in the "Paint" then cut the DTPicker window part only and tried to paste that in the Page. I couldn't paste that on this page.
    I don't know why?
    I even tried to paste it in the "Insert Code Block" window also, failed!!

    Can you explain how did you do that?

    Thanks
    Regards
    VKSBK
    A Real Novice Programmer !
    Tuesday, February 16, 2010 4:24 AM

  • How did you paste the picture here?

    I wanted to paste the picture of that window  (The one you showed here) - to make my question easily understood - so, I took a screen-shot of the Form with DTPicker and pasted that in the "Paint" then cut the DTPicker window part only and tried to paste that in the Page. I couldn't paste that on this page.
    I don't know why?
    I even tried to paste it in the "Insert Code Block" window also, failed!!

    Can you explain how did you do that?

    Thanks
    Regards
    VKSBK
    A Real Novice Programmer !

    Hi again V.K.B.S.K_,

    Please see my 3rd post in this thread which is still a sticky thread at the moment.>>

    http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/33bf2119-5f56-40b2-a689-d437ec09e550


    You could read it all if you want to. LOL!!  It has had over 7000 views!!    :-D    ;-)


    Regards,

    John

    • Marked as answer by V.K.S.B.K Tuesday, February 16, 2010 8:14 AM
    Tuesday, February 16, 2010 4:47 AM
  • Dear John,

    Thanks for the link. Long ago I went through this but could not do - I was not sure which one the correct post so gave up.
    Today I'm trying one.

    Photobucket

    Yes, I did it. Thank you very much.

    Thanks
    Kind regards

    VKSBK
    A Real Novice Programmer ! 
    Tuesday, February 16, 2010 8:03 AM
  • Hi V.K.S.B.K_,

    If you use the DIRECT LINK then hit the HTML in this forum then insert the DIRECT LINK like this.>>

    <img src="http://s1003.photobucket.com/albums/af155/VKSBK/?action=view&current=Trial.jpg"/>

    then the picture is not clickable back to the original source.


    Obviously you need to replace

    http://s1003.photobucket.com/albums/af155/VKSBK/?action=view&current=Trial.jpg


    with the correct picture reference each time.

    I see you have used.>>

    <a href= "http://s1003.photobucket.com/albums/af155/VKSBK/?action=view&amp;current=Trial.jpg"><img src= "http://i1003.photobucket.com/albums/af155/VKSBK/Trial.jpg" border="0" alt="Photobucket" width="275" height="249" /> </a>

    which shows a picture as a link that can be clicked on. Note: You can specify the height or width or both.

    Using.>>

    <img src="http://s1003.photobucket.com/albums/af155/VKSBK/?action=view&current=Trial.jpg"/>

    is much easier and you can still add a border or / and height or / and width before the closing />

    To learn more about HTML see.>> http://w3schools.com/   particularly.>> http://w3schools.com/html/default.asp

    Try a border="5" or a border="10" or some other small number to see the effect. :-)


    Regards,

    John

    Tuesday, February 16, 2010 1:22 PM
  • Dear John Anthony oliver,


    Sorry, I missed your last reply.

    Thanks for it.
    I'll try this.


    Thanks
    regards

    VKSBK.
    A Real Novice Programmer !
    Thursday, March 11, 2010 5:56 PM