vascoc Posted June 22, 2012 Report Share Posted June 22, 2012 (edited) Boas, eu tenho uma textbox para passwords, e só gostava que aceitasse Numeros e Letras, espaço, e simbolos (|!"#$%&()=, etc) não... Isto é vb6, alguem sabe como passar para 2010 ? Private Sub PasswordTextBox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles PasswordTextBox.KeyPress PasswordTextBox.MaxLength = 10 '10 is max of characters allowed in the text box If KeyAscii >= 48 And KeyAscii <= 57 Then '0-9 'do something ElseIf KeyAscii >= 65 And KeyAscii <= 90 Then 'a-z 'do something ElseIf KeyAscii >= 97 And KeyAscii <= 122 Then 'A-Z 'do something ElseIf KeyAscii = 8 Then 'back space 'do something Else KeyAscii = vbCancel End If End Sub Edited June 22, 2012 by ribeiro55 Link to comment Share on other sites More sharing options...
Adão Posted June 22, 2012 Report Share Posted June 22, 2012 (edited) Põe o código no evento KeyPress: If Not Char.IsLetter(e.KeyChar) And _ Not Convert.ToInt32(e.KeyChar) = Keys.Back And _ Not Convert.ToInt32(e.KeyChar) = Keys.Space And _ Not Char.IsDigit(e.KeyChar) And _ Not Convert.ToInt32(e.KeyChar) = Keys.Delete Then Beep() e.Handled = True End If Edited June 22, 2012 by ribeiro55 Link to comment Share on other sites More sharing options...
vascoc Posted June 22, 2012 Author Report Share Posted June 22, 2012 (edited) Põe o código no evento KeyPress: If Not Char.IsLetter(e.KeyChar) And _ Not Convert.ToInt32(e.KeyChar) = Keys.Back And _ Not Convert.ToInt32(e.KeyChar) = Keys.Space And _ Not Char.IsDigit(e.KeyChar) And _ Not Convert.ToInt32(e.KeyChar) = Keys.Delete Then Beep() e.Handled = True End If Obrigado, mas, olha, ainda aceita o " . " e " º ª ", não dá para bloquear esses também ? Edited June 22, 2012 by ribeiro55 Link to comment Share on other sites More sharing options...
jlpcalado Posted June 22, 2012 Report Share Posted June 22, 2012 (edited) Direta/ no Text_Changed: Private Sub txtnumeros_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtnumeros.TextChanged If Not Char.IsLetterOrDigit(sender.text.Substring(sender.text.Length - 1, 1)) Then sender.text = sender.text.Substring(0, sender.text.Length - 1) sender.SelectionStart = sender.TextLength End If End Sub Edited June 22, 2012 by ribeiro55 Link to comment Share on other sites More sharing options...
vascoc Posted June 25, 2012 Author Report Share Posted June 25, 2012 Acontece o seguinte erro: Citação Direta/ no Text_Changed: Private Sub txtnumeros_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtnumeros.TextChanged If Not Char.IsLetterOrDigit(sender.text.Substring(sender.text.Length - 1, 1)) Then sender.text = sender.text.Substring(0, sender.text.Length - 1) sender.SelectionStart = sender.TextLength End If End Sub http://img23.imageshack.us/img23/8697/semttuloxgs.png Link to comment Share on other sites More sharing options...
Caça Posted June 25, 2012 Report Share Posted June 25, 2012 Primeiro deves colocar o sender no seu tipo correcto, não deves usar directamente o Object. Deves ter sempre o Option Strict ligado. Antes desse If deves verificar se tem algum texto. Pedro Martins Não respondo a duvidas por PM Link to comment Share on other sites More sharing options...
vascoc Posted June 25, 2012 Author Report Share Posted June 25, 2012 Certo, mas como eu só não quero que apareçam " . " e " º ª ", não dá para bloquear esses também, dando continuação ao código seguinte ? If Not Char.IsLetter(e.KeyChar) And _ Not Convert.ToInt32(e.KeyChar) = Keys.Back And _ Not Convert.ToInt32(e.KeyChar) = Keys.Space And _ Not Char.IsDigit(e.KeyChar) And _ Not Convert.ToInt32(e.KeyChar) = Keys.Delete Then Beep() e.Handled = True End If Link to comment Share on other sites More sharing options...
NunoDinis Posted June 25, 2012 Report Share Posted June 25, 2012 If Not Char.IsNumber(e.KeyChar) And Not e.KeyChar = vbBack And Not e.KeyChar = "." Then e.Handled = True End If Poe o código no evento KeyPress. Depois adaptas o "." ás tuas necessidades, ou seja "º" e "ª" . Espero ter ajudado. Estranha forma de vida que tem a capacidade de transformar comandos em mensagens de erro. ndsotware.org Link to comment Share on other sites More sharing options...
vascoc Posted June 27, 2012 Author Report Share Posted June 27, 2012 (edited) If Not Char.IsNumber(e.KeyChar) And Not e.KeyChar = vbBack And Not e.KeyChar = "." Then e.Handled = True End If Poe o código no evento KeyPress. Depois adaptas o "." ás tuas necessidades, ou seja "º" e "ª" . Espero ter ajudado. Então e como ficaria juntamente com este ? Para não aparecer nem . nem ºª If Not Char.IsLetter(e.KeyChar) And _ Not Convert.ToInt32(e.KeyChar) = Keys.Back And _ Not Convert.ToInt32(e.KeyChar) = Keys.Space And _ Not Char.IsDigit(e.KeyChar) And _ Not Convert.ToInt32(e.KeyChar) = Keys.Delete Then Beep() e.Handled = True End If Edited June 27, 2012 by vascoc Link to comment Share on other sites More sharing options...
NunoDinis Posted June 27, 2012 Report Share Posted June 27, 2012 (edited) experimenta assim. Nunca utilizei assim o código, mas creio que dará. If Not Char.IsLetter(e.KeyChar) And _ Not Convert.ToInt32(e.KeyChar) = Keys.Back And _ Not Convert.ToInt32(e.KeyChar) = Keys.Space And _ Not Char.IsDigit(e.KeyChar) And _ Not Convert.ToInt32(e.KeyChar) = Keys.Delete And _ Not e.KeyChar = "." And Not e.KeyChar = "º" And Not e.KeyChar = "ª" Then Beep() e.Handled = True End If Edited June 27, 2012 by NunoDinis Estranha forma de vida que tem a capacidade de transformar comandos em mensagens de erro. ndsotware.org Link to comment Share on other sites More sharing options...
vascoc Posted June 27, 2012 Author Report Share Posted June 27, 2012 (edited) experimenta assim. Nunca utilizei assim o código, mas creio que dará. If Not Char.IsLetter(e.KeyChar) And _ Not Convert.ToInt32(e.KeyChar) = Keys.Back And _ Not Convert.ToInt32(e.KeyChar) = Keys.Space And _ Not Char.IsDigit(e.KeyChar) And _ Not Convert.ToInt32(e.KeyChar) = Keys.Delete And _ Not e.KeyChar = "." And Not e.KeyChar = "º" And Not e.KeyChar = "ª" Then Beep() e.Handled = True End If Assim né ? Isso atrofiou com as cores 😛 Edited June 27, 2012 by vascoc Link to comment Share on other sites More sharing options...
NunoDinis Posted June 27, 2012 Report Share Posted June 27, 2012 .. o código que editei. Ficou todo marado -.- Estranha forma de vida que tem a capacidade de transformar comandos em mensagens de erro. ndsotware.org Link to comment Share on other sites More sharing options...
vascoc Posted June 27, 2012 Author Report Share Posted June 27, 2012 .. o código que editei. Ficou todo marado -.- Eu reparei, obrigado, mas foi sem sucesso, terá de ser de outra maneira, hummm Link to comment Share on other sites More sharing options...
NunoDinis Posted June 27, 2012 Report Share Posted June 27, 2012 na caixa de texto não queres nem ".", nem "ª", nem "º", certo ? Estranha forma de vida que tem a capacidade de transformar comandos em mensagens de erro. ndsotware.org Link to comment Share on other sites More sharing options...
vascoc Posted June 27, 2012 Author Report Share Posted June 27, 2012 (edited) na caixa de texto não queres nem ".", nem "ª", nem "º", certo ? Sim, porque o resto já está "tudo" bloqueado Edited June 27, 2012 by vascoc Link to comment Share on other sites More sharing options...
NunoDinis Posted June 27, 2012 Report Share Posted June 27, 2012 este código não deixa meteres esses caracteres: If Not Char.IsNumber(e.KeyChar) And Not e.KeyChar = vbBack And e.KeyChar = "." Then e.Handled = True End If If Not Char.IsNumber(e.KeyChar) And Not e.KeyChar = vbBack And e.KeyChar = "ª" Then e.Handled = True End If If Not Char.IsNumber(e.KeyChar) And Not e.KeyChar = vbBack And e.KeyChar = "º" Then e.Handled = True End If Poe no keypress.. Estranha forma de vida que tem a capacidade de transformar comandos em mensagens de erro. ndsotware.org Link to comment Share on other sites More sharing options...
vascoc Posted June 27, 2012 Author Report Share Posted June 27, 2012 Resultado final: If Not Char.IsLetter(e.KeyChar) And Not Convert.ToInt32(e.KeyChar) = Keys.Back And Not Convert.ToInt32(e.KeyChar) = Keys.Space And Not Char.IsDigit(e.KeyChar) And Not Convert.ToInt32(e.KeyChar) = Keys.Delete Then e.Handled = True End If If Not Char.IsNumber(e.KeyChar) And Not e.KeyChar = vbBack And e.KeyChar = "." Then e.Handled = True End If If Not Char.IsNumber(e.KeyChar) And Not e.KeyChar = vbBack And e.KeyChar = "ª" Then e.Handled = True End If If Not Char.IsNumber(e.KeyChar) And Not e.KeyChar = vbBack And e.KeyChar = "º" Then e.Handled = True End If End Sub esqueci-me foi da parte que também quero tirar os numeric Link to comment Share on other sites More sharing options...
NunoDinis Posted June 27, 2012 Report Share Posted June 27, 2012 então acresta mais este código no evento keypress: If Char.IsNumber(e.KeyChar) Then e.Handled = True End If Estranha forma de vida que tem a capacidade de transformar comandos em mensagens de erro. ndsotware.org Link to comment Share on other sites More sharing options...
vascoc Posted June 27, 2012 Author Report Share Posted June 27, 2012 Obrigado mais uma vez;) Resolvido! Link to comment Share on other sites More sharing options...
NunoDinis Posted June 27, 2012 Report Share Posted June 27, 2012 É o que interessa. De nada 🙂 Bom trabalho. Estranha forma de vida que tem a capacidade de transformar comandos em mensagens de erro. ndsotware.org Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now