Changer separateur décimal, framework +api en vb.net

Soyez le premier à donner votre avis sur cette source.

Snippet vu 16 279 fois - Téléchargée 32 fois

Contenu du snippet

un petit code qui permet de modifier le séparateur décimal du panneau de configuration, !!! RESTAURE L'ANCIENNE VALEUR!!!!Ce code est en VB.NET, alors ne venait pa sme dire qu'en VB, on fait des choses mieux c'est le plus optimisé que j'ai trouvé au niveau du développement, l'utilisation de l'API et du framework me parait indispensable pour attaquer le panneau de config.

Sur ce Bon code

la deuxieme fonction restaure l'ancienne valeur dans la panneau de config ^^

Source / Exemple :


#Region "--------------- Imports ------------------"

Imports System.Runtime.InteropServices
Imports System.Globalization

#End Region

Public Class GlobalizationHelper

#Region "--------------- Constantes ---------------"

    Const LOCALE_SDECIMAL = &HE 'Decimal separator code : HE in hexadecimal means 14 in decimal
    Const LOCALE_SCURRENCY = &H16 'Monetary Decimal Separator code 

#End Region

#Region "--------------- Dll Imports --------------"

    'Import of the kernel32.dll SetLocaleInfo function 
    'required to insert in the configuration panel
    <DllImport("kernel32.dll", CharSet:=CharSet.Auto)> _
    Private Shared Function SetLocaleInfo(ByVal Locale As Integer, _
                            ByVal LCType As Integer, _
                            ByVal lpLCData As String) As Long
    End Function

    'Import of the kernel32.dll GetSystemDefaultLCID function 
    'required to check the LCID of the application
    <DllImport("kernel32.dll", CharSet:=CharSet.Auto)> _
    Private Shared Function GetSystemDefaultLCID() As Integer
    End Function

#End Region

#Region "--------------- Variables ----------------"

    Private Shared _oldDecimalSeparator As String
    'retrieves the old decimal operator initialized in the configuration panel
    Private Shared _newDecimalSeparator As Boolean = False
    'If a new decimal operator is set the value becomes true

    Private Shared _oldDecimalMonetaryOperator As String
    'Retrieves the old decimal monetary operator initialized in the configuration panel
    Private Shared _newDecimalMonetaryOperator As Boolean = False
    'If a new monetary operator is set the value becomes true

#End Region

#Region "--------------- Fonctions ----------------"

    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Force the dot of the monetary value to "." if not defined by default
    ''' </summary>
    ''' <remarks>
    ''' do not restore it
    ''' </remarks>
    ''' <history>
    ''' 		05/10/2004	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public Shared Function ForceDot()

        'Retrieves the old decimal separator in the configuration panel
        _oldDecimalSeparator = Application.CurrentCulture.NumberFormat.NumberDecimalSeparator

        Dim LCID As Integer 'Locale unique identifier'
        '32-bit value that consists of a language identifier and a sort identifier. 
        'The identifier supplied in an LCID is a standard international numeric abbreviation. 
        'This LCID has the components necessary to uniquely identify one of the installed 
        'system-defined locales. The LCID is constructed using the MAKELCID macro. 
        'The following illustration shows the format of the bits in an LCID. 

        '+-------------+---------+-------------------------+
        '|   Reserved  | Sort ID |      Language ID        |
        '+-------------+---------+-------------------------+
        '31          20 19     16 15                       0   bit
        '+-------------------------------------------------+

        Dim NouveauOperateur As Long 'Return Value for SetLocaleInfo

        'Test, in order to find, if the actual value of the decimal separator is the one you need in 
        'the configuration panel
        'Seek the number value, not monetary value
        If _oldDecimalSeparator <> "." Then
            'The separator is different from point, so insert value in the configuration panel

            LCID = GetSystemDefaultLCID()
            'Retrieves the system default locale identifier

            NouveauOperateur = SetLocaleInfo(LCID, LOCALE_SDECIMAL, ".")
            'Set the new operateur value to "." instead of the one there was before

            ForceDotForCurrentApplication()

            _newDecimalSeparator = True

        End If

        'If the _oldDecimalSeparator is "." when you retrieve it, the code if..endif matches and
        'no more actions is needed, because this is the good operator
        'If the block if..endif does not match, you have now the good operator affected
        'in the configuration panel

        _oldDecimalMonetaryOperator = Application.CurrentCulture.NumberFormat.CurrencyDecimalSeparator

        'Test, in order to find, if the actual value of the monetary oerator is the one you need in 
        'the configuration panel
        'Seek the monetary value, not number value
        If _oldDecimalMonetaryOperator <> "." Then
            'The separator is different from point, so insert value in the configuration panel

            LCID = GetSystemDefaultLCID()
            'Retrieves the system default locale identifier

            NouveauOperateur = SetLocaleInfo(LCID, LOCALE_SCURRENCY, ".")
            'Set the new operateur value to "." instead of the one there was before

            ForceDotMoneyForCurrentApplication()

            _newDecimalMonetaryOperator = True

        End If

        'If the _oldDecimalMonetaryOperator is "." when you retrieve it, the code if..endif matches and
        'no more actions is needed, because this is the good operator
        'If the block if..endif does not match, you have now the good operator affected
        'in the configuration panel

    End Function

    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Get the old decimal separator set in the configuration panel
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' 		05/10/2004	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public Shared Function ForceDotForCurrentApplication()

        'Le séparateur instancié dans le panneau de configuration est la virgule : ","
        'On effectue les modifications dans la culture
        Dim forceDotCulture As CultureInfo

        'Code un peu louche il faut avouer, mais il faut faire avec car le framework pose problème
        'ici; en effet, il faut cloner la culture pour pouvoir modifier les paramètres de l'application
        'car sinon la culture de base est en lecture seule.
        forceDotCulture = Application.CurrentCulture.Clone()

        'On affecte le point : "." comme paramètre de séparateur décimal
        'à la culture qui a été clonée
        forceDotCulture.NumberFormat.NumberDecimalSeparator = "."

        'Là, on affecte l'application cloné à celle où l'on travaille 
        'C'est un passage flou car en fait, l'appli est en mode readonly et l'on ne peut pas
        'la modifier directement, d'où cette affectation
        Application.CurrentCulture = forceDotCulture

    End Function

    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Get the old monetary decimal operator set in the configuration panel
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' 		05/10/2004	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public Shared Function ForceDotMoneyForCurrentApplication()

        'Le séparateur instancié dans le panneau de configuration est la virgule : ","
        'On effectue les modifications dans la culture
        Dim forceDotCulture As CultureInfo

        'Code un peu louche il faut avouer, mais il faut faire avec car le framework pose problème
        'ici; en effet, il faut cloner la culture pour pouvoir modifier les paramètres de l'application
        'car sinon la culture de base est en lecture seule.
        forceDotCulture = Application.CurrentCulture.Clone()

        'On affecte le point : "." comme paramètre de séparateur décimal
        'à la culture qui a été clonée
        forceDotCulture.NumberFormat.CurrencyDecimalSeparator = "."

        'Là, on affecte l'application cloné à celle où l'on travaille 
        'C'est un passage flou car en fait, l'appli est en mode readonly et l'on ne peut pas
        'la modifier directement, d'où cette affectation
        Application.CurrentCulture = forceDotCulture

    End Function

    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' If the decimal separator was changed by the ForceDot function
    ''' Restore the old value, if not does not do anything
    ''' </summary>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' 		05/10/2004	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public Shared Function RestoreDefaultSeparator()

        Dim LCID As Integer 'Locale unique identifier'
        '32-bit value that consists of a language identifier and a sort identifier. 
        'The identifier supplied in an LCID is a standard international numeric abbreviation. 
        'This LCID has the components necessary to uniquely identify one of the installed 
        'system-defined locales. The LCID is constructed using the MAKELCID macro. 
        'The following illustration shows the format of the bits in an LCID. 

        '+-------------+---------+-------------------------+
        '|   Reserved  | Sort ID |      Language ID        |
        '+-------------+---------+-------------------------+
        '31          20 19     16 15                       0   bit
        '+-------------------------------------------------+

        Dim NouveauOperateur As Long 'Return Value for SetLocaleInfo

        If _newDecimalSeparator = False Then
            'If the newDecimalSeparator is false, no modification has been done
            'exit the sub whithout modification
            Exit Function

        Else

            LCID = GetSystemDefaultLCID()
            'Retrieves the system default locale identifier

            NouveauOperateur = SetLocaleInfo(LCID, LOCALE_SDECIMAL, _oldDecimalSeparator)
            'Set the new operateur value to the oldDecimaloperator"." instead of the one there was before

        End If

        If _newDecimalMonetaryOperator = False Then
            'If the newDecimalMonetayrOperator is false, no modification has been done
            'exit the sub whithout modification
            Exit Function

        Else

            LCID = GetSystemDefaultLCID()
            'Retrieves the system default locale identifier

            NouveauOperateur = SetLocaleInfo(LCID, LOCALE_SCURRENCY, _oldDecimalMonetaryOperator)
            'Set the new operateur value to the oldDecimaloperator"." instead of the one there was before
        End If

    End Function

#End Region

End Class

A voir également

Ajouter un commentaire

Commentaires

Commenter la réponse de cs_Patrice99

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.