Condicionar Textbox a un rango determinado ??

(1/2) > >>

luis456:
Hola jejej a todos vuelvo con una chorrada

tengo varios TextBox y quiero que a partir del sexto no  me acepte números mayores a 45 y que cuando el usuario ponga un numero mayor a 45 no deje continuar el programa hasta poner un numero menor a 45 estoy con este codigo simplón pero deja que siga el programa auqnue le avise al usuario

Código
Private Sub TextBox6_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles TextBox6.Validating
       If (Int(TextBox6.Text) < 0 Or Int(TextBox6.Text) > 45) Then
 
           MsgBox("Numero mayor que 45 en casilla 6 introduzca numero correcto")
 
       End If
 
   End Sub  


saludos
Luis


Eleкtro:
Hacer eso es reinventar la rueda, te sugiero usar un NumericUpDown, le pones valor máximo:45 valor mínimo:0 y ya lo tienes todo hecho.

De todas formas si sigues prefiriendo usar un TextBox dímelo y te explico como (con un ErrorProvider sería lo ideal).

Saludos!

luis456:
Cita de: Eleкtro en 11 Marzo 2016, 14:48 pm

Hacer eso es reinventar la rueda, te sugiero usar un NumericUpDown, le pones valor máximo:45 valor mínimo:0 y ya lo tienes todo hecho.

De todas formas si sigues prefiriendo usar un TextBox dímelo y te explico como (con un ErrorProvider sería lo ideal).

Saludos!


Hola elektro bueno cambiar por  numeric me supondria hacer un monton de cambios en el codigo yo pensaba en algo mas facil , ya te aviso que hare, voy a mirar primero lo del error ese que pusiste

gracias

Luis

Eleкtro:
Cita de: luis456 en 11 Marzo 2016, 15:34 pm

yo pensaba en algo mas facil


Estoy casi seguro que la pregunta que has formulado ya la resolviste hace tiempo...
http://foro.elhacker.net/net/ayuda_como_hacer_para_validar_una_text_box_para_q_admita_solo_numeros-t429160.0.html;msg1994277#msg1994277
...no exactamente en ese post, pero no encuentro donde te mostré un ejemplo.

En fin, lo más sencillo es que uses un NumericUpDown, o heredar la Class de ese control para hacerlo más similar a un TextBox:

Código
Public Class NumericTextBox : Inherits NumericUpDown
 
   Public Sub New()
       MyBase.New()
       MyBase.Controls(0).Visible = False
   End Sub
 
   Protected Overrides Sub OnTextBoxResize(ByVal source As Object, ByVal e As EventArgs)
       MyBase.Controls(0).Size = New Size(0, MyBase.ClientSize.Height)
       MyBase.Controls(1).Size = New Size(MyBase.ClientSize.Width, MyBase.ClientSize.Height)
   End Sub
 
End Class
(no está pulido, es un ejemplo breve)


Con un TextBox corriente puedes hacerlo de la siguiente manera:
Código
   Private Sub TextBox1_Keypress(ByVal sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
 
       e.Handled = Not Char.IsNumber(e.KeyChar)
 
   End Sub
 
   Private Sub TextBox1_TextChanged(ByVal sender As Object, e As EventArgs) Handles TextBox1.TextChanged
 
       Dim ctrl As TextBox = DirectCast(sender, TextBox)
 
       If CInt(ctrl.Text) > 45 Then
           ctrl.Text = "45"
       End If
 
   End Sub



luis456:
Hola Elektro

Me decante por la segunda propuesta

Con un TextBox corriente puedes hacerlo de la siguiente manera:
Código
   Private Sub TextBox1_Keypress(ByVal sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
 
       e.Handled = Not Char.IsNumber(e.KeyChar)
 
   End Sub
 
   Private Sub TextBox1_TextChanged(ByVal sender As Object, e As EventArgs) Handles TextBox1.TextChanged
 
       Dim ctrl As TextBox = DirectCast(sender, TextBox)
 
       If CInt(ctrl.Text) > 45 Then
           ctrl.Text = "45"
       End If
 
   End Sub

[/quote]


Pero claro siempre hay un pero jejje

error
La conversión de la cadena "" en el tipo 'Integer' no es válida.

Bien te enseño como tengo esto montado


Código
Private Sub TextBox6_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox6.KeyPress, TextBox5.KeyPress, TextBox4.KeyPress, TextBox3.KeyPress, TextBox2.KeyPress, TextBox1.KeyPress
       Button1.Enabled = ((TextBox1.TextLength > 0) And (TextBox2.TextLength > 0) And (TextBox3.TextLength > 0) And (TextBox4.TextLength > 0) And (TextBox5.TextLength > 0) And (TextBox6.TextLength > 0))
       'And (TextBox7.TextLength > 0))
 
 
       solonumeros(e)
       If e.KeyChar = ChrW(Keys.Enter) Then
           e.Handled = True
           SendKeys.Send("{TAB}")
 
       End If
 
 
   End Sub


puse tu código

de esta forma y si retrocede a 45 si pongo un numero mayor pero me salta el error


Código
 Private Sub TextBox6_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox6.KeyPress, TextBox5.KeyPress, TextBox4.KeyPress, TextBox3.KeyPress, TextBox2.KeyPress, TextBox1.KeyPress
       Button1.Enabled = ((TextBox1.TextLength > 0) And (TextBox2.TextLength > 0) And (TextBox3.TextLength > 0) And (TextBox4.TextLength > 0) And (TextBox5.TextLength > 0) And (TextBox6.TextLength > 0))
       'And (TextBox7.TextLength > 0))
       e.Handled = Not Char.IsNumber(e.KeyChar)
 
       solonumeros(e)
       If e.KeyChar = ChrW(Keys.Enter) Then
           e.Handled = True
           SendKeys.Send("{TAB}")
 
       End If
 
   End Sub
 
   Private Sub TextBox6_TextChanged(ByVal sender As Object, ByVal e As EventArgs) Handles TextBox6.TextChanged
 
       Dim ctrl As TextBox = DirectCast(sender, TextBox)
 
       If CInt(ctrl.Text) > 45 Then
           ctrl.Text = "45"
       End If
 
   End Sub


Luis




Navegación

[0] Índice de Mensajes

[#] Página Siguiente