﻿Imports System.Globalization

Public Class Numeric_TextBox
    Inherits TextBox

    '*****************************************************
    'verify text is a number 
    Public Function verif_nombre(ByVal text As String) As String

        Dim count As Integer 'to count quantity of the letter e/E
        Dim l As Integer = text.Length
        If l = 0 Then Return text ' if text is empty don't go further (prevent error)
        Dim charac As Char = text.Chars(text.Length - 1) ' get last charac typed

        'count quantity of the letter e/E in text
        If text.Contains("e") And text.Contains("E") Then
            count = 2
        ElseIf text.Contains("e") Then
            count = text.Split(CChar("e")).Length - 1
        ElseIf text.Contains("E") Then
            count = text.Split(CChar("E")).Length - 1
        End If

        ' manage - sign
        If (charac = CChar("-") And text.Length = 1) Then 'if is it the first charac then OK
            Return text
        ElseIf text.Length > 2 Then
            If (charac = CChar("-") And (text(text.Length - 2) = CChar("e") Or text(text.Length - 2) = CChar("E"))) Then 'if it is after e/E then OK
                Return text
            ElseIf (charac = CChar("-") And (text(text.Length - 2) <> CChar("e") Or text(text.Length - 2) <> CChar("E"))) Then 'prevent use of - sign as last charac
                text = text.Remove(l - 1)
                Return text
            End If
        End If

        'Replace space by nothing . in case the user put a space in the middle
        If text.Contains(" ") Then
            text = text.Replace(" ", "")
            Return text
        End If

        If count = 1 And (charac = CChar("E") Or charac = CChar("e")) And l > 1 Then 'manage  scientific notation and use of e/E
            Return text
        Else
            If IsNumeric(text) Then ' verify if text is a number
                Return text
            Else
                text = text.Remove(l - 1) 'else remove last charac until it is a number
                Return text
            End If
        End If

    End Function

    '***************************************************
    ' Manage coma and dot
    Public Function gpv(ByVal text As String) As String
        Dim nfi As NumberFormatInfo = NumberFormatInfo.CurrentInfo 'get info on decimal separator for current user
        If nfi.NumberDecimalSeparator = "." Then 'if it is a dot replace coma by dot
            text = text.Replace(",", ".")
        Else 'if it is a coma, replace dot by coma
            text = text.Replace(".", ",")
        End If
        Return text
    End Function

    Public Sub Numeric_TextBox_TextChanged(sender As Object, e As System.EventArgs) Handles Me.TextChanged
        Me.Text = verif_nombre(gpv(Me.Text)).ToString
        Me.Select(Me.Text.Length, 0)
    End Sub

    Private Sub Numeric_TextBox_Leave(sender As Object, e As System.EventArgs) Handles Me.Leave
        If IsNumeric(Me.Text) = False Then ' when focus lost, do a last check in case you finish with e/E
            'Me.Text = Me.Text.Remove(Me.Text.Length - 1) 'remove last charac
            MsgBox("L'entrée n'est pas valide")
            Me.Select()
            Me.Select(Me.Text.Length, 0) 'stay in the textbox
        End If

    End Sub



End Class
