Membersihkan Seluruh Control/Object pada Form dengan VB.NET dan C#

Setelah beberapa hari yang lalu saya memposting tentang Membuat Placeholder pada Textbox dengan VB.NET dan C#
pada postingan kali ini saya akan membahas tentang Membersihkan Seluruh Control pada Form dengan VB.NET dan C#.
Terkadang ketika membuat sebuah form dimana form tersebut memiliki banyak control seperti textbox, listview, listbox atau yang lainnya, lalu anda ingin membersihkan isi dari control tersebut misalnya saja pada textbox dengan script textbox1.Text = "", mungkin jika hanya beberapa textbox akan mudah namum bagaiman jika kita memiliki banyak textbox dan juga control - control yang lainnya? kita pasti akan menggunakan banyak code untuk membersihkan isi dari control tersebut, saya juga terkadang merasa lelah jika melakukan itu terus - menerus. Tapi disini saya akan share source code hasil cari - cari google untuk Membersihkan Seluruh Control/Object pada Form dengan VB.NET dan C# dengan mudah dan cepat

1. Buka visual studio yang anda punya

2. Buatlah sebuah fungsi seperti dibawah ini

VBNET
Private Sub Clear(frm As Form)
For Each obj As Object In frm.Controls
If TypeOf obj Is TextBox Or TypeOf obj Is MaskedTextBox Or TypeOf obj Is RichTextBox Or TypeOf obj Is ComboBox Then 'jika tipe obj adalah textbox atau maskedtextbox atau richtextbox atau combobox
obj.Text = ""
ElseIf TypeOf obj Is ListView Or TypeOf obj Is ListBox Or TypeOf obj Is ComboBox Then ' jika tipe obj adalah listview atau listbox atau combobox
obj.Items.Clear()
ElseIf TypeOf obj Is DataGridView Then ' jika tipe obj adalah datagridview
obj.Rows.Clear()
ElseIf TypeOf obj Is PictureBox Then ' jika tipe obj adalah picturebox
obj.Image = Nothing
ElseIf TypeOf obj Is CheckBox Or TypeOf obj Is RadioButton Then ' jika tipe obj adalah checkebox atau radiobutton
obj.Checked = False
ElseIf TypeOf obj Is NumericUpDown Then ' jika tipe obj adalah numericupdown
obj.Value = 0
End If
Next
End Sub
C#
private void Clear(Form frm)
{
foreach (object obj in frm.Controls)
{
if (obj.GetType() == typeof(TextBox)) // jika tipe obj adalah textbox
{
TextBox txtbox = (TextBox)obj;
txtbox.Text = "";
}
else if (obj.GetType() == typeof(MaskedTextBox)) // jika tipe obj adalah maskedtextbox
{
MaskedTextBox msktextbox = (MaskedTextBox)obj;
msktextbox.Text = "";
}
else if (obj.GetType() == typeof(NumericUpDown)) // jika tipe obj adalah numericupdown
{
NumericUpDown numeric = (NumericUpDown)obj;
numeric.Value = 0;
}
else if (obj.GetType() == typeof(CheckBox)) // jika tipe obj adalah checkbox
{
CheckBox chkbox = (CheckBox)obj;
chkbox.Checked = false;
}
else if (obj.GetType() == typeof(RadioButton)) // jika tipe obj adalah radiobutton
{
RadioButton rbutton = (RadioButton)obj;
rbutton.Checked = false;
}
else if (obj.GetType() == typeof(ComboBox)) // jika tipe obj adalah combobox
{
ComboBox cbox = (ComboBox)obj;
cbox.Items.Clear();
cbox.Text = "";
}
else if (obj.GetType() == typeof(PictureBox)) // jika tipe obj adalah picturebox
{
PictureBox pbox = (PictureBox)obj;
pbox.Image = null;
}
else if (obj.GetType() == typeof(RichTextBox)) // jika tipe obj adalah richtextbox
{
RichTextBox rtbox = (RichTextBox)obj;
rtbox.Text = "";
}
else if (obj.GetType() == typeof(ListBox)) // jika tipe obj adalah listbox
{
ListBox lstbox = (ListBox)obj;
lstbox.Items.Clear();
}
else if (obj.GetType() == typeof(ListView)) // jika tipe obj ada listview
{
ListView lv = (ListView)obj;
lv.Items.Clear();
}
else if (obj.GetType() == typeof(DataGridView)) // jika tipe obj adalah datagridview
{
DataGridView dgv = (DataGridView)obj;
dgv.Rows.Clear();
}
}
}
Anda juga bisa menyesuaikan control apa saja yang anda butuhkan dengan menambahkan IF baru atau yang lainnya

3. Untuk cara penggunaannya seperti dibawah ini

VB.NET
Clear(Me)
C#
Clear(this)

Berikut beberapa screenshotnya

Membersihkan Seluruh Control/Object pada Form dengan VB.NET dan C#

Membersihkan Seluruh Control/Object pada Form dengan VB.NET dan C#

Anda dapat mendownload source codenya dibawah ini
| VB.NET | C# |
Untuk password silahkan klik disini

Sekian dari saya.
Terima kasih atas kunjugannya semoga bermanfaat

Tidak ada komentar

Diberdayakan oleh Blogger.