none
DateTimePicker backcolor RRS feed

  • Question

  • Hi All

    I'm trying to change a DateTimePicker backcolor using a code I found on forums

    Imports System.Windows.Forms
    Imports System.Drawing
    Imports System.ComponentModel
    Namespace DateTimePickerWithBackColor
        Public Class MyDateTimePicker
            Inherits DateTimePicker
            <Browsable(True)>
            Public Overrides Property BackColor() As Color
                Get
                    Return MyBase.BackColor
                End Get
                Set
                    MyBase.BackColor = Value
                End Set
            End Property
    
            Protected Overrides Sub WndProc(ByRef m As Message)
                Dim WM_ERASEBKGND As Integer = &H14
                If (m.Msg = WM_ERASEBKGND) Then
                    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
    End Namespace

    I use in a simple winform where there is a Button and the MyDatePickerControl

    Nothing else

      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            DateTimePicker1.BackColor = Color.Yellow
            DateTimePicker1.Invalidate()
        End Sub

    When I click on the button the DateTimePicker back color do not change and I spent hours to understand why

    Can you help Me ?

    Tuesday, February 26, 2019 10:19 AM

Answers

  • No, I want to change just the DatetimePicker text

    A way is to override WM_PAINT, get the original bitmap with WM_PRINT, change the background color, draw the result on the original DC.
    I tested with TransparentBlt to change the color on Windows 10 :

    • Marked as answer by Claudio111 Sunday, March 3, 2019 7:48 AM
    Wednesday, February 27, 2019 2:04 AM

All replies

  • If you want to change colors of the MonthCal part of DTP, it can be done by disabling the Visual Style, like in this thread
    • Edited by Castorix31 Tuesday, February 26, 2019 11:20 AM
    Tuesday, February 26, 2019 11:18 AM
  • No, I want to change just the DatetimePicker text
    Tuesday, February 26, 2019 11:28 AM
  • I use this ,

    Works great

    https://social.msdn.microsoft.com/Forums/windowsdesktop/en-US/0f8e2dfb-98cd-4676-82f5-097f3165e60b/datetimepickerforecolor?forum=vbgeneral


    • Edited by KeesBlunder Tuesday, February 26, 2019 12:28 PM
    Tuesday, February 26, 2019 12:27 PM
  • HI Karen

    Did you try my code or the code in your link?

    Tuesday, February 26, 2019 2:14 PM
  • For example , i made this just now

    https://social.msdn.microsoft.com/Forums/windowsdesktop/en-US/0f8e2dfb-98cd-4676-82f5-097f3165e60b/datetimepickerforecolor?forum=vbgeneral

    Tuesday, February 26, 2019 2:24 PM
  • Sorry but this is not what I need since a) I need to change background color runtime in a particular event and b) this does not allow to user to use keybord to change date. :-((((

    thanks anyway

    Tuesday, February 26, 2019 5:16 PM
  • No, I want to change just the DatetimePicker text

    A way is to override WM_PAINT, get the original bitmap with WM_PRINT, change the background color, draw the result on the original DC.
    I tested with TransparentBlt to change the color on Windows 10 :

    • Marked as answer by Claudio111 Sunday, March 3, 2019 7:48 AM
    Wednesday, February 27, 2019 2:04 AM