|
-
Aug 22nd, 2009, 12:48 AM
#1
Thread Starter
Hyperactive Member
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
-
Aug 22nd, 2009, 06:26 AM
#2
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.
-
Aug 22nd, 2009, 07:47 PM
#3
Thread Starter
Hyperactive Member
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
-
Aug 23rd, 2009, 11:41 PM
#4
Thread Starter
Hyperactive Member
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
-
Aug 27th, 2009, 03:21 AM
#5
Thread Starter
Hyperactive Member
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
-
Aug 27th, 2009, 04:21 AM
#6
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.
-
Aug 27th, 2009, 05:09 AM
#7
Thread Starter
Hyperactive Member
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
-
Aug 27th, 2009, 05:40 AM
#8
Re: bug in inherited controls?
 Originally Posted by adshocker
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.
-
Aug 27th, 2009, 07:59 AM
#9
Thread Starter
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|