Can I change the time format of the System DateTimePicker in the Compact Framework? Yes, you can use multipel formats in the control.
First, let me give credit, where credit is due. Most of the code in this article came from an article on DevBuzz.Com. This was not my code; I have simply made a couple of minor modifications to the calling Form to improve the operation of the code.
The form shown, in Figure 1, is shown exactly as I retrieved it from DevBuzz, except that I removed the DevBuzz Logo. Additionally, I have adjusted the width of the two panels on the form for two reasons. First, for some reason unknown to me, the first panel, which becomes the framework for displaying the results of the DateTimePicker, was showing two extraneous boxes to the right of the date and time. I believe the two characters to be character representations of two unknown characters being returned from the setup of the first call to the System APIs. Strangely enough, they never appeared in the second instance of the control. Since I could not determine the cause of this anamoly, I simply shortened the width of the two panels so that they are just wide enough to display the desired date and time format, thereby hiding the extraneous characters.
Figure 1 - Using the DateTimePicker in the Compact Framework.

The picture in Figure 1, shows the result of clicking the arrow in the control. Figure 2, below, shows that you can select any field within the control, month, year, etc., and they you have several was of manipulating the contents of the selected field, such as the toggle control on the device and the InputPanel, etc.
Figure 2 - Changing the contents of fields in the DateTimePicker.

In the Form_Load event of the test form, I made a couple of changes, just to change the format from 24 hour time to use 12 hour time. I have bolded the changed lines in the code, which is displayed below. You can see the code that was in the DevBuzz example.
| Private Sub Form1_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load Me.Text = "Form1" 'Set the text of the panel to a unique string 'so that the Load_DTPicker routines 'can find the control on this form. pnlStartDate.Text = "pnlStartDate" Form_hWnd = FindWindow(Nothing, MeText.ToCharArray) pStartDate_hWnd = Load_DTPicker(Form_hWnd, pnlStartDate.Text, _ pnlStartDate.Size.Width, pnlStartDate.Size.Height) If IntPtr.Zero.Equals(pStartDate_hWnd) Then MessageBox.Show("DateTimePicker control couldn't be created") Else SetDTP_Format(pStartDate_hWnd, "MM/dd/yyyy hh:mm tt") 'Set Date to today's date ' was Now.ToShortDateString) DTPicker_SetDate(pStartDate_hWnd, Format(Now, "MM/dd/yyyy hh:mm tt")) End If pEndDate_hWnd = Load_DTPicker(Form_hWnd, pnlEndDate.Text, _ pnlEndDate.Size.Width, pnlEndDate.Size.Height) If IntPtr.Zero.Equals(pEndDate_hWnd) Then MessageBox.Show("End DateTime Picker control couldn't be created") Else ' was hh:mm SetDTP_Format(pEndDate_hWnd, "MM/dd/yyyy hh:mm tt") ' Set Date to today's date ' Now.ToShortDateString) DTPicker_SetDate(pEndDate_hWnd, Format(Now, "MM/dd/yyyy hh:mm tt")) End If End Sub |