Visual Basic.NET - Ayuda con Textbox

 
Vista:
sin imagen de perfil

Ayuda con Textbox

Publicado por SandraP (12 intervenciones) el 07/08/2017 14:25:26
Hola, tengo un Textbox que:

1. Solo debe aceptar números con rango entre cero y 36 (00-36)
2. Que solo acepte 2 caracteres numéricos
3. Cuando escribe el segundo carácter numérico el foco se me vaya al siguiente textbox sin necesidad de la tecla enter,
4. Si presiona Enter estando el textbox vacio no se mueva.

Me esta funcionando, pero cuando escribe el primer carácter en textbox y borra se me va al siguiente textbox

Este es mi código:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Private Sub TextNumero_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextNumero.KeyPress
	If Char.IsNumber(e.KeyChar) Then
		e.Handled = False
	ElseIf Char.IsControl(e.KeyChar) Then
		e.Handled = False
	Else
		e.Handled = True
	End If
 
	If Asc(e.KeyChar) = 13 And (Val(TextNumero.Text) <> 0) Or Len(TextNumero.Text) = 1 Then
		TextNumero.BackColor = Color.White
		Textmonto.BackColor = Color.Yellow
		Textmonto.Focus()
 
	Else
		TextNumero.BackColor = Color.Yellow
		TextNumero.Focus()
	End If
End Sub

Pues eso...espero me den luces...

Saludos
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
Imágen de perfil de Diego
Val: 605
Bronce
Ha mantenido su posición en Visual Basic.NET (en relación al último mes)
Gráfica de Visual Basic.NET

Ayuda con Textbox

Publicado por Diego (190 intervenciones) el 09/08/2017 13:55:04
Hola Sandra, te paso el codigo comentado para que entiendas y recomendacion, poner el valor de MaxLenght de TEXTNUMERO en 2...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
Private Sub textNumero_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles textNumero.Enter
	If textNumero.Text = "00" Then textNumero.Text = Val(textNumero.Text)
End Sub
 
Private Sub TextNumero_KeyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs) Handles textNumero.KeyPress
	If Char.IsNumber(e.KeyChar) Then
		If textNumero.TextLength = 0 And e.KeyChar <= "3" Then 'Si es el primero digito y es menor que 3
			e.Handled = False
		ElseIf textNumero.TextLength = 1 AndAlso e.KeyChar <= "6" Then 'Si es el segundo digito y es menor que 6
			e.Handled = False
		Else 'Cualquier otro digito en cualquier otra posicion, lo anula
			e.Handled = True
		End If
	ElseIf e.KeyChar = vbBack Then
		e.Handled = False
	Else
		e.Handled = True
	End If
 
	If e.KeyChar = vbCr AndAlso (Val(textNumero.Text) <> 0) Then
		textNumero.BackColor = Color.White
		textMonto.BackColor = Color.Yellow
		textMonto.Focus()
 
	Else
		textNumero.BackColor = Color.Yellow
		textNumero.Focus()
	End If
End Sub
 
Private Sub textNumero_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles textNumero.KeyUp
	If textNumero.TextLength = 2 Then 'Al quitar el dedo de la 2da pulsacion salta al campo siguiente
		If Not e.KeyCode = Keys.ShiftKey And Not e.KeyCode = Keys.Tab Then 'Comprueba que no estes volviendo con un shift-tab
			textNumero.BackColor = Color.White
			textMonto.BackColor = Color.Yellow
			textMonto.Focus()
		End If
	End If
End Sub
Private Sub textNumero_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles textNumero.Leave
	If textNumero.TextLength = 0 Then textNumero.Text = "00" : Exit Sub 'Formatea el texto antes de salir del textbox
	textNumero.Text = Format(Val(textNumero.Text), "00")
End Sub

Como ves, hay varios eventos que tenes que controlar sobre el textbox.
Espero te sirva.
Saludos y +Bytes.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
1
Comentar
sin imagen de perfil

Ayuda con Textbox

Publicado por SandraP (12 intervenciones) el 10/08/2017 12:00:02
Muchas gracias Diego !!!!

Funciona muy bien
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar