Note that there are some explanatory texts on larger screens.

plurals
  1. POVB.NET - Iterating through controls in a container object
    primarykey
    data
    text
    <p>I have a form with a "Clear" button.</p> <p>When the user clicks "Clear", I want to clear the value of all the visible elements on the form. In the case of date controls, I want to reset them to the current date.</p> <p>All of my controls are contained on a Panel.</p> <p>Right now, I'm doing this with the below code. Is there an easier way than manually checking for each control type? This method seems excessively unwieldy.</p> <p>To make matters worse, in order to recursively clear controls inside sub-containers (i.e., a group box within the panel) I have to repeat the whole monster with an overloaded "GroupBox" version.</p> <p><em>Edit: Thanks to your suggestions, the below code is greatly simplified.</em></p> <pre><code>Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click 'User clicks Clear, so clear all the controls within this panel ClearAllControls(panMid, True) 'True indicates that yes, i want to recurse through sub-containers End Sub ClearAllControls(ByRef container As Panel, Optional Recurse As Boolean = True) 'Clear all of the controls within the container object 'If "Recurse" is true, then also clear controls within any sub-containers Dim ctrl As Control For Each ctrl In container.Controls If (ctrl.GetType() Is GetType(TextBox)) Then Dim txt As TextBox = CType(ctrl, TextBox) txt.Text = "" End If If (ctrl.GetType() Is GetType(CheckBox)) Then Dim chkbx As CheckBox = CType(ctrl, CheckBox) chkbx.Checked = False End If If (ctrl.GetType() Is GetType(ComboBox)) Then Dim cbobx As ComboBox = CType(ctrl, ComboBox) cbobx.SelectedIndex = -1 End If If (ctrl.GetType() Is GetType(DateTimePicker)) Then Dim dtp As DateTimePicker = CType(ctrl, DateTimePicker) dtp.Value = Now() End If If Recurse Then If (ctrl.GetType() Is GetType(Panel)) Then Dim pnl As Panel = CType(ctrl, Panel) ClearAllControls(pnl, Recurse) End If If ctrl.GetType() Is GetType(GroupBox) Then Dim grbx As GroupBox = CType(ctrl, GroupBox) ClearAllControls(grbx, Recurse) End If End If Next End Sub </code></pre> <p>@Theraccoonbear: I like your suggestion, but when I change the declaration to this:</p> <pre><code>Private Sub ClearAllControls(ByRef controls As ControlCollection, Optional ByVal Recurse As Boolean = True) </code></pre> <p>Then this line gives me "Unable to cast object of type 'ControlCollection' to type 'ControlCollection'.":</p> <pre><code> ClearAllControls(panMid.Controls) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload