Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Problem with events

Problem with events

Scheduled Pinned Locked Moved C#
questionhelp
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Johnny 0
    wrote on last edited by
    #1

    In the following code I register for two events in the last two lines. The evnts fire for the first time the form is loaded but from the second time and on, events are not registered anymore. I tired different things like adding dataGrid.Scroll -= new ScrollEventHandler(dataGrid_Scroll); dataGrid.KeyPress -= new KeyPressEventHandler(dataGrid_KeyPress); beofre assigning dataGrid to the new FinderDataGridView object but stil doesn't work. I guess the question is what is the correct way of registering for events when you reassign an object to a new one? Hopefully the question is clear :~ public void SetFinder(ComplexObject finderObject) { complexObject = finderObject; view = complexObject[0].DefaultView; initialFilter = view.RowFilter; dataGrid = new FinderDataGridView(view, complexObject[0].QuickFinderColumns, complexObject); dataGrid.ColumnHeadersVisible = false; datagridSplit.Panel2.Controls.Add(dataGrid); emptyTable = new DataTable(); foreach (DataColumn column in finderObject.Tables[0].Columns) { if (column.ColumnMapping == MappingType.Element) { emptyTable.Columns.Add(new DataColumn(column.ColumnName, column.DataType)); } } emptyDataGrid.DataSource = emptyTable; emptyTable.Rows.Add(emptyTable.NewRow()); emptyTable.AcceptChanges(); dataGrid.Scroll += new ScrollEventHandler(dataGrid_Scroll); dataGrid.KeyPress += new KeyPressEventHandler(dataGrid_KeyPress); }

    M 1 Reply Last reply
    0
    • J Johnny 0

      In the following code I register for two events in the last two lines. The evnts fire for the first time the form is loaded but from the second time and on, events are not registered anymore. I tired different things like adding dataGrid.Scroll -= new ScrollEventHandler(dataGrid_Scroll); dataGrid.KeyPress -= new KeyPressEventHandler(dataGrid_KeyPress); beofre assigning dataGrid to the new FinderDataGridView object but stil doesn't work. I guess the question is what is the correct way of registering for events when you reassign an object to a new one? Hopefully the question is clear :~ public void SetFinder(ComplexObject finderObject) { complexObject = finderObject; view = complexObject[0].DefaultView; initialFilter = view.RowFilter; dataGrid = new FinderDataGridView(view, complexObject[0].QuickFinderColumns, complexObject); dataGrid.ColumnHeadersVisible = false; datagridSplit.Panel2.Controls.Add(dataGrid); emptyTable = new DataTable(); foreach (DataColumn column in finderObject.Tables[0].Columns) { if (column.ColumnMapping == MappingType.Element) { emptyTable.Columns.Add(new DataColumn(column.ColumnName, column.DataType)); } } emptyDataGrid.DataSource = emptyTable; emptyTable.Rows.Add(emptyTable.NewRow()); emptyTable.AcceptChanges(); dataGrid.Scroll += new ScrollEventHandler(dataGrid_Scroll); dataGrid.KeyPress += new KeyPressEventHandler(dataGrid_KeyPress); }

      M Offline
      M Offline
      Martin 0
      wrote on last edited by
      #2

      Hello,

      ~Johnny~ wrote:

      I tired different things like adding dataGrid.Scroll -= new ScrollEventHandler(dataGrid_Scroll); dataGrid.KeyPress -= new KeyPressEventHandler(dataGrid_KeyPress); beofre assigning dataGrid to the new FinderDataGridView object but stil doesn't work.

      Although, it is not solving your problem, you should always unregister the event like this, bevor creating a new instance, cause the old instance will not be cleared by the GC. I think the problem could be that you are not removing the old dataGrid instance out of the datagridSplit.Panel2.Controls. If your dataGrid instance has a dispose method, you also should call it. Hope it helps! All the best, Martin

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups