Invite de commande : textbox locker

Contenu du snippet

Cette source montre comment bloquer un textbox une fois que l'utilisateur à cliqué sur Entrer. L'utilisateur ne peut alors plus éditer le contenus du textBox mais il peut écrire à la fin (un peut comme command.com).

Source / Exemple :


'Code à mettre dans votre Feuille
'Votre textBox : Text1

Private LockedLength As Long
Private LockedString As String

Private Sub Text1_Change()
    If LockedLength = 0 Or LockedString = "" Then Exit Sub
    TextBoxLocker Text1, -1
End Sub

Private Sub Text1_KeyPress(KeyAscii As Integer)
    KeyAscii = TextBoxLocker(Text1, KeyAscii, "Shell> ")
End Sub

Public Function TextBoxLocker(ByVal ObjTextBox As TextBox, ByRef KeyAscii As Integer, Optional Chaine As String = "") As Integer
    If KeyAscii = -1 Then
        If Left(ObjTextBox.Text, LockedLength) <> LockedString Then
            ObjTextBox.Text = LockedString
            ObjTextBox.SelStart = LockedLength
        End If
        Exit Function
    End If
    TextBoxLocker = KeyAscii
    If ObjTextBox.SelStart < LockedLength Then
        ObjTextBox.SelStart = LockedLength
        TextBoxLocker = 0
        Exit Function
    End If
    Select Case KeyAscii
    Case 8
        If ObjTextBox.SelStart <= LockedLength Then
            TextBoxLocker = 0
            Exit Function
        End If
    Case 13
        ObjTextBox.Text = ObjTextBox.Text & vbNewLine & Chaine
        ObjTextBox.SelStart = Len(ObjTextBox.Text)
        LockedLength = Len(ObjTextBox.Text)
        LockedString = Left(ObjTextBox.Text, LockedLength)
        TextBoxLocker = 0
    End Select
End Function

Conclusion :


Ce code est le début d'un invite de commande. D'autres bouts de code viendront par la suite.

A voir également

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.