|
-
Jul 19th, 2009, 08:13 AM
#1
Thread Starter
Hyperactive Member
Need help with this custom control
hi all,
i created a custom control that accepts only datetime values.
http://www.vbforums.com/showthread.php?p=3551859
but i'm having some issues with it when it is bound to a column and then i navigate thru records. it seems to trigger a rowchange to all navigated records even though i haven't manually change anything on these records.
this is affecting the last_update_date and last_updated_by fields of my tables. i need to only affect records that were manually changed.
can anyone help me?
also, any date field should be bound to the Value property of this control instead of Text. haven't figured out how to remove the Text from bindable properties yet.
thanks
VB Version: Microsoft Visual Studio 2008 Professional Edition
.NET Version: Microsoft .NET Framework Version 3.5
OS: Windows XP SP3
-
Aug 20th, 2009, 12:18 AM
#2
Thread Starter
Hyperactive Member
Re: Need help with this custom control
hi,
no one?
i've pasted my code below in case nobody wants to check out the source.
Code:
Public Class DateTimeItem
Inherits System.Windows.Forms.TextBox
Dim _Value As Nullable(Of DateTime) = Nothing
Dim _FormatMask As String = "MM/dd/yyyy" 'Default
Dim _Required As Boolean = False 'Default
Dim _RequiredBackColor As Color = Color.LightYellow 'Default
Dim _Text As String = String.Empty
Public Property FormatMask() As String
Get
Return _FormatMask
End Get
Set(ByVal value As String)
If value <> String.Empty Then
_FormatMask = value
End If
End Set
End Property
<Bindable(True)> _
Public Property Value() As Nullable(Of DateTime)
Get
Return _Value
End Get
Set(ByVal value As Nullable(Of DateTime))
If value.HasValue Then
Me.Text = UCase(value.Value.ToString(_FormatMask))
_Value = value
Else
Me.Text = String.Empty
End If
End Set
End Property
Public Property Required() As Boolean
Get
Return _Required
End Get
Set(ByVal value As Boolean)
_Required = value
If _Required = True Then
MyBase.BackColor = _RequiredBackColor
Else
MyBase.BackColor = SystemColors.Window
End If
End Set
End Property
Public Property RequiredBackColor() As Color
Get
Return _RequiredBackColor
End Get
Set(ByVal value As Color)
If _Required = True Then
MyBase.BackColor = _RequiredBackColor
Else
MyBase.BackColor = SystemColors.Window
End If
End Set
End Property
Public Sub ShowCalendar()
Dim fcalendar As New CalendarForm
With fcalendar
.Source = Me
.ShowDialog()
End With
End Sub
Private Sub DateTimeItem_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.DoubleClick
Try
ShowCalendar()
Catch ex As Exception
Me.Focus()
Me.SelectAll()
Beep()
End Try
End Sub
Private Sub DateTimeItem_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles Me.Validating
Try
MsgBox(MyBase.Text.Length.ToString)
If MyBase.Text.Length > 0 Then
If Me.Value.HasValue Then
If DateTime.ParseExact(Me.Text, _FormatMask, CultureInfo.InvariantCulture) <> DateTime.ParseExact(Me.Value.Value.ToString(_FormatMask), _FormatMask, CultureInfo.InvariantCulture) Then
Me.Value = DateTime.ParseExact(Me.Text, _FormatMask, CultureInfo.InvariantCulture)
End If
End If
End If
Catch ex As Exception
Me.Focus()
Me.SelectAll()
End Try
End Sub
End Class
thanks
VB Version: Microsoft Visual Studio 2008 Professional Edition
.NET Version: Microsoft .NET Framework Version 3.5
OS: Windows XP SP3
-
Aug 20th, 2009, 12:45 AM
#3
Re: Need help with this custom control
First up, you should be overriding the OnValidating method, not handling the Validating event.
As for the issue, try checking in your Value setter whether the actual value has changed and only set the Text property if it has.
-
Aug 20th, 2009, 02:21 AM
#4
Thread Starter
Hyperactive Member
Re: Need help with this custom control
hi,
thanks for the tip.
i've changed my validating process into
Code:
Protected Overrides Sub OnValidating(ByVal e As System.ComponentModel.CancelEventArgs)
MyBase.OnValidating(e)
Try
MsgBox(MyBase.Text.Length.ToString)
If MyBase.Text.Length > 0 Then
If Me.Value.HasValue Then
If DateTime.ParseExact(Me.Text, _FormatMask, CultureInfo.InvariantCulture) <> DateTime.ParseExact(Me.Value.Value.ToString(_FormatMask), _FormatMask, CultureInfo.InvariantCulture) Then
Me.Value = DateTime.ParseExact(Me.Text, _FormatMask, CultureInfo.InvariantCulture)
End If
End If
End If
Catch ex As Exception
Me.Focus()
Me.SelectAll()
End Try
End Sub
also, i'm having a bit of a problem getting the logic for checking if the value has changed. i have this set of codes but doesn't seem to work still.
Code:
<Bindable(True)> _
Public Property Value() As Nullable(Of DateTime)
Get
Return _Value
End Get
Set(ByVal value As Nullable(Of DateTime))
If value.HasValue And _Value.HasValue Then
If _Value.Value <> value.Value Then
_Value = value
Me.Text = UCase(value.Value.ToString(_FormatMask))
End If
ElseIf value.HasValue And _Value.HasValue = False Then
_Value = value
Me.Text = UCase(value.Value.ToString(_FormatMask))
ElseIf value.HasValue = False And _Value.HasValue = True Then
_Value = value
Me.Text = UCase(value.Value.ToString(_FormatMask))
Else
Me.Text = String.Empty
End If
End Set
End Property
can you help me figure out what's wrong?
thanks
VB Version: Microsoft Visual Studio 2008 Professional Edition
.NET Version: Microsoft .NET Framework Version 3.5
OS: Windows XP SP3
-
Aug 20th, 2009, 02:42 AM
#5
Re: Need help with this custom control
You use the Equals method to compare Nullable types. Try this code for an example of its behaviour:
Code:
Dim d1 As Nullable(Of DateTime) = Nothing
Dim d2 As Nullable(Of DateTime) = Nothing
Dim d3 As Nullable(Of DateTime) = #1/1/2001#
Dim d4 As Nullable(Of DateTime) = #1/1/2001#
Dim d5 As Nullable(Of DateTime) = #2/2/2002#
If d1.Equals(d2) Then
MessageBox.Show("Nothing equals Nothing")
Else
MessageBox.Show("Nothing does not equal Nothing")
End If
If d1.Equals(d3) Then
MessageBox.Show("Nothing equals #1/1/2001#")
Else
MessageBox.Show("Nothing does not equal #1/1/2001#")
End If
If d3.Equals(d1) Then
MessageBox.Show("#1/1/2001# equals Nothing")
Else
MessageBox.Show("#1/1/2001# does not equal Nothing")
End If
If d3.Equals(d4) Then
MessageBox.Show("#1/1/2001# equals #1/1/2001#")
Else
MessageBox.Show("#1/1/2001# does not equal #1/1/2001#")
End If
If d3.Equals(d5) Then
MessageBox.Show("#1/1/2001# equals #2/2/2002#")
Else
MessageBox.Show("#1/1/2001# does not equal #2/2/2002#")
End If
Your code should be:
vb.net Code:
Set(ByVal value As Nullable(Of DateTime)) If Not _Value.Equals(value) Then _Value = value If value.HasValue Then Me.Text = value.Value.ToString(_FormatMask).ToUpper() Else Me.Text = String.Empty End If End If End Set
-
Aug 20th, 2009, 03:11 AM
#6
Thread Starter
Hyperactive Member
Re: Need help with this custom control
hi,
thanks for the suggestion.
i changed my code copying yours like so
Code:
<Bindable(True)> _
Public Property Value() As Nullable(Of DateTime)
Get
Return _Value
End Get
Set(ByVal value As Nullable(Of DateTime))
If Not _Value.Equals(value) Then
_Value = value
If value.HasValue Then
Me.Text = value.Value.ToString(_FormatMask).ToUpper()
Else
Me.Text = String.Empty
End If
End If
End Set
End Property
but the records navigated still updates even if there were no changes on them. i placed a breakpoint on this block and when i tried to save a record, it loops around 4 times. the first 3 times it does not go inside the If value.HasValue block but the 4th time it goes there.
not sure what's causing it to go there though. do you have any thoughts on this?
thanks
VB Version: Microsoft Visual Studio 2008 Professional Edition
.NET Version: Microsoft .NET Framework Version 3.5
OS: Windows XP SP3
-
Aug 20th, 2009, 03:29 AM
#7
Thread Starter
Hyperactive Member
Re: Need help with this custom control
hi,
i tried to do some more tests.
i added a code in my form to catch the rowstate value of the previous row whenever the position changes.
it then kept on saying "Modified" each time i tried to navigate to other records.
i then removed the custom control from my form and then tried to navigate to other records.
this time it kept saying "Unchanged".
i guess there might be something wrong with the custom control.
VB Version: Microsoft Visual Studio 2008 Professional Edition
.NET Version: Microsoft .NET Framework Version 3.5
OS: Windows XP SP3
-
Aug 21st, 2009, 10:01 AM
#8
Thread Starter
Hyperactive Member
Re: Need help with this custom control
i noticed one other thing.
if the property that is bound to a datatable column is not a property of the MyBase object, it seems to change the rowstate to Modified even though there are no codes inside that Property Block.
could this be a bug?
VB Version: Microsoft Visual Studio 2008 Professional Edition
.NET Version: Microsoft .NET Framework Version 3.5
OS: Windows XP SP3
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|