habilitar boton por medio a textbox

17/11/2005 - 17:36 por Joel John | Informe spam
hola.
esto es una caja de texto que me habilita un boton dependiendo si hay datos
en ella. y funciona. pero:

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles TextBox1.TextChanged

If TextBox1.Text <> 0 Then
Button1.Enabled = True
Else
Button1.Enabled = False
End If

End Sub
cuando escribo algo se habilita (hasta ahi esta bien) pero en caso de que
retroceda o borre lo escrito inmediatamente da un error. ¿porque?
¿que esta mal?

Preguntas similare

Leer las respuestas

#6 Saga
17/11/2005 - 21:30 | Informe spam
Solo un comentario Javier...

En tu ejemplo de Text1.text <> "" and Text2.text <> ""
el boton se habilita solo si las tres cajas tienen algo, mientras que
en este ejemplo, el boton se habilita si cualquiera de las caja tiene
algun contenido. Si deseas duplicar el funcionamiento del primer
ejemplo, necesitas hacer alg asi:

Button1.Enabled = TextBox1.Text.Length > 0 and TextBox2.Text.Length >
0 and _
TextBox3.Text.Length > 0

Saludos
Saga

"Javier (Clevertec)" wrote
in message news:
Perdona, he tenido un fallo debido a que me ha pedido un relogin el
sistema,
el segundo ejemplo es, correctamente:

Button1.Enabled = TextBox1.Text.Length + TextBox2.Text.Length +
TextBox3.Text.Length > 0

Se podría mejorar el código para prevenir la ampliación, creando una
función
común, u otros métodos, pero si es sólo para tres no merece la pena.

Un saludo
Javier Muñoz


"Javier (Clevertec)" escribió:

Muy sencillo.

Pegas en cada una de las cajas lo siguiente:

Button1.Enabled = TextBox1.Text <> "" and TextBox2.Text <> "" and
TextBox3.Text <> ""

o, con la propiedad que apunta Jorge:

Button1.Enabled = TextBox1.Text.Length + TextBox1.Text.Length +
TextBox1.Text.Length > 0

Un saludo,
Javier Muñoz


"Joel John" escribió:

> hola.
> esto es una caja de texto que me habilita un boton dependiendo si
> hay datos
> en ella. y funciona. pero:
>
> Private Sub TextBox1_TextChanged(ByVal sender As System.Object,
> ByVal e As
> System.EventArgs) Handles TextBox1.TextChanged
>
> If TextBox1.Text <> 0 Then
> Button1.Enabled = True
> Else
> Button1.Enabled = False
> End If
>
> End Sub
> cuando escribo algo se habilita (hasta ahi esta bien) pero en caso
> de que
> retroceda o borre lo escrito inmediatamente da un error. ¿porque?
> ¿que esta mal?
Respuesta Responder a este mensaje
#7 Jorge Serrano [MVP VB]
17/11/2005 - 21:52 | Informe spam
Hagámoslo más sencillo aún (usando el evento del TextBox1):

Tal y como entiendo el ejemplo, si las tres cajas están vacías, entonces el
botón se deshabilita.

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles TextBox1.TextChanged, TextBox2.TextChanged,
TextBox3.TextChanged
If TextBox1.Text.Length = 0 And _
TextBox2.Text.Length = 0 And _
TextBox3.Text.Length = 0 Then
Button1.Enabled = False
Else
Button1.Enabled = True
End If
End Sub

Extendiendo lo que apuntaba Javier, tambiénse puede solucionar esto de esta
forma:

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles TextBox1.TextChanged, TextBox2.TextChanged,
TextBox3.TextChanged
If (TextBox1.Text.Length + TextBox2.Text.Length +
TextBox3.Text.Length) > 0 Then
Button1.Enabled = True
Else
Button1.Enabled = False
End If
End Sub



Jorge Serrano Pérez
Microsoft MVP VB.NET
PortalVB.com
http://www.portalvb.com/
Weblog de Jorge Serrano
http://weblogs.golemproject.com/jorge/


"Saga" wrote:


Solo un comentario Javier...

En tu ejemplo de Text1.text <> "" and Text2.text <> ""
el boton se habilita solo si las tres cajas tienen algo, mientras que
en este ejemplo, el boton se habilita si cualquiera de las caja tiene
algun contenido. Si deseas duplicar el funcionamiento del primer
ejemplo, necesitas hacer alg asi:

Button1.Enabled = TextBox1.Text.Length > 0 and TextBox2.Text.Length >
0 and _
TextBox3.Text.Length > 0

Saludos
Saga

