Lompat ke konten Lompat ke sidebar Lompat ke footer

Clear All Object In Form

Biasanya pada sebuah aplikasi atau program terdapat tombol standard seperti New,Edit,Save,Cancel,Close,Print, Search dan lain2. Nah, yang akan saya bahas kali ini adalah tombol Cancel, dimana pada umumnya berfungsi untuk membersihkan kembali semua object di form. Sebenarnya ada cara clasic yang sering digunakan untuk membersihkan object, seperti ini :
TextBox1.Text="" : ComboBox1.Text="" : ListView1.Items.Clear dan seterusnya, nah yang jadi sedikit permasalahan adalah bagaimana jika object di formnya banyak, otomatis sintak yang di tulisnya pun semakin banyak, belum lagi jika ada perubahan nama object, maka kita harus megkoreksi pula sintaknya, maka ini akan sedikit menjengkelkan dan menyusahkan. Nah saya akan share bagaimana membersihkan object di form secara otomatis. Saya sudah buatkan function nya.

ini untuk VB.NET :

Private Sub ClearAllObject()
        Dim iObject As Object
        For i As Integer = 0 To Me.Controls.Count - 1
            iObject = Me.Controls(i)
            If TypeOf iObject Is TextBox Then
                iObject.text = ""
            ElseIf TypeOf iObject Is ComboBox Or TypeOf iObject Is ListBox Then
              iObject.Items.Clear()
            ElseIf TypeOf iObject Is ListView Then
                iObject.Items.Clear()
            ElseIf TypeOf iObject Is TreeView Then
                iObject.Nodes.Clear()
            ElseIf TypeOf iObject Is DataGridView Then
                iObject.Rows.Clear()
            ElseIf TypeOf iObject Is PictureBox Then
                iObject.Image = Nothing
            ElseIf TypeOf iObject Is CheckBox Or TypeOf iObject Is RadioButton Then
                CheckBox1.Checked = False
            End If
        Next
    End Sub

Ada satu lagi caranya, berdasarkan pengalaman saya yang ini lebih CEPAT execute nya :

Private Sub ClearAllObject()
        For Each iObject As Object In Me.Controls
            If TypeOf iObject Is TextBox Then
                iObject.text = ""
            ElseIf TypeOf iObject Is ComboBox Or TypeOf iObject Is ListBox Then
                iObject.Items.Clear()
            ElseIf TypeOf iObject Is ListView Then
                iObject.Items.Clear()
            ElseIf TypeOf iObject Is TreeView Then
                iObject.Nodes.Clear()
            ElseIf TypeOf iObject Is DataGridView Then
                iObject.Rows.Clear()
            ElseIf TypeOf iObject Is PictureBox Then
                iObject.Image = Nothing
            ElseIf TypeOf iObject Is CheckBox Or TypeOf iObject Is RadioButton Then
                CheckBox1.Checked = False
            End If
        Next
    End Sub

Ini Untuk VB.6 :

Private Sub ClearAllObject()
    Dim i As Integer
    Dim iObject As Object
    For i = 0 To Me.Controls.Count - 1
        Set iObject = Me.Controls(i)
             If TypeOf iObject Is TextBox Then
                iObject.Text = ""
            ElseIf TypeOf iObject Is ComboBox Or TypeOf iObject Is ListBox Then
                iObject.Clear
                iObject.Clear
            ElseIf TypeOf iObject Is ListView Then
                iObject.ListItems.Clear
            ElseIf TypeOf iObject Is TreeView Then
               iObject.Nodes.Clear
            ElseIf TypeOf iObject Is DataGrid Then
                Set iObject.DataSource = Nothing
            ElseIf TypeOf iObject Is PictureBox Then
               Set iObject.Image = Nothing
            ElseIf TypeOf iObject Is OptionButton Then
                iObject.Value = False
            ElseIf TypeOf iObject Is CheckBox Then
                iObject.Value = Unchecked
            End If

    Next
End Sub

Atau gunakan yang ini utuk execute lebih cepat :
Private Sub ClearAllObject()
      Dim iObject As Object
     For Each iObject In Me.Controls
             If TypeOf iObject Is TextBox Then
                iObject.Text = ""
            ElseIf TypeOf iObject Is ComboBox Or TypeOf iObject Is ListBox Then
                iObject.Clear
                iObject.Clear
            ElseIf TypeOf iObject Is ListView Then
                iObject.ListItems.Clear
            ElseIf TypeOf iObject Is TreeView Then
               iObject.Nodes.Clear
            ElseIf TypeOf iObject Is DataGrid Then
                Set iObject.DataSource = Nothing
            ElseIf TypeOf iObject Is PictureBox Then
               Set iObject.Image = Nothing
            ElseIf TypeOf iObject Is OptionButton Then
                iObject.Value = False
            ElseIf TypeOf iObject Is CheckBox Then
                iObject.Value = Unchecked
            End If

    Next
End Sub

Untuk banyaknya object dalam "if typeof" sesuaikan saja dengan kebutuhan, ataupun jika ada object baru (user controls, dll) tinggal ditambahkan saja ke function itu. Sekian, semoga bermanfaat. Salam Programmer.

Tags:
clear all object from, auto clear object, fast clear object form, clear all object, clear form , vbnet , c#, visual studio, function, quick clear object form

Posting Komentar untuk "Clear All Object In Form"