Results 1 to 9 of 9

Thread: bug in inherited controls?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2007
    Posts
    403

    bug in inherited controls?

    hi all,

    on the custom inherited control i created, i noticed that when i bind my custom property to a data column, if that data column returns a value other than Null it always return a datarowstate of modified even if there were no changes.

    to produce this issue, i have below a sample code for a test inherited control

    Code:
    Imports System.Windows.Forms
    Imports System.ComponentModel
    
    Public Class TestControl
      Inherits TextBox
    
      <Bindable(True)> _
      Public Property Value() As String
        Get
          Return MyBase.Text
        End Get
        Set(ByVal value As String)
          MyBase.Text = value
        End Set
      End Property
    End Class
    create a form, add the usual objects necessary for data bindings, (datasource, datatable). make sure the data column bound to this TestControl will return non null values.

    also create a procedure for position changed to catch the datarowstate like below...

    Code:
      Private Sub PositionChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim rowstate As DataRowState = DataRowState.Unchanged
        rowstate = Me.YourDataSet.YourDataTable.Rows(Me.YourBindingSource.Position - 1).RowState
        MsgBox(rowstate.ToString)
      End Sub
    Note that i used Position - 1 so that when you go to the next record, it will show you the rowstate of the previous record.

    on your Form_Load add the ff:

    Code:
    AddHandler YourBindingSource.PositionChanged, AddressOf PositionChanged
    don't make any changes to the current record before you go to the next record. you will see that the rowstate will still return Modified.

    can anyone share their thoughts on this?

    thanks

    VB Version: Microsoft Visual Studio 2008 Professional Edition
    .NET Version: Microsoft .NET Framework Version 3.5
    OS: Windows XP SP3

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

    Re: bug in inherited controls?

    This is a really odd situation. I just used your code as you suggested and saw the behaviour you described. I messed around a bit and continued to see that behaviour with no real indication of why. I even took out the body of both the getter and the setter and still saw the same behaviour. I have no real idea of the cause but I'm interested enough to test a bit more so I'll let you know if I discover anything.
    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

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2007
    Posts
    403

    Re: bug in inherited controls?

    i've now tried this on both vb 2005 express and vb 2008 express. unfortunately same behavior. i haven't tried on other editions yet as i'm still downloading vs 2008 pro trial version. although i'm not very confident that it has something to do with the editions.

    Edit:
    i've now tried it in vs studio 2008 pro trial and same behavior. i've also posted this in the microsoft forums and asking if it is a bug but have got no response yet.
    Last edited by adshocker; Aug 22nd, 2009 at 08:52 PM.

    VB Version: Microsoft Visual Studio 2008 Professional Edition
    .NET Version: Microsoft .NET Framework Version 3.5
    OS: Windows XP SP3

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2007
    Posts
    403

    Re: bug in inherited controls?

    Updates:
    I've also now tried creating a custom control which does not inherits any standard control but still same behavior.

    i hope there's a fix to this.

    VB Version: Microsoft Visual Studio 2008 Professional Edition
    .NET Version: Microsoft .NET Framework Version 3.5
    OS: Windows XP SP3

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2007
    Posts
    403

    Re: bug in inherited controls?

    hi all,

    after lots of research, somehow i manage to find some codes that made the rowstate stay as Unchanged but the problem now is that it stays Unchanged even if there are changes.

    i added the following codes:

    Code:
      Public Event ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs)
    
      Public Sub OnValueChanged(ByVal e As System.EventArgs)
        RaiseEvent ValueChanged(Me, e)
      End Sub
    but i'm not really that familiar with events. i just read in this thread that you need to have an event for the property.

    i'm hoping someone can help create the events properly.

    thanks.
    Last edited by adshocker; Aug 27th, 2009 at 03:24 AM.

    VB Version: Microsoft Visual Studio 2008 Professional Edition
    .NET Version: Microsoft .NET Framework Version 3.5
    OS: Windows XP SP3

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

    Re: bug in inherited controls?

    Are you raising the ValueChanged event when the Value changes? The idea is that, in the property setter, you check whether the new value is different to the old value and, if it is, you set the backing field and raise the event, i.e. call OnValueChanged, otherwise you do nothing.

    I thought I'd tested whether an event would affect this but if you're saying it does then either I didn't or I did something wrong.
    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

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2007
    Posts
    403

    Re: bug in inherited controls?

    hi,

    thanks for the quick response.

    actually it does have an effect on the DataRowState but it still needs a bit more tweaking.

    anyways, after reading your advice, i now have

    Code:
      Public Event ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
    
      Protected Overridable Sub OnValueChanged(ByVal e As System.EventArgs)
        RaiseEvent ValueChanged(Me, e)
      End Sub
    
      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
            Call OnValueChanged()
            If value.HasValue Then
              _Value = value
              MyBase.Text = value.Value.ToString(_FormatMask).ToUpper()
            Else
              MyBase.Text = String.Empty
            End If
    
          End If
        End Set
      End Property
    however i get the error "Error 1: Argument not specified for parameter 'e' of 'Protected Overridable Sub OnValueChanged(e As System.EventArgs)'." on the line

    Code:
    Call OnValueChanged()
    as i'm not sure what to pass to it since there's no System.EventArgs as parameter in the property.

    please help.

    thanks.

    VB Version: Microsoft Visual Studio 2008 Professional Edition
    .NET Version: Microsoft .NET Framework Version 3.5
    OS: Windows XP SP3

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

    Re: bug in inherited controls?

    Quote Originally Posted by adshocker View Post
    i'm not sure what to pass to it since there's no System.EventArgs as parameter in the property.
    You're supposed to create the event args and provide the data. There is no data for a straight EventArgs though, so you just pass EventArgs.Empty.

    While it's not going to hurt, there's no point setting _Value twice as you are. It's also not going to hurt but your use of the Call key word is redundant too. You can call a method without the Call key word, which exists for backward compatibility with VB6, which did require its use in certain situations.
    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

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2007
    Posts
    403

    Re: bug in inherited controls?

    hi,

    i've changed the code to meet your suggestion but somehow it still returns a rowState of Unchanged.

    here's my new code:

    Code:
        Public Event ValueChanged As EventHandler
    
        Protected Overridable Sub OnValueChanged(ByVal e As System.EventArgs)
            RaiseEvent ValueChanged(Me, e)
        End Sub
    
        <Bindable(True), _
         Browsable(False)> _
        Public Property Value() As Nullable(Of DateTime)
            Get
                Return _Value
            End Get
            Set(ByVal value As Nullable(Of DateTime))
                OnValueChanged(System.EventArgs.Empty)
    
                If Not _Value.Equals(value) Then
                    _Value = value
    
                    If value.HasValue Then
                        MyBase.Text = value.Value.ToString(_FormatMask).ToUpper()
                    Else
                        MyBase.Text = String.Empty
                    End If
                End If
            End Set
        End Property
    something is not right with the even i think.

    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