|
-
Mar 29th, 2003, 05:17 PM
#1
Thread Starter
Frenzied Member
AddHandler
The code below does not work when i make any change in the datatable called 'tb', what am i missing?
VB Code:
Dim tb As New DataTable()
#Region " Windows Form Designer generated code "
'
'
'
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
dim clm as DataColumn = New DataColumn("P", System.Type.GetType("System.String"))
tb.Columns.Add(clm)
AddHandler tb.RowChanged, New System.Data.DataRowChangeEventHandler(AddressOf TestEvents)
End Sub
Private Sub TestEvents(ByVal sender As Object, ByVal e As System.Data.DataRowChangeEventArgs)
MessageBox.Show("Something Happened")
End Sub
'Heading for the automatic overload'
Marillion, Brave, The Great Escape, 1994
'How will WE stand the FIRE TOMORROW?'
Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979
-
Mar 30th, 2003, 01:03 AM
#2
Try this:
AddHandler tb.RowChanged, AddressOf TestEvents
Although if you know ahead of time then the Handles keyword is better:
Private Sub TestEvents(ByVal sender As Object, ByVal e As System.Data.DataRowChangeEventArgs) Handles tb.RowChanged
Also make sure you declared the tb withevents.
-
Mar 30th, 2003, 05:45 AM
#3
Thread Starter
Frenzied Member
Thanks Edneeis.
I have forgot to declare WithEvents, however both your method and mine did not work unless i added Handles tb.RowChanged.
So comparing with this piece of code, can you please tell me whats the difference? why here it work without Handles keyword?
VB Code:
Public Class DGNumOnly
Inherits DataGridTextBoxColumn
Public Sub New()
MyBase.New()
AddHandler Me.TextBox.KeyPress, New System.Windows.Forms.KeyPressEventHandler(AddressOf HandleKeyPress)
Me.TextBox.AcceptsReturn = True
End Sub
Private Sub HandleKeyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs)
If (Not (System.Char.IsDigit(e.KeyChar)) And Not (System.Char.IsControl(e.KeyChar))) Then
If Not (e.KeyChar = "." And InStr(sender.Text, ".", CompareMethod.Text) = False) Then
e.Handled = True
End If
End If
End Sub
End Class
'Heading for the automatic overload'
Marillion, Brave, The Great Escape, 1994
'How will WE stand the FIRE TOMORROW?'
Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979
-
Mar 30th, 2003, 05:37 PM
#4
Where is tb declared? It could be falling out of scope or be set to New or another instance after the Handler is added.
-
Mar 30th, 2003, 05:48 PM
#5
Thread Starter
Frenzied Member
Thanks!
The problem was not falling out of scope but a new instance of 'tb' after adding the Handler.
'Heading for the automatic overload'
Marillion, Brave, The Great Escape, 1994
'How will WE stand the FIRE TOMORROW?'
Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979
-
Mar 31st, 2003, 04:37 AM
#6
Thread Starter
Frenzied Member
Just to note that it seems that the problem was just new instances of 'tb' and it now works without declaring WthEvents and without adding Handles keyword unless making a new instance of tb.
Thanks for your help Edneeis
'Heading for the automatic overload'
Marillion, Brave, The Great Escape, 1994
'How will WE stand the FIRE TOMORROW?'
Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979
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
|