Results 1 to 6 of 6

Thread: AddHandler

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474

    AddHandler

    The code below does not work when i make any change in the datatable called 'tb', what am i missing?
    VB Code:
    1. Dim tb As New DataTable()
    2.  
    3. #Region " Windows Form Designer generated code "
    4.  
    5. '
    6. '
    7. '
    8. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    9. dim clm as DataColumn = New DataColumn("P", System.Type.GetType("System.String"))
    10. tb.Columns.Add(clm)
    11. AddHandler tb.RowChanged, New System.Data.DataRowChangeEventHandler(AddressOf TestEvents)
    12.  
    13. End Sub
    14.  
    15. Private Sub TestEvents(ByVal sender As Object, ByVal e As System.Data.DataRowChangeEventArgs)
    16.         MessageBox.Show("Something Happened")
    17. 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

  2. #2
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    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.

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    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:
    1. Public Class DGNumOnly
    2. Inherits DataGridTextBoxColumn
    3.    
    4. Public Sub New()
    5.         MyBase.New()
    6.         AddHandler Me.TextBox.KeyPress, New System.Windows.Forms.KeyPressEventHandler(AddressOf HandleKeyPress)
    7.         Me.TextBox.AcceptsReturn = True
    8. End Sub
    9.  
    10. Private Sub HandleKeyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs)
    11.         If (Not (System.Char.IsDigit(e.KeyChar)) And Not (System.Char.IsControl(e.KeyChar))) Then
    12.             If Not (e.KeyChar = "." And InStr(sender.Text, ".", CompareMethod.Text) = False) Then
    13.                 e.Handled = True
    14.             End If
    15.         End If
    16. End Sub
    17. 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

  4. #4
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Where is tb declared? It could be falling out of scope or be set to New or another instance after the Handler is added.

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    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

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    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
  •  



Click Here to Expand Forum to Full Width