TheBugSlayer
Programmer
Dear fellows,
I have been running into this program since morning. I am developing a component which derives from UserControl and includes a MaskedTextBox and a DateTimePicker. It was all working fine until earlier today when I added two new properties and rebuilt the component. From that moment on, when I tried to drop the component on a form it fails with error "Failed do create component 'NullableDateTime'. The error message follows: 'System.InvalidCastException: Conversion from string 'NullableDateTime1' to 'Date' is not valid...". Please refer to attached image file for complete error description. Oops, sorry, company prohibits uploads...hopefully that is descriptive enough.
Below is the complete code of my Windows Class Library in NullableDateTime.vb:
Suggestions ARE welcome. Been stuck on this component for three days...deadline approaching. You may uncomment the two properties, it doesn't make a difference. Thanks!
I have been running into this program since morning. I am developing a component which derives from UserControl and includes a MaskedTextBox and a DateTimePicker. It was all working fine until earlier today when I added two new properties and rebuilt the component. From that moment on, when I tried to drop the component on a form it fails with error "Failed do create component 'NullableDateTime'. The error message follows: 'System.InvalidCastException: Conversion from string 'NullableDateTime1' to 'Date' is not valid...". Please refer to attached image file for complete error description. Oops, sorry, company prohibits uploads...hopefully that is descriptive enough.
Below is the complete code of my Windows Class Library in NullableDateTime.vb:
Code:
Imports System.ComponentModel
Public Class NullableDateTime
Inherits System.Windows.Forms.UserControl
Public Event OnValueChanged(ByVal Sender As System.Object, ByVal e As EventArgs)
Private mAssignedFromMaskedTextBox As Boolean = False
Private mCDay As String = "01"
<Description("Fixed day in date."), Browsable(True), Category("Custom")> _
Public Property CDay() As String
Get
Return mCDay
End Get
Set(ByVal value As String)
mCDay = value
End Set
End Property
Private mCMonth As String = "01"
<Description("Fixed month in date."), Browsable(True), Category("Custom")> _
Public Property CMonth() As String
Get
Return mCMonth
End Get
Set(ByVal value As String)
If value >= 1 And value <= 12 Then
mCMonth = value
Else
mCMonth = 12
End If
End Set
End Property
Private mCYear As String = "2007"
<Description("Fixed month in date."), Browsable(True), Category("Custom")> _
Public Property CYear() As String
Get
Return mCYear
End Get
Set(ByVal value As String)
mCYear = value
End Set
End Property
Private mUseConstants As Boolean = True
<Description("Determines whether to use fixed values for day, month and year."), Browsable(True), Category("Custom")> _
Public Property UseConstants() As Boolean
Get
Return mUseConstants
End Get
Set(ByVal value As Boolean)
mUseConstants = value
End Set
End Property
Private mText As String = String.Empty
<Description("The text (value) of the masked text box."), Browsable(True), Category("Custom")> _
Public Overrides Property Text() As String
Get
'Return mText
Return DateTimePicker.Value
End Get
Set(ByVal Value As String)
mText = Value
'We don't validate but DateTimePicker takes care of it.
'This assigns to MaskedTextBox too eventually by way of ValueChanged event
If DateTimePicker.Value.ToShortDateString().Equals(Value) Then
MaskedTextBox.Text = DateTimePicker.Value.ToString(System.Globalization.CultureInfo.InvariantCulture)
Else
mAssignedFromMaskedTextBox = False
DateTimePicker.Value = Value
End If
Dim e As New System.EventArgs
RaiseEvent OnValueChanged(Me, e)
End Set
End Property
Private mCustomeDateTimeFormat As String = String.Empty
'<Description("The custom format of the embedded DateTimePicker."), Browsable(True), Category("Custom")> _
'Public Property CustomDateTimeFormat() As String
' Get
' Return mCustomeDateTimeFormat
' End Get
' Set(ByVal value As String)
' mCustomeDateTimeFormat = CustomDateTimeFormat
' DateTimePicker.CustomFormat = mCustomeDateTimeFormat
' End Set
'End Property
Private mDateTimeFormat As DateTimePickerFormat = DateTimePickerFormat.Short
'<Description("The format of the embedded DateTimePicker."), Browsable(True), Category("Custom")> _
'Public Property DateTimeFormat() As DateTimePickerFormat
' Get
' Return mDateTimeFormat
' End Get
' Set(ByVal value As DateTimePickerFormat)
' mDateTimeFormat = DateTimeFormat
' DateTimePicker.Format = mDateTimeFormat
' End Set
'End Property
Public Sub DateTimePicker_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles DateTimePicker.ValueChanged
DateTimePicker.Value = CDate(DatePart(DateInterval.Month, DateTimePicker.Value).ToString() & _
"/" & CDay.ToString() & "/" & _
DatePart(DateInterval.Year, DateTimePicker.Value).ToString())
If Not mAssignedFromMaskedTextBox Then
MaskedTextBox.Text = DateTimePicker.Value.ToString(System.Globalization.CultureInfo.InvariantCulture)
End If
RaiseEvent OnValueChanged(sender, e)
End Sub
Private Sub NullableDateTime_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Enter, MyBase.Leave
mAssignedFromMaskedTextBox = False
DateTimePicker.Value = CDate(DatePart(DateInterval.Month, DateTimePicker.Value).ToString() & _
"/" & CDay.ToString() & "/" & _
DatePart(DateInterval.Year, DateTimePicker.Value).ToString())
End Sub
Private Sub DateTimePicker_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DateTimePicker.Leave
'Unfortunately DateTimePicker doesn't provide a CheckedChanged event, which would
'be the best place for this check.
Dim NextCtrl As New Control
If Not DateTimePicker.Checked Then
MaskedTextBox.Text = String.Empty
'MessageBox.Show(Me.ParentForm.Name & " <--Parent: " & "Me : " & Me.Name & " Next-->" & Me.ParentForm.GetNextControl(Me, True).Name)
With Me.ParentForm.GetNextControl(Me, True)
If CanFocus Then
.Focus()
ElseIf .CanSelect Then
.Select()
End If
End With
End If
End Sub
Private Sub MaskedTextBox_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MaskedTextBox.Validating
If MaskedTextBox.Text.Trim.Equals("/ /") Then
DateTimePicker.Checked = False
Else
'This will force validation through date time picker.
mAssignedFromMaskedTextBox = True
DateTimePicker.Value = MaskedTextBox.Text
End If
End Sub
Private Sub MaskedTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MaskedTextBox.TextChanged
RaiseEvent OnValueChanged(sender, e)
End Sub
Public Function IsNull() As Boolean
Return MaskedTextBox.Text.Trim.Equals("/ /")
End Function
End Class
Suggestions ARE welcome. Been stuck on this component for three days...deadline approaching. You may uncomment the two properties, it doesn't make a difference. Thanks!