6 Replies - 578 Views - Last Post: 05 February 2012 - 05:11 AM Rate Topic: -----

Topic Sponsor:

#1 Minooch  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 04-February 12

Readonly DateTimePicker. Change backcolor and disable calender

Posted 04 February 2012 - 11:19 PM

Hi guys,

This is my first post, so please forgive me if I am not doing this correctly.

I have the following code that sets a DateTimePicker as Readonly (and it works well). But to give the full effect of being readonly, I would like to implement the ability to change the Backcolor and disable the ability to use the calender.

Does anyone know how to change the Backcolor and disable the Calender?


Public Class ReadOnlyDTP
    Inherits DateTimePicker

    'This hash table stores all the controls extended by this extender provider
    Friend htProvidedProperties As New Hashtable


#Region "Extensible Properties"

    Private Class DateTimePickerProperties
        Public IsReadOnly As Boolean = False
    End Class

    <Category("Read Only DateTimePicker Provider")> _
    Sub SetReadOnly(ByVal ctrl As Control, ByVal value As Boolean)
        Dim dtpSender As DateTimePicker = CType(ctrl, DateTimePicker)
        GetControlFromHashtable(ctrl).IsReadOnly = value
    End Sub

    <Category("Read Only DateTimePicker Provider")> _
    Function GetReadOnly(ByVal ctrl As Control) As Boolean
        Return GetControlFromHashtable(ctrl).IsReadOnly
    End Function

#End Region

#Region "behavior"

    Private Function GetControlFromHashtable(ByVal ctrl As Control) As DateTimePickerProperties

        If htProvidedProperties.Contains(ctrl) Then
            Return DirectCast(htProvidedProperties(ctrl), DateTimePickerProperties)
        Else
            'Add Event Handlers as the control is added to hash table
            AddHandler ctrl.KeyPress, AddressOf KeyPressHandler

            Dim ProvidedProperties As New DateTimePickerProperties
            htProvidedProperties.Add(ctrl, ProvidedProperties)
            Return ProvidedProperties
        End If
    End Function

    Private Sub KeyPressHandler(ByVal sender As Object, ByVal e As KeyPressEventArgs)
        Dim dtpSender As DateTimePicker = CType(sender, DateTimePicker)

        If GetControlFromHashtable(dtpSender).IsReadOnly = True Then
            e.Handled = True
        Else
            e.Handled = False
        End If
    End Sub

#End Region



End Class




Is This A Good Question/Topic? 0
  • +

Replies To: Readonly DateTimePicker. Change backcolor and disable calender

#2 nK0de  Icon User is offline

  • can't spell BITCH without IT
  • member icon

Reputation: 183
  • View blog
  • Posts: 732
  • Joined: 21-December 11

Re: Readonly DateTimePicker. Change backcolor and disable calender

Posted 04 February 2012 - 11:44 PM

I'm not sure about changing the backcolor of a DateTimePicker but do you really have to go through all this to disable a DateTimePicker? Just the .Enabled property of the DateTimePicker to False.

DateTimePicker1.Enabled = False


Or did I take what you're trying to do in a wrong way?

This post has been edited by nK0de: 04 February 2012 - 11:53 PM

Was This Post Helpful? 0
  • +
  • -

#3 Minooch  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 04-February 12

Re: Readonly DateTimePicker. Change backcolor and disable calender

Posted 04 February 2012 - 11:51 PM

View PostnK0de, on 04 February 2012 - 11:44 PM, said:

I'm not sure of you can change the backcolor of a DateTimePicker but do you really have to go through all this to disable a DateTimePicker? Just the .Enabled property of the DateTimePicker to False.

DateTimePicker1.Enabled = False


Or did I take what you're trying to do in a wrong way?


Thanks nK0de,

I'd like our users to still have the ability to copy the text from the DateTimePickers. Setting the enabled property does not allow the user to get focus and copy the value.
Was This Post Helpful? 0
  • +
  • -

#4 nK0de  Icon User is offline

  • can't spell BITCH without IT
  • member icon

Reputation: 183
  • View blog
  • Posts: 732
  • Joined: 21-December 11

Re: Readonly DateTimePicker. Change backcolor and disable calender

Posted 05 February 2012 - 12:10 AM

View PostMinooch, on 05 February 2012 - 12:21 PM, said:

Can you actually copy the text from a DateTimePicker? like from a Textbox? 'Cause I just tried that but it only gets highlighted and doesn't get copied to the clipboard either. How are you able to


Can you actually copy the text from a DateTimePicker? like from a Textbox? 'Cause I just tried that but it only gets highlighted and doesn't get copied to the clipboard either. How are you able to do this?
Was This Post Helpful? 1
  • +
  • -

#5 Minooch  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 04-February 12

Re: Readonly DateTimePicker. Change backcolor and disable calender

Posted 05 February 2012 - 12:39 AM

View PostnK0de, on 05 February 2012 - 12:10 AM, said:

View PostMinooch, on 05 February 2012 - 12:21 PM, said:

Can you actually copy the text from a DateTimePicker? like from a Textbox? 'Cause I just tried that but it only gets highlighted and doesn't get copied to the clipboard either. How are you able to do this?


You are right. I don't know how I never noticed that before.
I guess setting the Enabled property to False would be the easiest solution.


Can you actually copy the text from a DateTimePicker? like from a Textbox? 'Cause I just tried that but it only gets highlighted and doesn't get copied to the clipboard either. How are you able to do this?
[/quote]

You are right. I don't know how I never noticed that before.
I guess setting the Enabled property to False would be the easiest solution.
Thanks
Was This Post Helpful? 0
  • +
  • -

#6 nK0de  Icon User is offline

  • can't spell BITCH without IT
  • member icon

Reputation: 183
  • View blog
  • Posts: 732
  • Joined: 21-December 11

Re: Readonly DateTimePicker. Change backcolor and disable calender

Posted 05 February 2012 - 12:53 AM

well, if it helps at all, you can just put a Textbox and assign the value from the DateTimePicker to it so that the user can copy it from there.
Was This Post Helpful? 0
  • +
  • -

#7 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 960
  • View blog
  • Posts: 3,354
  • Joined: 25-September 09

Re: Readonly DateTimePicker. Change backcolor and disable calender

Posted 05 February 2012 - 05:11 AM

If it's nothing more than displaying the date without allowing the user to make any changes, why not just use a textbox and disable it. If you're not wanting a calendar or the ability to enter, then a datetimepicker is overkill for just displaying a date.

EDIT:
You could also just use a label and in its click event, copy it's text to the clipboard. Then the user could just click on the date and they have it copied.

Clipboard.SetText(lblDate.Text)

This post has been edited by CharlieMay: 05 February 2012 - 05:18 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1