I have a form with a \"Clear\" button.
When the user clicks \"Clear\", I want to clear the value of all the visible elements on the form. In the case of date contro
Here it works for all inner controls.
Add if any other controls do you need to clear.
Private Sub ClearAll()
Try
For Each ctrl As Control In Me.Controls
If ctrl.[GetType]().Name = "Panel" Then
ClearControls(ctrl)
End If
If ctrl.[GetType]().Name = "GroupBox" Then
ClearControls(ctrl)
End If
If ctrl.[GetType]().Name = "ComboBox" Then
Dim tb As ComboBox = TryCast(ctrl, ComboBox)
tb.SelectedText = ""
End If
If ctrl.[GetType]().Name = "TabControl" Then
ClearControls(ctrl)
End If
If ctrl.[GetType]().Name = "TextBox" Then
Dim tb As TextBox = TryCast(ctrl, TextBox)
tb.Clear()
End If
If ctrl.[GetType]().Name = "RadioButton" Then
Dim tb As RadioButton = TryCast(ctrl, RadioButton)
tb.Checked = False
End If
If ctrl.[GetType]().Name = "CheckBox" Then
Dim tb As CheckBox = TryCast(ctrl, CheckBox)
tb.Checked = False
End If
If ctrl.[GetType]().Name = "ComboBox" Then
Dim tb As ComboBox = TryCast(ctrl, ComboBox)
tb.SelectedIndex = 0
End If
If ctrl.[GetType]().Name = "RichTextBox" Then
Dim tb As RichTextBox = TryCast(ctrl, RichTextBox)
tb.Clear()
End If
Next
Catch ex As Exception
MessageBox.Show(ex.Message, "Error Message", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Private Sub ClearControls(ByVal Type As Control)
Try
For Each ctrl As Control In Type.Controls
If ctrl.[GetType]().Name = "TextBox" Then
Dim tb As TextBox = TryCast(ctrl, TextBox)
tb.Clear()
End If
If ctrl.[GetType]().Name = "Panel" Then
ClearControls(ctrl)
End If
If ctrl.[GetType]().Name = "GroupBox" Then
ClearControls(ctrl)
End If
If ctrl.[GetType]().Name = "TabPage" Then
ClearControls(ctrl)
End If
If ctrl.[GetType]().Name = "ComboBox" Then
Dim tb As ComboBox = TryCast(ctrl, ComboBox)
tb.SelectedText = ""
End If
If ctrl.[GetType]().Name = "RadioButton" Then
Dim tb As RadioButton = TryCast(ctrl, RadioButton)
tb.Checked = False
End If
If ctrl.[GetType]().Name = "CheckBox" Then
Dim tb As CheckBox = TryCast(ctrl, CheckBox)
tb.Checked = False
End If
If ctrl.[GetType]().Name = "RichTextBox" Then
Dim tb As RichTextBox = TryCast(ctrl, RichTextBox)
tb.Clear()
End If
Next
Catch ex As Exception
MessageBox.Show(ex.Message, "Error Message", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub