Meilleur auteur de réponses
Conversion Qwerty / Azerty

Question
-
Bonjour,
J'ai besoin de convertir une chaine de caractères Qwerty d'un TextBox1 en une chaine Azerty dans un TextBox2.
Quand je tape "a" ou "z", j'ai bien un "q" ou un "w" mais pas l'inverse.
Ci-dessous mon code :
Dim str_qwt As String = TextBox1.Text Dim str_azt As String = str_qwt.Replace("a", "q").Replace("z", "w") _ .Replace("m", ",").Replace("q", "a").Replace("w", "z") TextBox2.Text = str_azt
Merci pour votre aide.
Cordialement.
Réponses
-
Le plus simple est d'utiliser une lookup table qui fait la correspondance entre qwerty et azerty.
Par exemple la fonction ressemblerai à ceci:
Private strTableAzerty As String = "azertyuiopqsdfghjklmwxcvbn,;:!AZERTYUIOPQSDFGHJKLMWXCVBN?./§"
Private strTableQwerty As String = "qwertyuiopasdfghjkl;zxcvbnm,./QWERTYUIOPASDFGHJKL;ZXCVBNM,./"Private Function Azerty2Qwerty(ByVal strTexteAModifier As String) As String Azerty2Qwerty = "" ' Conversion de azerty en qwerty: For Each _c As Char In strTexteAModifier Dim IndexTable As Integer ' Position du caractère dans la table azerty IndexTable = strTableAzerty.IndexOf(_c) If IndexTable <> -1 Then ' le caractère est présent on le remplace par son équivallent qwerty Azerty2Qwerty &= strTableQwerty.ElementAt(IndexTable) Else Azerty2Qwerty &= _c End If Next End Function
Et vous pouvez modifier String avec une extension pour ensuite appeler par exemple Textbox1.Text.Azerty2Qwerty:
Module StringExtensions Private strTableAzerty As String = "azertyuiopqsdfghjklmwxcvbn,;:!AZERTYUIOPQSDFGHJKLMWXCVBN?./§" Private strTableQwerty As String = "qwertyuiopasdfghjkl;zxcvbnm,./QWERTYUIOPASDFGHJKL;ZXCVBNM,./" <Runtime.CompilerServices.Extension()> Public Function Azerty2Qwerty(ByVal strTexteAModifier As String) As String Azerty2Qwerty = "" ' Conversion de azerty en qwerty: For Each _c As Char In strTexteAModifier Dim IndexTable As Integer ' Position du caractère dans la table azerty IndexTable = strTableAzerty.IndexOf(_c) If IndexTable <> -1 Then ' le caractère est présent on le remplace par son équivallent qwerty Azerty2Qwerty &= strTableQwerty.ElementAt(IndexTable) Else Azerty2Qwerty &= _c End If Next End Function <Runtime.CompilerServices.Extension()> Public Function Qwerty2Azerty(ByVal strTexteAModifier As String) As String Qwerty2Azerty = "" ' Conversion de azerty en qwerty: For Each _c As Char In strTexteAModifier Dim IndexTable As Integer ' Position du caractère dans la table azerty IndexTable = strTableQwerty.IndexOf(_c) If IndexTable <> -1 Then ' le caractère est présent on le remplace par son équivallent qwerty Qwerty2Azerty &= strTableAzerty.ElementAt(IndexTable) Else Qwerty2Azerty &= _c End If Next End Function End Module
Attention aux caractères spéciaux ^$¨£ù*%µ,;:!?./§ qui ne sont pas forcément à la même position sur les 2 claviers...il faut une conversion spéciale...
- Modifié Cyrille Précetti lundi 4 septembre 2017 10:24
- Proposé comme réponse Nina ZaekovaMicrosoft contingent staff, Moderator mardi 5 septembre 2017 06:08
- Marqué comme réponse NET Work mardi 5 septembre 2017 19:18
Toutes les réponses
-
Le plus simple est d'utiliser une lookup table qui fait la correspondance entre qwerty et azerty.
Par exemple la fonction ressemblerai à ceci:
Private strTableAzerty As String = "azertyuiopqsdfghjklmwxcvbn,;:!AZERTYUIOPQSDFGHJKLMWXCVBN?./§"
Private strTableQwerty As String = "qwertyuiopasdfghjkl;zxcvbnm,./QWERTYUIOPASDFGHJKL;ZXCVBNM,./"Private Function Azerty2Qwerty(ByVal strTexteAModifier As String) As String Azerty2Qwerty = "" ' Conversion de azerty en qwerty: For Each _c As Char In strTexteAModifier Dim IndexTable As Integer ' Position du caractère dans la table azerty IndexTable = strTableAzerty.IndexOf(_c) If IndexTable <> -1 Then ' le caractère est présent on le remplace par son équivallent qwerty Azerty2Qwerty &= strTableQwerty.ElementAt(IndexTable) Else Azerty2Qwerty &= _c End If Next End Function
Et vous pouvez modifier String avec une extension pour ensuite appeler par exemple Textbox1.Text.Azerty2Qwerty:
Module StringExtensions Private strTableAzerty As String = "azertyuiopqsdfghjklmwxcvbn,;:!AZERTYUIOPQSDFGHJKLMWXCVBN?./§" Private strTableQwerty As String = "qwertyuiopasdfghjkl;zxcvbnm,./QWERTYUIOPASDFGHJKL;ZXCVBNM,./" <Runtime.CompilerServices.Extension()> Public Function Azerty2Qwerty(ByVal strTexteAModifier As String) As String Azerty2Qwerty = "" ' Conversion de azerty en qwerty: For Each _c As Char In strTexteAModifier Dim IndexTable As Integer ' Position du caractère dans la table azerty IndexTable = strTableAzerty.IndexOf(_c) If IndexTable <> -1 Then ' le caractère est présent on le remplace par son équivallent qwerty Azerty2Qwerty &= strTableQwerty.ElementAt(IndexTable) Else Azerty2Qwerty &= _c End If Next End Function <Runtime.CompilerServices.Extension()> Public Function Qwerty2Azerty(ByVal strTexteAModifier As String) As String Qwerty2Azerty = "" ' Conversion de azerty en qwerty: For Each _c As Char In strTexteAModifier Dim IndexTable As Integer ' Position du caractère dans la table azerty IndexTable = strTableQwerty.IndexOf(_c) If IndexTable <> -1 Then ' le caractère est présent on le remplace par son équivallent qwerty Qwerty2Azerty &= strTableAzerty.ElementAt(IndexTable) Else Qwerty2Azerty &= _c End If Next End Function End Module
Attention aux caractères spéciaux ^$¨£ù*%µ,;:!?./§ qui ne sont pas forcément à la même position sur les 2 claviers...il faut une conversion spéciale...
- Modifié Cyrille Précetti lundi 4 septembre 2017 10:24
- Proposé comme réponse Nina ZaekovaMicrosoft contingent staff, Moderator mardi 5 septembre 2017 06:08
- Marqué comme réponse NET Work mardi 5 septembre 2017 19:18
-