Jump to content
Sign in to follow this  
Asgorath

[vb.net 2003] Classe que usa Reflection para percorrer componentes de uma form

Recommended Posts

Asgorath

Option Explicit On 
Option Strict On
Imports System.Reflection



Class ClassLimpaTexto

    '' Versão 1 - dia 20-04-2004
    '' Classe para percorrer todos os controlos/componentes de um form e que limpa as respectivas caixas de texto ou comboboxes.


#Region "Revisões"
    '' Revisão 1 - dia 17-08-2004
    '' Novo procedure LimpaCheckBoxes

#End Region
    Public Sub LimpaTextBoxes(ByVal f As Form)
        '' limpa os textboxes
        Dim meuForm As Type = f.GetType()
        Dim campos As FieldInfo() = meuForm.GetFields(BindingFlags.Instance Or BindingFlags.NonPublic)
        For Each campo As FieldInfo In campos
            If campo.FieldType.Name.ToLower = "textbox" Then
                Dim t As TextBox = DirectCast(campo.GetValue(f), TextBox)
                t.Text = String.Empty
            End If
        Next
    End Sub
   

    Public Sub LimpaComboBoxes(ByVal f As Form)
        '' limpa os comboboxes
        Dim meuForm As Type = f.GetType()
        Dim campos As FieldInfo() = meuForm.GetFields(BindingFlags.Instance Or BindingFlags.NonPublic)
        For Each campo As FieldInfo In campos
            If campo.FieldType.Name.ToLower = "combobox" Then
                Dim c As ComboBox = DirectCast(campo.GetValue(f), ComboBox)
                c.Text = String.Empty
            End If
        Next

    End Sub

    Public Sub LimpaCheckBoxes(ByVal f As Form)
        '' desactiva os checkboxes
        Dim meuForm As Type = f.GetType()
        Dim campos As FieldInfo() = meuForm.GetFields(BindingFlags.Instance Or BindingFlags.NonPublic)
        For Each campo As FieldInfo In campos
            If campo.FieldType.Name.ToLower = "checkbox" Then
                Dim c As CheckBox = DirectCast(campo.GetValue(f), CheckBox)
                c.Checked = False
            End If
        Next

    End Sub

    Public Sub LimpaRadioButton(ByVal f As Form)
        '' desactiva os radiobuttons
        Dim meuForm As Type = f.GetType()
        Dim campos As FieldInfo() = meuForm.GetFields(BindingFlags.Instance Or BindingFlags.NonPublic)
        For Each campo As FieldInfo In campos
            If campo.FieldType.Name.ToLower = "radiobutton" Then
                Dim c As RadioButton = DirectCast(campo.GetValue(f), RadioButton)
                c.Checked = False
            End If
        Next

    End Sub


End Class

Como usar :

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim limpa As New ClassLimpaTexto
        limpa.LimpaTextBoxes(Me)
End Sub

Atentamente

Jorge


"The Dark Side Clouds Everthing. Impossible To See The Future Is."My rig: Intel Core 2 Quad Q9450 | abit IP35P | 4G Kingston 800 Mhz | XFX GeForce 9800 GX2 1G ddr3 | 2X WD5000AAJS 500Gb Sata 2 | PSU 600W || Caixa El-Diablo | Creative XMod

Share this post


Link to post
Share on other sites
deathseeker25

Um bom tutorial Asgorath. Sou um newb em VB.NET, mas fico contente ao saber que existe gente a querer dar este tipo de contributo ao forum.

Cumps

Share this post


Link to post
Share on other sites
Dabubble

Muito a frente!! Ja tinha utilizado reflection em Java e da mesmo muito jeito (principalmente em middleware). Ja agora sabes se esses componentes podem ser componenentes web?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.