Can anyone know how to make a readonly datetime picker in VB.NET?

Recommended Answers

All 5 Replies

How can the user pick the date and time if it's read only? You need some clarification there. If you want to display the date and time as read only use a label and the built in tostring functions of the datetime class.

that's confusing..

If you want to display the date and time as read only use a label and the built in tostring functions of the datetime class.
yeah, just do this instead

I guess if you you have it formatted to a short date format, then allowing the user to invoke the dropdown to show the MonthCalndar may be of some use.

Anyways, making a readonly DTP control is fairly easy.

Public Class ReadOnlyDTP
   Inherits DateTimePicker
   Public Shadows Property Value() As DateTime
      Get
         Return MyBase.Value
      End Get
      Set(ByVal value As DateTime)
         ' Need to set values in correct order 
         ' to prevent internal validation error
         Select Case value
            Case Is < MyBase.Value
               MyBase.MinDate = value
               MyBase.Value = value
               MyBase.MaxDate = value
            Case Is > MyBase.Value
               MyBase.MaxDate = value
               MyBase.Value = value
               MyBase.MinDate = value
         End Select
      End Set
   End Property 'Value

   Const WM_Char As Int32 = &H102
   Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
      ' prevent keyboard changes to the fields
      If m.Msg = WM_Char Then Exit Sub
      MyBase.WndProc(m)
   End Sub
End Class

Add this to your project, build the project and it should showup in your ToolBox if your VS is configured to do so (it is the default setting).

commented: I've been wanting just that trick for something else. Thanks. +12

then allowing the user to invoke the dropdown to show the MonthCalndar may be of some use

Yeah I can see that. You could use the ValueChanged event to reset the value to what you need, and just ignore the users input.

hello !

you can simple disable the datetimepicker like this

Me.dateTimePicker1.Enabled = false

Regards

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.