Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 67161
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T19:12:59+00:00 2026-05-10T19:12:59+00:00

I have a form with a Clear button. When the user clicks Clear, I

  • 0

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 controls, I want to reset them to the current date.

All of my controls are contained on a Panel.

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.

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.

Edit: Thanks to your suggestions, the below code is greatly simplified.

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 

@Theraccoonbear: I like your suggestion, but when I change the declaration to this:

Private Sub ClearAllControls(ByRef controls As ControlCollection, Optional ByVal Recurse As Boolean = True) 

Then this line gives me ‘Unable to cast object of type ‘ControlCollection’ to type ‘ControlCollection’.’:

  ClearAllControls(panMid.Controls) 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T19:13:00+00:00Added an answer on May 10, 2026 at 7:13 pm

    You can skip the GetType and CType dance with TryCast:

    Dim dtp as DateTimePicker = TryCast(ctrl, DateTimePicker) If dtp IsNot Nothing then dtp.Value = Now() 

    That’ll save you about 10 lines.

    An extension method off the Control class should keep it pretty tidy:

    <Extension()> _ Public Shared Sub ClearValue(c as Control, recursive as Boolean)    Dim dtp as DateTimePicker = TryCast(c, DateTimePicker)    If dtp IsNot Nothing Then dtp.Value = Now()    ' Blah, Blah, Blah End Sub 

    Edit: If the thought of Evil extension methods that ignore NullReferenceExceptions don’t make you cringe:

    <Extension()> _ Public Shared Sub ClearValue(c as CheckBox)    If c IsNot Nothing Then c.Checked = False End Sub  TryCast(ctrl, CheckBox).ClearValue() 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 65k
  • Answers 65k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer For a menu system with choices 0..9 this is reasonably… May 11, 2026 at 11:08 am
  • added an answer You need a join table: tbl_location ID: int Name: Varchar(20)… May 11, 2026 at 11:08 am
  • added an answer The best solution I have found is to make use… May 11, 2026 at 11:08 am

Related Questions

I have a form with a Clear button. When the user clicks Clear, I
I have a form with a progress bar and a cancel button which is
I have a form with a textarea. Users enter a block of text which
I have a form with a textbox and a button. IE is the only
I have a form with a lot of controls on it. How can I
I have a form with a <textarea> and I want to capture any line
I have a form with a few buttons which execute code when pressed like
HI I have a form with a few buttons. Each button runs a few
I have a Drupal module page where I am populating a form with a
I have a rails form with a datetime_select field. When I try to submit

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.