Foros del Web » Programación para mayores de 30 ;) » .NET »

Limpiar todos los textbox de un formulario

Estas en el tema de Limpiar todos los textbox de un formulario en el foro de .NET en Foros del Web. Hola. Logré limpiar los textbox de un formulario. Pero si este formulario tiene textbox dentro de un GroupBox no los limpia. Código: Public Sub Limpiar_Cajas(ByVal ...
  #1 (permalink)  
Antiguo 10/11/2011, 06:34
 
Fecha de Ingreso: abril-2005
Mensajes: 483
Antigüedad: 19 años
Puntos: 3
Limpiar todos los textbox de un formulario

Hola. Logré limpiar los textbox de un formulario. Pero si este formulario tiene textbox dentro de un GroupBox no los limpia.

Código:
    Public Sub Limpiar_Cajas(ByVal f As Form)
        'Recorrer todos los controles del formulario indicado  
        For Each c As Control In f.Controls
            If TypeOf c Is TextBox Then
                c.Text = "" ' eliminar el texto  
            End If
        Next
    End Sub
¿De qué forma puedo hace que limpie todos los textbox inclusive los que están dentro del GroupBox?

Gracias desde ya por las respuestas.
Saludos
  #2 (permalink)  
Antiguo 10/11/2011, 08:36
Avatar de alan_69niupi  
Fecha de Ingreso: junio-2011
Mensajes: 200
Antigüedad: 12 años, 10 meses
Puntos: 17
Respuesta: Limpiar todos los textbox de un formulario

bueno yo simplemente hago eso

textbox.Clear()

aun y este dentro del GroupBox y me funciona. tambien podrias hacer
textbox.text=" "

aunque este ultimo solo desapacere lo escrito pero no lo limpia del todo
  #3 (permalink)  
Antiguo 10/11/2011, 09:37
Avatar de rikakashi  
Fecha de Ingreso: julio-2011
Mensajes: 226
Antigüedad: 12 años, 9 meses
Puntos: 33
Sonrisa Respuesta: Limpiar todos los textbox de un formulario

Yo lo hacia de forma similar pero para ello hay que llamar todos los controles en cada contenedor... usaba un codigo como este solo que en lugar de enable seria .tex=nothing

Código vb:
Ver original
  1. Dim c As Control
  2.         Dim g As Control
  3.         Dim p As Control
  4.  
  5.         For Each p In TabPage1.Controls
  6.             If TypeOf p Is Panel Then
  7.                 For Each g In p.Controls
  8.                     If TypeOf g Is GroupBox Then
  9.                         For Each c In g.Controls
  10.                             If TypeOf c Is TextBox Then
  11.                                 c.Enabled = True
  12.                             ElseIf TypeOf c Is ComboBox Then
  13.                                 c.Enabled = True
  14.                             ElseIf TypeOf c Is CheckBox Then
  15.                                 c.Enabled = True
  16.                             End If
  17.                         Next
  18.                     End If
  19.                 Next
  20.             End If
  21.         Next

pero tambien pudieras hacer un arreglo de todos tus textbox y despues un metodo para recorrer el arreglo ahora uso este metodo ya que valido todos mis controles

Código vb:
Ver original
  1. Public Sub limpiar1()
  2.         'LIMPIA LOS CONTROLES DE LA PRIMERA PESTAÑA
  3.        Dim arr_con_tp1() As Control = {txt_nom_completo, txt_1_nom, txt_2_nom, txt_1_ap, txt_2_ap, txt_rfc, _
  4.                                             txt_calle, txt_num_int, txt_num_ext, txt_limite, txt_num_locker, _
  5.                                             txt_fecha_alta, dtp_fecha_aniversario, dtp_fecha_cumpleaños, txt_fecha_ult_acc, txt_fecha_ult_comentario, txt_fecha_ult_consumo, txt_fecha_ult_pago, _
  6.                                             txt_valordecuota, txt_dep_garantia, txt_num_locker, _
  7.                                             cmb_titulo, cmb_edocivil, cmb_ptocardinal, cmb_pais, cmb_estado, cmb_ciudad, cmb_colonia, _
  8.                                             rad_fisico, chk_inha_cred, chk_tarj_credito, chk_cargo_aut, chk_factura, chk_carga_intereses, chk_consumo_minimo_salas, _
  9.                                             chk_notif_cxc, chk_notif_nf, chk_notif_np, _
  10.                                             pic_foto, pic_firma, txt_edad, txt_tipo_tarjeta, txt_num_tarj, dtp_fecha_aniversario, dtp_fecha_cumpleaños}
  11.         limpiacontroles(arr_con_tp1)
  12.          End Sub

Código vb:
Ver original
  1. Public Sub limpiacontroles(ByVal arr_con_tp1_1() As Control)
  2.         'REGRESAR EL CONTROL A UN FORMATO INICIAL SIN TEXTO NI ELEMENTOS
  3.        For j As Integer = 0 To arr_con_tp1_1.Length - 1
  4.             If (TypeOf arr_con_tp1_1(j) Is TextBox) Then               'SI ES TEXTBOX
  5.                CType(arr_con_tp1_1(j), TextBox).Text = Nothing
  6.             ElseIf (TypeOf arr_con_tp1_1(j) Is CheckBox) Then          'SI ES CHECKBOX
  7.                CType(arr_con_tp1_1(j), CheckBox).Checked = False
  8.             ElseIf (TypeOf arr_con_tp1_1(j) Is RadioButton) Then       'SI ES RADIOBUTOON
  9.                CType(arr_con_tp1_1(j), RadioButton).Checked = False
  10.             ElseIf (TypeOf arr_con_tp1_1(j) Is DateTimePicker) Then      'SI ES DATETIMEPICKER
  11.  
  12.                 If CType(arr_con_tp1_1(j), DateTimePicker).Name <> "dtp_fecha_aniversario" Then
  13.                     CType(arr_con_tp1_1(j), DateTimePicker).Value = CType(arr_con_tp1_1(j), DateTimePicker).MaxDate
  14.                     CType(arr_con_tp1_1(j), DateTimePicker).Format = DateTimePickerFormat.Custom
  15.                     CType(arr_con_tp1_1(j), DateTimePicker).CustomFormat = "dd/MMM/yyyy"
  16.                 ElseIf CType(arr_con_tp1_1(j), DateTimePicker).Name = "dtp_fecha_cumpleaños" Then
  17.                     CType(arr_con_tp1_1(j), DateTimePicker).Value = Date.Now.Day & "/" & Date.Now.Month & "/" & (Date.Now.Year - 18)
  18.                     CType(arr_con_tp1_1(j), DateTimePicker).Format = DateTimePickerFormat.Custom
  19.                     CType(arr_con_tp1_1(j), DateTimePicker).CustomFormat = "dd/MMM/yyyy"
  20.                 End If
  21.  
  22.             ElseIf (TypeOf arr_con_tp1_1(j) Is PictureBox) Then        'SI ES PICTUREBOX
  23.                If CType(arr_con_tp1_1(j), PictureBox).Name = "pic_foto" Or CType(arr_con_tp1_1(j), PictureBox).Name = "pic_adi_foto" Then
  24.                     CType(arr_con_tp1_1(j), PictureBox).ImageLocation = rutaimagenes & rutafotos & "\foto.png"
  25.                 ElseIf CType(arr_con_tp1_1(j), PictureBox).Name = "pic_firma" Then
  26.                     CType(arr_con_tp1_1(j), PictureBox).ImageLocation = rutaimagenes & rutafirmas & "\firma.png"
  27.                 ElseIf CType(arr_con_tp1_1(j), PictureBox).Name = "pic_adi_foto" Then
  28.                     CType(arr_con_tp1_1(j), PictureBox).ImageLocation = rutaimagenes & rutafotosadi & "\foto.png"
  29.                 End If
  30.             ElseIf (TypeOf arr_con_tp1_1(j) Is ComboBox) Then       'SI ES COMBOBOX
  31.                CType(arr_con_tp1_1(j), ComboBox).Items.Clear()
  32.                 CType(arr_con_tp1_1(j), ComboBox).Text = Nothing
  33.                 CType(arr_con_tp1_1(j), ComboBox).Refresh()
  34.                 End If
  35.         Next
  36.     End Sub



y cuando quiera limpiar solo llamo la funcion
Código vb:
Ver original
  1. limpiar()
y listo
__________________
la programación es tan grande como la imaginación (+.+)
  #4 (permalink)  
Antiguo 10/11/2011, 13:16
Avatar de eperedo  
Fecha de Ingreso: septiembre-2009
Ubicación: Perú
Mensajes: 654
Antigüedad: 14 años, 7 meses
Puntos: 16
Respuesta: Limpiar todos los textbox de un formulario

GroupBox también tiene una propiedad controls, tendrías que recorrerla e ir borrando las cajas de texto que estén dentro de él, tal como haces con el form.
__________________
Eduardo Peredo
Wigoin

Etiquetas: formulario, limpiar, textbox
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 06:41.