BANNER

Seo Services

Limpiar TextBox en VB y C#

Encontré este código para limpiar todos los textbox de un formulario, aquí lo pongo en VB, me imagino cada quien lo hace a su manera, yo solo lo tengo aquí por cualquier cosa



'Declaramos nuestro metodo que hara la limpieza de los textbox
    Private Sub LimpiarTextBox(ByVal ofrm As Form)
        'hace un chequeo por todos los textbox del formulario
        For Each oControl As Control In ofrm.Controls
            If TypeOf oCobtrol Is TextBox Then
                oControl.Text = ""
            End If
        Next
    End Sub
 
   Private Sub BtnPruebaLimpiarTextBox_Click( _
       ByVal sender As System.Object, _
       ByVal e As System.EventArgs) Handles BtnPruebaLimpiarTextBox.Click
 
       Call LimpiarTextBox(Me)
 
   End Sub





Ahora si los textbox estan al interior de GrouBox entonces se puede usar este código


Public Class Form2
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        LimpiarCajas(GroupBox1)
    End Sub
    Sub LimpiarCajas(ByVal Grupo As Windows.Forms.GroupBox)
        Dim caja As TextBox
        For Each ctrl As Control In Grupo.Controls
            caja = TryCast(ctrl, TextBox)
            If Not (caja Is Nothing) Then
                caja.Clear()
            End If
        Next ctrl
    End Sub
End Class







Y para C#

// Declaramos nuestro metodo que hara la limpieza de los textbox
    private void LimpiarTextBox(Form ofrm)
    {
        // hace un chequeo por todos los textbox del formulario
        foreach (Control oControls in ofrm.Controls)
        {
            if (oControls is TextBox)
            {
                oControls.Text = ""; // eliminar el texto
           }
       }
   }
  
   private void BtnPruebaLimpiarTextBox_Click(System.Object sender, System.EventArgs e)
   {
       // pasar el formulario
       LimpiarTextBox(this);
  
   }





y de nuevo si los textbox estan dentro de un groupBox este codigo los limpia en C#


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
LimpiarCajas(groupBox1);
        }
        void LimpiarCajas(System.Windows.Forms.GroupBox Grupo)
        {
            TextBox caja = default(TextBox);
            foreach (Control ctrl in Grupo.Controls)
            {
                caja = ctrl as TextBox;
                if ((caja != null))
                {
                    caja.Clear();
                }
            }
        }
   
   }
}

2 comentarios:

  1. Vi este video y me funcionó y me permitió descargar el proyecto , se los comparto! https://www.youtube.com/watch?v=ATDyPCUVtHU&t=2s

    ResponderEliminar