"Javier (Clevertec)" wrote
in message news:
> Perdona, he tenido un fallo debido a que me ha pedido un relogin el
> sistema,
> el segundo ejemplo es, correctamente:
>
> Button1.Enabled = TextBox1.Text.Length + TextBox2.Text.Length +
> TextBox3.Text.Length > 0
>
> Se podría mejorar el código para prevenir la ampliación, creando una
> función
> común, u otros métodos, pero si es sólo para tres no merece la pena.
>
> Un saludo
> Javier Muñoz
>
>
> "Javier (Clevertec)" escribió:
>
>> Muy sencillo.
>>
>> Pegas en cada una de las cajas lo siguiente:
>>
>> Button1.Enabled = TextBox1.Text <> "" and TextBox2.Text <> "" and
>> TextBox3.Text <> ""
>>
>> o, con la propiedad que apunta Jorge:
>>
>> Button1.Enabled = TextBox1.Text.Length + TextBox1.Text.Length +
>> TextBox1.Text.Length > 0
>>
>> Un saludo,
>> Javier Muñoz
>>
>>
>> "Joel John" escribió:
>>
>> > hola.
>> > esto es una caja de texto que me habilita un boton dependiendo si
>> > hay datos
>> > en ella. y funciona. pero:
>> >
>> > Private Sub TextBox1_TextChanged(ByVal sender As System.Object,
>> > ByVal e As
>> > System.EventArgs) Handles TextBox1.TextChanged
>> >
>> > If TextBox1.Text <> 0 Then
>> > Button1.Enabled = True
>> > Else
>> > Button1.Enabled = False
>> > End If
>> >
>> > End Sub
>> > cuando escribo algo se habilita (hasta ahi esta bien) pero en caso
>> > de que
>> > retroceda o borre lo escrito inmediatamente da un error. ¿porque?
>> > ¿que esta mal?



Respuesta Responder a este mensaje
#8 Joel John
17/11/2005 - 22:25 | Informe spam
gracias por compartir sus ideas. muy acertadas.


"Jorge Serrano [MVP VB]" wrote:

Hagámoslo más sencillo aún (usando el evento del TextBox1):

Tal y como entiendo el ejemplo, si las tres cajas están vacías, entonces el
botón se deshabilita.

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles TextBox1.TextChanged, TextBox2.TextChanged,
TextBox3.TextChanged
If TextBox1.Text.Length = 0 And _
TextBox2.Text.Length = 0 And _
TextBox3.Text.Length = 0 Then
Button1.Enabled = False
Else
Button1.Enabled = True
End If
End Sub

Extendiendo lo que apuntaba Javier, tambiénse puede solucionar esto de esta
forma:

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles TextBox1.TextChanged, TextBox2.TextChanged,
TextBox3.TextChanged
If (TextBox1.Text.Length + TextBox2.Text.Length +
TextBox3.Text.Length) > 0 Then
Button1.Enabled = True
Else
Button1.Enabled = False
End If
End Sub



Jorge Serrano Pérez
Microsoft MVP VB.NET
PortalVB.com
http://www.portalvb.com/
Weblog de Jorge Serrano
http://weblogs.golemproject.com/jorge/


"Saga" wrote:

>
> Solo un comentario Javier...
>
> En tu ejemplo de Text1.text <> "" and Text2.text <> ""
> el boton se habilita solo si las tres cajas tienen algo, mientras que
> en este ejemplo, el boton se habilita si cualquiera de las caja tiene
> algun contenido. Si deseas duplicar el funcionamiento del primer
> ejemplo, necesitas hacer alg asi:
>
> Button1.Enabled = TextBox1.Text.Length > 0 and TextBox2.Text.Length >
> 0 and _
> TextBox3.Text.Length > 0
>
> Saludos
> Saga
>
> "Javier (Clevertec)" wrote
> in message news:
> > Perdona, he tenido un fallo debido a que me ha pedido un relogin el
> > sistema,
> > el segundo ejemplo es, correctamente:
> >
> > Button1.Enabled = TextBox1.Text.Length + TextBox2.Text.Length +
> > TextBox3.Text.Length > 0
> >
> > Se podría mejorar el código para prevenir la ampliación, creando una
> > función
> > común, u otros métodos, pero si es sólo para tres no merece la pena.
> >
> > Un saludo
> > Javier Muñoz
> >
> >
> > "Javier (Clevertec)" escribió:
> >
> >> Muy sencillo.
> >>
> >> Pegas en cada una de las cajas lo siguiente:
> >>
> >> Button1.Enabled = TextBox1.Text <> "" and TextBox2.Text <> "" and
> >> TextBox3.Text <> ""
> >>
> >> o, con la propiedad que apunta Jorge:
> >>
> >> Button1.Enabled = TextBox1.Text.Length + TextBox1.Text.Length +
> >> TextBox1.Text.Length > 0
> >>
> >> Un saludo,
> >> Javier Muñoz
> >>
> >>
> >> "Joel John" escribió:
> >>
> >> > hola.
> >> > esto es una caja de texto que me habilita un boton dependiendo si
> >> > hay datos
> >> > en ella. y funciona. pero:
> >> >
> >> > Private Sub TextBox1_TextChanged(ByVal sender As System.Object,
> >> > ByVal e As
> >> > System.EventArgs) Handles TextBox1.TextChanged
> >> >
> >> > If TextBox1.Text <> 0 Then
> >> > Button1.Enabled = True
> >> > Else
> >> > Button1.Enabled = False
> >> > End If
> >> >
> >> > End Sub
> >> > cuando escribo algo se habilita (hasta ahi esta bien) pero en caso
> >> > de que
> >> > retroceda o borre lo escrito inmediatamente da un error. ¿porque?
> >> > ¿que esta mal?
>
>
>
Respuesta Responder a este mensaje
#9 Javier (Clevertec)
18/11/2005 - 01:46 | Informe spam
Aunque el hilo está ya terminado, no me resisto a hacer un comentario.

