Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing text color in a DTPicker control

Tags:

colors

vb6

In VB6, I have a DTPicker control on a form. (The DTPicker is the calendar date/time selector, included in Microsoft Windows Common Controls-2 6.0, available from the Components dialog.)

While there are many properties to affect the colors of the calendar when it's dropped down, there is no property that allows changing the color of the date that's displayed in the textbox. I'm looking for something like the standard TextBox's ForeColor property.

Does anyone have a little API magic to allow me to simulate that property?

like image 314
JeffK Avatar asked Nov 06 '22 22:11

JeffK


2 Answers

I hate to post something that is not really helpful, but this appears to be something beyond the scope of what Microsoft intended developers to do with the control. While there must certainly be an API call to set the color (Windows certainly knows to paint it black when enabled and gray when disabled), the method to do so escapes me.

My recommendation, should no one else respond with how to do what you need, is to either obtain a new DateTime Picker control with the needed properties (it would seem that there are a few 3rd party options), or "roll your own" control.

FWIW, this same issue exists in VB.NET with the exception being that Microsoft specifically overrides (and then hides) the ForeColor (and BackColor) properties inherited from the generic Control object to do nothing.

like image 136
smbarbour Avatar answered Nov 13 '22 05:11

smbarbour


I'm going to address two issues with the DatePicker object and a workaround for them.

  1. You cannot put a blank value into DatePicker, which lead me to the 2nd problem.
  2. You cannot change the font color to at least make it appear blank.

To keep the functionality of the DatePicker AND gain the ability to have a blank value and your regular font formatting options (colors, etc), I used two objects. First make a DTP object and set the width so that you can only really see the drop down arrow. For me this was 15. Then make a regular TextBox that is wide enough to hold your date. Put the DTP arrow directly to the right (or left) of the text box. Then you simply add code to the Change event of the DTP to copy its .Value into the .Text of the TextBox like so:

Private Sub MyDTP_Change()    
    MyUserForm.MyDateTextBox.Text = MyUserForm.MyDTP.Value
End Sub

Then have any data references you need access the MyDateTextBox.Text instead of the MyDTP.Value and presto! You get the functionality of the DTP with the formatting control of a regular TextBox.

EDIT: Sorry JeffK, I wasn't working with VB in a production environment 9 years ago. :) I would like to add the other side of the functionality to this as well. This allows 2-way syncing between the TextBox and the DTP. IE: Manually enter a date into the TextBox and the DTP Calendar follows. If the TextBox is blank or has an invalid date, the DTP defaults to today's date.

Private Sub MyDateTextBox_Change()    
    If MyUserForm.MyDateTextBox.Text <> "" And 
    IsDate(MyUserForm.MyDateTextBox.Text) = True Then
        If CDate(MyUserForm.MyDateTextBox.Text) <= MyUserForm.MyDPT.MaxDate And _
        CDate(MyUserForm.MyDateTextBox.Text) >= MyUserForm.MyDPT.MinDate Then
            MyUserForm.MyDTP.Value = MyUserForm.MyDateTextBox.Text
        Else
            MyUserForm.MyDTP.Value = Date
        End If
    Else
        MyUserForm.MyDTP.Value = Date
    End If
End Sub
like image 38
SM177Y Avatar answered Nov 13 '22 04:11

SM177Y