Change text color in a DTPicker control

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

Despite the fact that many properties affect the colors of the calendar when it is omitted, there is no property that allows you to change the color of the date displayed in the text box. I am looking for something like the standard TextBox ForeColor property.

Does anyone have an API magic allowing me to simulate this property?

+4
source share
2 answers

I don’t like posting something that isn’t very useful, but it seems to go beyond what Microsoft planned for the developers to do with control. Although, of course, there must be an API call to set the color (Windows, of course, knows that it colors it black when it is turned on, and gray when it is turned off), the method for this eludes me.

My recommendation is that no one answers how to do what you need - either get a new DateTime Picker control with the necessary properties (it seems that there are several third-party options), or unscrew your own control.

FWIW, the same problem exists in VB.NET, except that Microsoft intentionally overrides (and then hides) the ForeColor (and BackColor) properties inherited from the common Control object to do nothing.

+1
source

I am going to solve two problems with a DatePicker object and a workaround for them.

  • You cannot put an empty value in a DatePicker, which will lead me to the second problem.
  • You cannot change the font color to at least make it blank.

To preserve the functionality of DatePicker and to be able to have an empty value and your usual font formatting options (colors, etc.), I used two objects. First create a DTP object and set the width so that you can only see the down arrow. For me it was 15. Then make a regular TextBox that is big enough to hold your date. Place the DTP arrow right to the right (or left) of the text box. Then you simply add the code to the DTP change event to copy it .Value to .Text TextBox, for example:

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

Then you have links to the data you need to access MyDateTextBox.Text, not MyDTP.Value and presto! You get DTP functionality with a regular TextBox formatting control.

EDIT: Sorry JeffK, I did not work with VB in the production environment 9 years ago. :) I would like to add the other side of functionality to this. This allows two-way synchronization between TextBox and DTP. IE: Manually enter the date in the TextBox and then select DTP Calendar. If the TextBox is empty or has an invalid date, 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 
0
source

All Articles