Tienes razón, Saga, gracias por corregirme. La verdad es que a esas alturas
ya no tenía muy claro lo que pretendía nuestro amigo Joel.

Yo, como soy tan anticuado, y tuve que lidiar con los ahorros de espacio en
código y en tiempos de proceso, procuro evitar, si es posible, los If, aunque
el código quede menos claro.

Por eso vuelvo a aportar la idea:

Button1.Enabled = TextBox1.Text.Length + TextBox2.Text.Length +
TextBox3.Text.Length = 0

queda raro, ¿verdad?, pero es perfectamente funcional; quizás se entienda
mejor así:

Button1.Enabled = (TextBox1.Text.Length + TextBox2.Text.Length +
TextBox3.Text.Length = 0)

Un saludo,
Javier Muñoz


"Saga" escribió:


Solo un comentario Javier...

En tu ejemplo de Text1.text <> "" and Text2.text <> ""
el boton se habilita solo si las tres cajas tienen algo, mientras que
en este ejemplo, el boton se habilita si cualquiera de las caja tiene
algun contenido. Si deseas duplicar el funcionamiento del primer
ejemplo, necesitas hacer alg asi:

Button1.Enabled = TextBox1.Text.Length > 0 and TextBox2.Text.Length >
0 and _
TextBox3.Text.Length > 0

Saludos
Saga

"Javier (Clevertec)" wrote
in message news:
> Perdona, he tenido un fallo debido a que me ha pedido un relogin el
> sistema,
> el segundo ejemplo es, correctamente:
>
> Button1.Enabled = TextBox1.Text.Length + TextBox2.Text.Length +
> TextBox3.Text.Length > 0
>
> Se podría mejorar el código para prevenir la ampliación, creando una
> función
> común, u otros métodos, pero si es sólo para tres no merece la pena.
>
> Un saludo
> Javier Muñoz
>
>
> "Javier (Clevertec)" escribió:
>
>> Muy sencillo.
>>
>> Pegas en cada una de las cajas lo siguiente:
>>
>> Button1.Enabled = TextBox1.Text <> "" and TextBox2.Text <> "" and
>> TextBox3.Text <> ""
>>
>> o, con la propiedad que apunta Jorge:
>>
>> Button1.Enabled = TextBox1.Text.Length + TextBox1.Text.Length +
>> TextBox1.Text.Length > 0
>>
>> Un saludo,
>> Javier Muñoz
>>
>>
>> "Joel John" escribió:
>>
>> > hola.
>> > esto es una caja de texto que me habilita un boton dependiendo si
>> > hay datos
>> > en ella. y funciona. pero:
>> >
>> > Private Sub TextBox1_TextChanged(ByVal sender As System.Object,
>> > ByVal e As
>> > System.EventArgs) Handles TextBox1.TextChanged
>> >
>> > If TextBox1.Text <> 0 Then
>> > Button1.Enabled = True
>> > Else
>> > Button1.Enabled = False
>> > End If
>> >
>> > End Sub
>> > cuando escribo algo se habilita (hasta ahi esta bien) pero en caso
>> > de que
>> > retroceda o borre lo escrito inmediatamente da un error. ¿porque?
>> > ¿que esta mal?



Respuesta Responder a este mensaje
#10 Karlos Traveceras
18/11/2005 - 19:01 | Informe spam
Trata de la siguiente manera

dentro del mismo evento textchanged

if textbox1.text.lentgh >0 then
boton1.enable=true
else
boton.enable=false
endif

Suerte



"Joel John" wrote:

hola.
esto es una caja de texto que me habilita un boton dependiendo si hay datos
en ella. y funciona. pero:

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles TextBox1.TextChanged

If TextBox1.Text <> 0 Then
Button1.Enabled = True
Else
Button1.Enabled = False
End If

End Sub
cuando escribo algo se habilita (hasta ahi esta bien) pero en caso de que
retroceda o borre lo escrito inmediatamente da un error. ¿porque?
¿que esta mal?
email Siga el debate Respuesta Responder a este mensaje
Ads by Google
Help Hacer una pregunta AnteriorRespuesta Tengo una respuesta
Search Busqueda sugerida