Masque de saisie pou ip

bendsiham Messages postés 78 Date d'inscription mercredi 17 juin 2009 Statut Membre Dernière intervention 23 juin 2017 - 14 juin 2010 à 18:29
 Utilisateur anonyme - 23 mars 2013 à 17:07
bonjour

je voudrais savoir comment je peux faire un masque de saisie dans un textbox pour saisir une adresse ip
sous vb.net

Merci d'avance

4 réponses

cs_ghuysmans99 Messages postés 3982 Date d'inscription jeudi 14 juillet 2005 Statut Membre Dernière intervention 30 juin 2013 16
14 juin 2010 à 18:54
En cherchant un peu sur Google on trouve ceci en premier résultat : http://www.vbforums.com/showthread.php?t=430169

VB.NET is good ... VB6 is better
Utilise Réponse acceptée quand un post répond à ta question
0
lamoussaserge
21 mars 2013 à 11:32
Bonjour!

Ce n'est pas un masque de saisie mais c'est tout comme.
C'est surtout pour aider les personnes pour qui c'est d'actualité en ce moment:
ip_Change() est une macro qui faut rattacher au textbox concerné.

Private Sub ip_Change()
Dim element As Integer
Dim cpt As Integer
element = UBound(Split(ip, "."))
If Not IsNumeric(Right(ip, 1)) And Right(ip, 1) <> "." And Right(ip, 1) <> "" Then
MsgBox "Le caractere saisi doit être un chiffre", vbExclamation
ip = Left(ip, Len(ip) - 1)
Exit Sub
End If
If Right(ip, 1) "." And InStr(ip, ".") 1 Then
MsgBox "Format incorrecte", vbExclamation
ip = Left(ip, Len(ip) - 1)
ElseIf Right(ip, 1) = "." And InStr(ip, ".") <> 1 Then
For cpt = 0 To element - 1
If Not IsNumeric(Split(ip, ".")(cpt)) Then
MsgBox "Format incorecte", vbExclamation
ip = Left(ip, Len(ip) - (Len(Split(ip, ".")(cpt)) + 1))
Exit Sub
ElseIf CInt(Split(ip, ".")(cpt)) > 255 Then
MsgBox "Format incorecte", vbExclamation
ip = Left(ip, Len(ip) - (Len(Split(ip, ".")(cpt)) + 1))
Exit Sub
End If
Next cpt
If nbOccurences(".", ip.Value) > 3 Then
MsgBox "Format incorecte", vbExclamation
ip = Left(ip, Len(ip) - 1)
Exit Sub
End If
End If
If nbOccurences(".", ip.Value) = 3 And ip.Value <> "" Then
If Split(ip, ".")(element) <> "" Then
If (CInt(Split(ip, ".")(element)) > 255) Then
MsgBox "Format incorecte", vbExclamation
ip = Left(ip, Len(ip) - 1)
Exit Sub
End If
End If
End If
End Sub


Public Function nbOccurences(car As String, chaine As String) As Integer
Dim i As Integer, res As Integer
res = 0
For i = 1 To Len(chaine)
If Mid(chaine, i, 1) = car Then
res = res + 1
End If
Next
nbOccurences = res
End Function
0
cs_ghuysmans99 Messages postés 3982 Date d'inscription jeudi 14 juillet 2005 Statut Membre Dernière intervention 30 juin 2013 16
21 mars 2013 à 19:06
nbOccurences est inutile, tu peux avoir le nombre de parties avec UBound(element)+1.
Faute d'ortho aussi : Format incorect

VB.NET is good ... VB6 is better
Utilise Réponse acceptée quand un post répond à ta question
0
Utilisateur anonyme
23 mars 2013 à 17:07
Bonjour,

Nous sommes bien d'accord qu'il s'agit bien de VB.NET?
(L'actuel section: Accueil > Forum > VB.NET et VB 2005 >)

On pourrait imaginer ça par exemple:
    Private Sub ip_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles ip.LostFocus
        Verif()
    End Sub

    Private Sub ip_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ip.TextChanged
        ip.Text = Replace(ip.Text, "..", ".")
        ip.SelectionStart = ip.Text.Length
        If ip.Text.Length > 0 Then
            Dim Str As String = ip.Text.ToCharArray(ip.Text.Length - 1, 1)
            If IsNumeric(Str) False And Str <> "." Then ip.Text ip.Text.ToCharArray(0, ip.Text.Length - 1)
        End If
    End Sub

    Private Sub Verif()
        Dim i As Integer = 0
        For Each Valeur In ip.Text.Split(".")
            If Val(Valeur) > 255 Then
                MessageBox.Show("L'adresse IP est composée de quatre valeurs inférieures à 256 séparées par des '.'")
                ip.Text = ""
                ip.Focus()
            End If
            i += 1
        Next
        If i <> 4 Then
            MessageBox.Show("L'adresse IP est composée de quatre valeurs inférieures à 256 séparées par des '.'")
            ip.Text = ""
            ip.Focus()
        End If
    End Sub


Ou encore jouer avec la propriété e.SuppressKeyPress dans ip_KeyDown...


Cordialement


CF2i - Guadeloupe
Ingénierie Informatique
0
Rejoignez-nous