2008-10-13 43 views
11

Trong .NET (ít nhất là trong phiên bản 2008 và có thể trong năm 2005), việc thay đổi thuộc tính BackColor của DateTimePicker hoàn toàn không ảnh hưởng đến giao diện. Làm cách nào để thay đổi màu nền của vùng văn bản, không phải của lịch thả xuống?Thay đổi màu nền của một DateTimePicker trong .NET

Chỉnh sửa: Tôi đã nói về các biểu mẫu Windows, chứ không phải ASP.

Trả lời

14

Theo MSDN:

Đặt BackColor không ảnh hưởng đến hình thức của DateTimePicker.

Bạn cần viết điều khiển tùy chỉnh mở rộng DateTimePicker. Ghi đè thuộc tính BackColor và phương pháp WndProc.

Bất cứ khi nào bạn thay đổi BackColor, đừng quên gọi phương thức myDTPicker.Invalidate(). Điều này sẽ buộc điều khiển vẽ lại bằng màu mới được chỉ định.

const int WM_ERASEBKGND = 0x14; 
protected override void WndProc(ref System.Windows.Forms.Message m) 
{ 
    if(m.Msg == WM_ERASEBKGND) 
    { 
     using(var g = Graphics.FromHdc(m.WParam)) 
     { 
      using(var b = new SolidBrush(_backColor)) 
      { 
       g.FillRectangle(b, ClientRectangle); 
      } 
     } 
     return; 
    } 

    base.WndProc(ref m); 
} 
+0

Xin lỗi đã mất quá nhiều thời gian để chấp nhận. Tôi đã hy vọng có những giải pháp khác mà không liên quan đến việc biết giá trị hex, sau đó tôi quên mất điều này. –

+0

Cảm ơn bạn đã đăng bài này. Tôi tùy chỉnh vẽ một điều khiển DateTimePicker và điều này đưa tôi đi đúng hướng. –

+7

Tôi đã thử thay đổi này trong Vista (có và không có kiểu trực quan) và nó không hoạt động. –

2

cho vùng văn bản bạn sẽ phải làm điều đó trên hộp văn bản thực tế mà bạn đang thêm extender vào
cho C# bạn sẽ làm điều đó như thế này:

<asp:TextBox runat="server" ID="txt" BackColor="Aqua" Text="Date"></asp:TextBox> 
+0

Tôi xin lỗi phải mất quá lâu để có được điều này. Tôi nghĩ tôi đã thêm bình luận này rồi. Tôi đã nói về các ứng dụng windows, không phải ứng dụng ASP. –

3

Có một triển khai miễn phí bắt nguồn từ DateTimePicker cho phép bạn thay đổi thuộc tính BackColor về thay đổi.

Xem trang web CodeProject: DateTimePicker with working BackColor

+0

Đó là một phiên bản hoàn toàn lobotomized mà bạn chỉ có thể sử dụng lịch thả xuống để thực sự chọn một ngày, mặc dù. – Nyerguds

1

Dựa trên this project (như được đăng trên) Tôi đã viết lại một lớp tùy chỉnh datepicker (trong VB.NET) cho phép tùy biến màu nền, các Format và hình ảnh nhỏ xuất hiện bên cạnh nút thả xuống.

Eg.1:

enter image description here

Eg.2:

enter image description here

Để làm cho nó làm việc chỉ cần tạo một lớp mới trong dự án của bạn với đoạn mã sau và xây dựng lại các giải pháp . Một điều khiển mới gọi MyDateTimePicker bây giờ sẽ xuất hiện trong danh sách hộp công cụ:

Public Class MyDateTimePicker 
    Inherits System.Windows.Forms.DateTimePicker 
    Private _disabled_back_color As Color 
    Private _image As Image 
    Private _text_color As Color = Color.Black 

    Public Sub New() 
     MyBase.New() 
     Me.SetStyle(ControlStyles.UserPaint, True) 
     _disabled_back_color = Color.FromKnownColor(KnownColor.Control) 
    End Sub 

    ''' <summary> 
    '''  Gets or sets the background color of the control 
    ''' </summary> 
    <Browsable(True)> 
    Public Overrides Property BackColor() As Color 
     Get 
      Return MyBase.BackColor 
     End Get 
     Set 
      MyBase.BackColor = Value 
     End Set 
    End Property 

    ''' <summary> 
    '''  Gets or sets the background color of the control when disabled 
    ''' </summary> 
    <Category("Appearance"), Description("The background color of the component when disabled")> 
    <Browsable(True)> 
    Public Property BackDisabledColor() As Color 
     Get 
      Return _disabled_back_color 
     End Get 
     Set 
      _disabled_back_color = Value 
     End Set 
    End Property 

    ''' <summary> 
    '''  Gets or sets the Image next to the dropdownbutton 
    ''' </summary> 
    <Category("Appearance"), 
    Description("Get or Set the small Image next to the dropdownbutton")> 
    Public Property Image() As Image 
     Get 
      Return _image 
     End Get 
     Set(ByVal Value As Image) 
      _image = Value 
      Invalidate() 
     End Set 
    End Property 

    ''' <summary> 
    '''  Gets or sets the text color when calendar is not visible 
    ''' </summary> 
    <Category("Appearance")> 
    Public Property TextColor As Color 
     Get 
      Return _text_color 
     End Get 
     Set(value As Color) 
      _text_color = value 
     End Set 
    End Property 


    Protected Overrides Sub OnPaint(e As System.Windows.Forms.PaintEventArgs) 
     Dim g As Graphics = Me.CreateGraphics() 
     g.TextRenderingHint = Drawing.Text.TextRenderingHint.ClearTypeGridFit 

     'Dropdownbutton rectangle 
     Dim ddb_rect As New Rectangle(ClientRectangle.Width - 17, 0, 17, ClientRectangle.Height) 
     'Background brush 
     Dim bb As Brush 

     Dim visual_state As ComboBoxState 

     'When enabled the brush is set to Backcolor, 
     'otherwise to color stored in _disabled_back_Color 
     If Me.Enabled Then 
      bb = New SolidBrush(Me.BackColor) 
      visual_state = ComboBoxState.Normal 
     Else 
      bb = New SolidBrush(Me._disabled_back_color) 
      visual_state = ComboBoxState.Disabled 
     End If 

     'Filling the background 
     g.FillRectangle(bb, 0, 0, ClientRectangle.Width, ClientRectangle.Height) 

     'Drawing the datetime text 
     g.DrawString(Me.Text, Me.Font, New SolidBrush(TextColor), 5, 2) 

     'Drawing icon 
     If Not _image Is Nothing Then 
      Dim im_rect As New Rectangle(ClientRectangle.Width - 40, 4, ClientRectangle.Height - 8, ClientRectangle.Height - 8) 
      g.DrawImage(_image, im_rect) 
     End If 

     'Drawing the dropdownbutton using ComboBoxRenderer 
     ComboBoxRenderer.DrawDropDownButton(g, ddb_rect, visual_state) 

     g.Dispose() 
     bb.Dispose() 
    End Sub 
End Class 

* Lưu ý rằng lớp này được đơn giản hóa, vì vậy nó đã hạn chế functionallity

+0

Tác phẩm này, ngay cả trong Windows 7. Nhưng nó thay đổi giao diện của DatetimePicker vì Combobox. Giải pháp tốt đẹp mặc dù. – LuckyLuke82

Các vấn đề liên quan