Dim str, word As String Dim num As Int64 str = "ABC001" num = Convert.ToInt64(str.Substring(str.Length - 3, 3)) word = str.Substring(0, str.Length - 3).ToCharArray If num < 999 Then num += 1 str = word & num.ToString.PadLeft(3, "0"c) Else num = 0 'ici il manque End If
Public Class Form1 Private errchar As Boolean, chn As String, valide As Boolean Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load ' A000 -> A999 ' AA000->AA999 ' AB000->AB999 ' AC000->AC999 ' AD000->AD999 ' ...........->........... ' AZ000->AZ999 ' AAA000->AAA999 ' AAB000->AAB999 Saisie.Text = "" End Sub Private Sub Saisie_Click(sender As Object, e As EventArgs) Handles Saisie.Click ' on efface la zone de texte en cliquant dedans Saisie.Text = "" End Sub Private Sub Saisie_KeyPress(sender As Object, e As KeyPressEventArgs) Handles Saisie.KeyPress Dim caractere As String valide = False If e.KeyChar <> Convert.ToChar(13) Then Exit Sub valide = True Saisie.Text = Saisie.Text.ToUpper chn = Saisie.Text errchar = False If chn.Length < 4 Then MessageBox.Show("La saisie doit comporter au moins 4 caractères") Saisie.Text = "" errchar = True End If For i = 0 To chn.Length - 4 caractere = chn.Substring(i, 1) If caractere > "Z" Or caractere < "A" Then MessageBox.Show("Seuls les 3 derniers caractères doivent être des chiffres : ceux avant doivent être des lettres") Saisie.Text = "" errchar = True Exit For End If Next i If errchar = True Then Exit Sub chn = chn.Substring(chn.Length - 3) For i = 0 To chn.Length - 1 caractere = chn.Substring(i, 1) If caractere > "9" Or caractere < "0" Then MessageBox.Show("Vous ne devez tapez que des chiffres pour les 3 derniers caractères") Saisie.Text = "" errchar = True Exit For End If Next If errchar = False Then Increment() End Sub Private Sub Suivant_Click(sender As Object, e As EventArgs) Handles Suivant.Click Increment() End Sub Private Sub Increment() Dim valeur3 As Integer, chn3 As String, chnlettre As String, lettrefin As String, position As Integer Dim alphabet As String = "ABCDEFGHIJKLMNOPQRSTUVWXZ" If errchar = True Then MessageBox.Show("L'erreur à la saisie précédente n'a pas été corrigée ") Exit Sub End If chn = Saisie.Text ' test sur la valeur des trois chiffres valeur3 = Convert.ToInt32(chn.Substring(chn.Length - 3)) If valeur3 < 999 Then valeur3 = valeur3 + 1 chn3 = valeur3.ToString If chn3.Length < 3 Then chn3 = New String("0", 3 - chn3.Length) & chn3 Saisie.Text = Saisie.Text.Substring(0, Saisie.Text.Length - 3) & chn3 Else chn3 = "000" chnlettre = chn.Substring(0, chn.Length - 3) ' on récupère les lettres If chnlettre.Length = 1 Then ' on insère le deuxième A pour la première fois Saisie.Text = "AA" & chn3 Else chnlettre = chnlettre.Substring(1, chnlettre.Length - 1) lettrefin = chnlettre.Substring(chnlettre.Length - 1, 1) If lettrefin = "Z" Then lettrefin = "AA" ' le Z devient A et on rajoute un A à la suite Else position = alphabet.IndexOf(lettrefin) lettrefin = alphabet.Substring(position + 1, 1) End If If chnlettre.Length = 1 Then Saisie.Text = "A" & lettrefin & "000" Else Saisie.Text = "A" & chnlettre.Substring(0, chnlettre.Length - 1) & lettrefin & "000" End If End If End If Saisie.Refresh() End Sub End Class
Private Function GetNextID(ByVal lastID As String) As String ' rend le prochain id Dim word, ch, acc As String Dim flag As Boolean Dim num As Int64 num = Convert.ToInt64(lastID.Substring(lastID.Length - 3, 3)) word = lastID.Substring(0, lastID.Length - 3) If num < 999 Then num += 1 Return word & num.ToString.PadLeft(3, "0"c) Else num = 0 ch = word.Substring(1) flag = False acc = String.Empty If ch <> String.Empty AndAlso Convert.ToInt32(ch(ch.Length - 1)) < Convert.ToInt32("Z"c) Then Return "A" & ch.Substring(0, ch.Length - 1) & Convert.ToChar(Convert.ToInt32(ch(ch.Length - 1)) + 1).ToString & num.ToString.PadLeft(3, "0"c) Else For iter As Integer = ch.Length To 1 Step -1 If Convert.ToInt32(ch(iter - 1)) = Convert.ToInt32("Z"c) Then acc = String.Concat(acc, "A") Else If Not flag Then acc = Convert.ToChar(Convert.ToInt32(ch(iter - 1)) + 1) & acc & num.ToString.PadLeft(3, "0"c) flag = True Else acc = "A" & ch.Substring(0, 1) & acc End If End If Next If Not flag Then Return "AA" & acc & num.ToString.PadLeft(3, "0"c) Else Return "A" & acc End If End If End If End Function
Return word & num.ToString.PadLeft(3, "0"c)
Dim num As Int32 Dim str As String num = 1 str = num.ToString.PadLeft(3, Convert.ToChar("0")) 'ou str = num.ToString.PadLeft(3, "0"c)
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questionA000 -> A999 AA000->AA999 AB000->AB999 AC000->AC999
3 sept. 2013 à 08:27
Désolé, comme j'ai dit a Zemerlo, je n'ai pas été assez clair,
Voici la méthode d'incrémentation. L'essentiel est de garder un A comme préfixe :
Merci pour ton code je vais essayer d'adapter cela !
3 sept. 2013 à 09:28
après A999 on a B000 ? jusqu'à Z999 ?
donc quand on va arriver Z999 ,il faut ajouter une nouvel lettre A et l'autre Z est remis à A AA000 ?
3 sept. 2013 à 09:39
Ainsi on a toujours A devant quoi qu'il arrive.
Voila la suite logique :
POur l'instant je vais uniquement m'interesser de A000 jusqu'a AZ999.
En fait en y réflechissant, c'est comme les anciennes plaques d'immatriculation.
(Un numéro + deux lettres (pour les vieilles plaques hein ^^) + département)
3 sept. 2013 à 14:28
c'est une sorte de ToChar() ?