Pergunta

I wanna make only predefined characters to be able to used on my textbox in vb6. How can i achive that?

Predefined characters will be like 0-9 and A, C, M, E all other characters besides these gonna give a msgbox as err. it can also be a,c,m,e i can use Ucase() to solve it.

Foi útil?

Solução

You can;

private Sub atextbox_KeyPress(keyascii As Integer)
   if InStr(1, "0123456789ACME", Chr$(keyascii)) = 0 Then keyascii  = 0 '//case sensitive
End Sub

or

if Chr$(keyascii) like "[0-9]" or Chr$(keyascii) like "[ACMEacme]"

alternatively formatted

select case true
    case chr$(keyascii) like "[0-9]"
    case chr$(keyascii) like "[ACMEacme]"
    case else
        keyascii = 0
end select

Outras dicas

You can detect each character entered using the KeyPress event and checking the ASCII value. If you set it to 0, the press will be ignored. Be sure to also check in the Change event to catch pasting, etc.

Also, don't use a messagebox as this will annoy users.

Use the KeyPress event:

Private Sub txtBox_KeyPress(KeyAscii As Integer)
    Dim KeyChar As String
    If KeyAscii > 31 Then 'ignore low-ASCII characters like BACKSPACE
        KeyChar = Chr(KeyAscii)

        If Not IsAllowed(KeyChar) Then
          KeyAscii = 0
          MsgBox.Show("The allowed characters are ... ")
        End If
    End If
End Sub

The IsAllowed function will contain the allowed key codes.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top