This project is read-only.

Capture control validating/validated events from editor

Feb 27, 2009 at 1:19 PM
I am using v4.11 because the new version (4.20) is for the VS 2008 and the newer framework. I need to stay on v2.0 of the framework.

I created a custom editor that inherits from SourceGrid.Cells.Editors.TextBoxNumeric. What is the best way to capture the validating and validated events?

In the form where I want to capture the validating event I put something like this ( code):
AddHandler _CellDoubleRoundingEditor.Validating, AddressOf Cell_OnValidating

The IDE highlights the .Validating and complains that it is obsolete and I should use Control.Validating. This is also true for the Validated event.

I tried this in my form:

Dim dt As DevAge.Windows.Forms.DevAgeTextBox = _CellDoubleRoundingEditor.Control
AddHandler dt.Validating, AddressOf Cell_OnValidating

But the handler doesn't fire...

In my custom editor class:
Friend NotInheritable Class CellEditorNumberRounding
        Inherits SourceGrid.Cells.Editors.TextBoxNumeric

#Region "Constructor"

        ''' <summary>
        ''' The default constructor will round double or single values to the nearest 0.01
        ''' </summary>
        ''' <remarks></remarks>
        Friend Sub New()

            If _MyRound Is Nothing Then _MyRound = New iRing.Common.Math.Rounding
            Me.TypeConverter = New DevAge.ComponentModel.Converter.NumberTypeConverter(GetType(Double), GenerateNumberFormatString(_MyRound.Digits))

            AddHandler Control.Validating, AddressOf Control_Validating
            AddHandler Control.Validated, AddressOf Control_Validated
        End Sub

#Region "Public Events"
        Friend Event Ctl_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs)
        Friend Event Ctl_Validated(ByVal sender As Object, ByVal e As System.EventArgs)
#End Region

#Region "Private events"
        Private Sub Control_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs)
            RaiseEvent Ctl_Validating(sender, e)
        End Sub

        Private Sub Control_Validated(ByVal sender As Object, ByVal e As System.EventArgs)
            RaiseEvent Ctl_Validated(sender, e)
        End Sub
#End Region
end class

I can set a break point on "Control_Validating" & "Control_Validated" and the code breaks on each point. I have this in my form load event:
_CellDoubleRoundingEditor = New iRing.Common.Grid.CellEditorNumberRounding
_CellDoubleRoundingEditor.EnableEdit = True
_CellDoubleRoundingEditor.AllowNull = True

AddHandler _CellDoubleRoundingEditor.Ctl_Validating, AddressOf Cell_OnValidating
AddHandler _CellDoubleRoundingEditor.Ctl_Validated, AddressOf Cell_OnValidated

Breakpoints are set in the Cell_OnValidating and Cell_OnValidated routines, only the Cell_OnValidated is triggered. There is nothing in the routine except for a debug statement (I am just testing this out).

Any ideas or help would be appreciated..

Feb 27, 2009 at 1:36 PM
The Validating event is now firing properly - I had to rebuild the entire project and it started  to fire properly.

I would still like to know if there is a better way to capture the control's validating event? Or is that the best way?
Feb 27, 2009 at 2:11 PM
Edited Feb 27, 2009 at 3:28 PM
There are a set of events in the ControllerBase you can override

Create your own ControllerBase class in your project

public class CellEvent : SourceGrid.Cells.Controllers.ControllerBase
         public override void OnValueChanged(SourceGrid.CellContext sender, EventArgs e)
            base.OnValueChanged(sender, e);

            // Do your validation here

Declare this in the grid
CellEvent eventController = new CellEvent();   

and add it to a cell
this[r, c].AddController(eventController);

From the documentation...
Here the list of events that can be used inside a Controller:
  • Mouse: OnMouseDown, OnMouseUp, OnMouseMove, OnMouseEnter, OnMouseLeave
  • Keys: OnKeyUp, OnKeyDown, OnKeyPress
  • Click: OnDoubleClick, OnClick
  • Focus: OnFocusLeaving, OnFocusLeft, OnFocusEntering, OnFocusEntered, CanReceiveFocus
  • Cell Value: OnValueChanging, OnValueChanged
  • Edit: OnEditStarting, OnEditStarted, OnEditEnded

Feb 27, 2009 at 2:51 PM
For Visual Studio 2008 - you can use SharpDevelop, which is open source and has all modern functionality. It works like a charm with SourceGrid

About the framework - AFAIK, SourceGrid i still on .Net 2.0, and at the moment there is no intention to move to higher versions
Feb 28, 2009 at 1:17 PM
Wavey, thanks for the tip. I do have a controller that deals with the events (Actually I broke the controller into a couple of logical units, one to deal with key boards, one for mouse events and one for focus events). Unfortunately there is no event in the controllerbase that I have found that allows you to verify the inputs and allow you to cancel the changes. The changes I need to validate against are accross 3 cells that contain different values from row to row. It is much easier to re-raise the control's validating event then to have a separate controller for each cell (the cells have the same type of data - lengths).

Darius, I do use SharpDevelop and like it (particularly for c#)! So all I need to do is open the v4.20 project in sharpdevelop and compile?
Mar 1, 2009 at 12:36 PM
Yes, exactly.  Install SharpDevelop 3, and just open .sln file and enjoy :)

This is how i done all of my development, including SourceGrid