customize behavior of edit control in NumberCellType

Posted by: zenrhapsody on 8 September 2017, 1:24 pm EST

  • Posted 8 September 2017, 1:24 pm EST

    How can I change or override the behavior of the NumberCellType's editor?  It only allows entry of numeric keys.  While I want all the formatting behavior of NumberCellType, I want to allow users to be able to input data in hexidecimal format. 


    For example, if a user entered "F", I would parse the value to be the number 15.  I don't want to emulate generic cell types, I only want user to be able to enter 0-9 and A-F characters.  The current NumberCellType limits entry to 0-9 and decimal character.


    I am familiar with customizing cell types, but I cannot find exactly what to override here to make this work. 

  • Replied 8 September 2017, 1:24 pm EST

    Hello,


    I found this thread, maybe it will get you started: Integer Hex Values in Column: http://www.clubfarpoint.com/Forums/forums/thread/7271.aspx. Good Luck!

  • Replied 8 September 2017, 1:24 pm EST

    Hello,


    I tweaked the sample code given in the link above a little bit. It will now allow &h to be entered and accepted. I also made the editor a WithEvents TextBox so that I could handle the TextChanged event and strip out any invalid characters there. The first couple lines below are debugging lines you can place in the FpSpread LeaveCell event to see that the HexCellType class is actually doing what you think it is. Note that there is no maximum value checking being done. Good Luck!


         If Me.FpSpread1.Sheets(0).Cells(e.Row, e.Column).Text.Trim.Length > 0 Then
            Debug.WriteLine([String].Format("Row: {0}, Column: {1}, Text: {2}, Value: {3}", e.Row, e.Column, Me.FpSpread1.Sheets(0).Cells(e.Row, e.Column).Text, Me.FpSpread1.Sheets(0).Cells(e.Row, e.Column).Value))
        End If


    Imports System.Text.RegularExpressions


    Public Class HexCellType
        Inherits FarPoint.Win.Spread.CellType.GeneralCellType


        Private WithEvents CustomEditor As New TextBox


        Public Sub New()
            MyBase.New()
        End Sub 'New


        Public Overrides Function Format(ByVal obj As Object) As String
            If obj Is Nothing Then
                Return Nothing
            Else
                Return "&H" & Hex(obj)
            End If
        End Function 'Format


        Public Overrides Function GetEditorControl(ByVal appearance As FarPoint.Win.Spread.Appearance, ByVal zoomFactor As Single) As System.Windows.Forms.Control
            customEditor.Clear()
            Return customEditor
        End Function 'GetEditorControl


        Public Overrides Function GetEditorValue() As Object
            Try
                Dim value As String = Me.CustomEditor.Text.Trim
                If value.ToLower.StartsWith("&h") Then
                    value = value.Substring(2)
                End If
                If [String].IsNullOrEmpty(value) Then Return Nothing
                Return Convert.ToInt64(value, 16)
            Catch ex As Exception
                Throw
            End Try
        End Function 'GetEditorValue


        Public Overrides Sub SetEditorValue(ByVal value As Object)
            Try
                If value IsNot Nothing Then
                    Me.CustomEditor.Text = Hex(value).ToString
                    Me.CustomEditor.SelectAll()
                End If
            Catch ex As Exception
                Throw
            End Try
        End Sub 'SetEditorValue


        Private Sub CustomEditor_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles customEditor.TextChanged
            Try
                ' User may be trying to key &H before the number.
                If Me.CustomEditor.Text.Trim = "&" Then Return


                Dim prefix As String = String.Empty, text As String = Me.CustomEditor.Text


                ' Save off optional prefix &H and adjust text accordingly.
                If text.Trim.ToLower.StartsWith("&h") Then
                    prefix = text.Substring(0, 2)
                    text = text.Substring(2)
                End If


                ' User has entered &H before the number.
                If text.Trim.Length = 0 Then Return


                If Regex.IsMatch(text, "[^\dA-F]+", RegexOptions.IgnoreCase Or RegexOptions.Multiline) Then
                    ' Save selection start and selection length.
                    Dim selectionStart As Integer = Me.CustomEditor.SelectionStart
                    Dim selectionLength As Integer = Me.CustomEditor.SelectionLength


                    ' Replace non hex characters
                    text = Regex.Replace(text, "[^\dA-F]+", String.Empty, RegexOptions.IgnoreCase Or RegexOptions.Multiline)
                    text = prefix & text


                    ' Adjust selection start in case it was at the end of the string and something was removed.
                    If selectionStart > text.Length Then
                        selectionStart = text.Length
                    End If


                    ' Adjust selection length if something was removed and it is now too long.
                    Do While selectionStart + selectionLength > text.Length
                        selectionLength -= 1
                    Loop


                    ' Set custom editor text to new value.
                    Me.CustomEditor.Text = text


                    ' Reset selection start and selection length.
                    Me.CustomEditor.SelectionStart = selectionStart
                    Me.CustomEditor.SelectionLength = selectionLength
                End If
            Catch ex As Exception
                Throw
            End Try
        End Sub 'CustomEditor_TextChanged


    End Class

  • Replied 8 September 2017, 1:24 pm EST

    Thanks for the detailed explanations and code.  I appreciate that!


    I also need to be sensitive to font, zoom factors, etc.  I know FarPoint's default editors take all this into account (and is part of the GetEditControl parameters).  Is there any detailed sample code for custom edit controls out there?  I've been searching, but can't find the detailed info.


     

  • Replied 8 September 2017, 1:24 pm EST

    Hello,


    Instead of using a standard textbox, why not use our editor. Make the following changes to the code posted and it should work for you.


    Private WithEvents CustomEditor As FarPoint.Win.Spread.CellType.GeneralEditor


    Public Overrides Function GetEditorControl(ByVal appearance As FarPoint.Win.Spread.Appearance, ByVal zoomFactor As Single) As System.Windows.Forms.Control


    CustomEditor = MyBase.GetEditorControl(appearance, zoomFactor)


    Return CustomEditor


    End Function 'GetEditorControl

  • Replied 8 September 2017, 1:24 pm EST

    Thanks ScottS, very helpful! The code posted above was based upon sample code found in the linked thread referenced earlier.
Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels