How to highlight specific portion within DateTimePicker Programmatically?

Viewed 106

I have a DateTimePicker control (custom format) onto my windows forms application that I am generating programmatically.

To highlight a specific portion / part (e.g. Hour) within it, I have to manually click on it. It is required that I be able to perform this highlighting on a specific part of the DateTimePicker programmatically (and not by manual click).

I have tried using the events, methods and other properties of the DateTimePicker control. I also have searched and re-searched the internet and came across no direction.

Here is a screenshot for illustration:

enter image description here

Over here, onto the cell of the DataGridView, lies my custom format DateTimePicker.

1 Answers

I have finally discovered a solution. I would want to share it so that people having similar issues in future may benefit from it.

The cause of DateTimePicker (DTP) to loose it's usual behavior was because:

  • It was being generated programatically and "Added" as a control to the GridView (instead of form)

Hence the regular methods like "Select()" and "Focus()" were failing to be applied.

I simply placed the DTP control onto the form and made invisible. Then whenever I needed to edit a cell, the control overlay-ed the cell and focus was shifted to it. Like normal DTP control, now it automatically highlighted the first part i.e. hour in my case.

This is the essence of the solution. Rest is handling of unexpected occurrences. I cannot share code due to it being a product of some client.

Related