Results 1 to 8 of 8

Thread: Need help with this custom control

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2007
    Posts
    403

    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

  2. #2

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2007
    Posts
    403

    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

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2007
    Posts
    403

    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

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    1. Set(ByVal value As Nullable(Of DateTime))
    2.     If Not _Value.Equals(value) Then
    3.         _Value = value
    4.  
    5.         If value.HasValue Then
    6.             Me.Text = value.Value.ToString(_FormatMask).ToUpper()
    7.         Else
    8.             Me.Text = String.Empty
    9.         End If
    10.     End If
    11. End Set
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2007
    Posts
    403

    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

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2007
    Posts
    403

    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

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2007
    Posts
    403

    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
  •  



Click Here to Expand Forum to Full Width