Visual Basic

How To Clear A Form In Visual Basic

To clear a form in Visual Basic .NET Windows Forms, reset each input control using its appropriate method or property: clear text boxes, remove selections and restore default values. Put these operations in a reusable procedure and call it from a Clear button. For larger.

Which licences fit your PC setup?

Prices are an AUD snapshot, not a live quote. Confirm variant and total on the listing.

Choose what clearing the form should do

A blank form and a form restored to its starting state are different outcomes. A name field might become empty, while a quantity returns to one and a delivery option returns to its default selection. Decide which values users should see before writing the reset procedure.

The examples below use VB.NET Windows Forms, rather than VBA UserForms or classic Visual Basic 6. Those environments have different control APIs. If you are still learning how controls and events fit together, the guide to whether Visual Basic is easy to learn provides useful background.

Windows Forms has no general Form.Clear() or Form.Reset() method that restores every input. Me.Controls.Clear() removes controls from their container. It does not empty their values, and using it for a Clear button can make the interface disappear. Microsoft's ControlCollection.Clear documentation describes that removal behaviour.

Reset each control using the right operation

For a small form, explicitly naming each input is straightforward and makes exceptions easy to see. Use the following operations for unbound controls, replacing the example names with those in your project:

  • TextBox: txtName.Clear() removes its text. Assigning txtName.Text = String.Empty also works.
  • ComboBox: cmbCategory.SelectedIndex = -1 removes the selected item. For an editable combo box, also set cmbCategory.Text = String.Empty to remove typed text.
  • ListBox: lstItems.ClearSelected() removes selections while keeping the available items.
  • CheckBox: chkSubscribe.Checked = False unticks the box. For a three-state checkbox, choose the intended CheckState explicitly.
  • RadioButton: set the intended default option's Checked property to True. If the form allows no selection, set each option in that group to False.
  • NumericUpDown: assign a valid default to Value, within its Minimum and Maximum limits.
  • DateTimePicker: assign the intended default date to Value, within its permitted date range.

Do not confuse clearing selections with deleting choices. Calling Items.Clear() on an unbound list or combo box removes the entries users can choose from. Likewise, a DateTimePicker's MinDate is the earliest permitted date, rather than an empty value. An optional date needs an explicit design, such as a checkbox that your application interprets as “no date”.

These operations change control values directly. Refresh() redraws a control; it does not reset input and is normally unnecessary after these assignments.

Add a Clear button and a reusable procedure

This example resets a simple unbound entry form containing txtName, cmbCategory, lstItems and chkSubscribe. It keeps the combo box and list box choices available.

  1. Add a Button to the form, name it btnClear and set its displayed Text to Clear.
  2. Add the ResetEntryForm procedure below inside the form class, outside any existing procedure.
  3. Double-click the button in the designer to create its Click handler, then place ResetEntryForm() inside that handler.
  4. Run the application, enter values, make selections and click Clear.

Private Sub ResetEntryForm()

txtName.Clear()

cmbCategory.SelectedIndex = -1

If cmbCategory.DropDownStyle <> ComboBoxStyle.DropDownList Then cmbCategory.Text = String.Empty

lstItems.ClearSelected()

chkSubscribe.Checked = False

txtName.Focus()

End Sub

The button handler should look like this. Update an existing handler rather than adding a duplicate with the same name.

Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click

ResetEntryForm()

End Sub

Calling the same procedure after a successful save keeps the next entry consistent. Wait until saving succeeds before clearing fields, so a failed submission leaves the user's work available for correction.

Include inputs inside panels, groups and tabs

A loop over Me.Controls visits the form's immediate children. Text boxes inside a Panel, GroupBox or TabPage belong to that container's Controls collection. A loop that ignores those collections will leave nested inputs unchanged.

For a larger form, use a helper procedure that accepts a parent Control. Within it, follow this sequence:

  1. Loop through parent.Controls using a Control variable.
  2. Check each input's type and apply its agreed reset operation.
  3. For containers, call the helper again with that container as the parent.
  4. After traversal finishes, restore any form-specific defaults and move focus to the first entry field.

Limit the helper to the controls you intend to reset. A blanket assignment to every control's Text property can erase labels and button captions. An explicit procedure is often easier to maintain when fields have different defaults or some values must remain unchanged.

Handle bound data and validation separately

Changing a bound text box can update its underlying record, depending on the binding settings. Before clearing a bound form, distinguish between cancelling edits to the current record and preparing a new record. Neither action should require deleting saved database rows.

For cancellation, BindingSource.CancelEdit() can discard pending row edits when the data source supports IEditableObject and EndEdit() has not already been called. It is not a general undo operation for saved changes; see Microsoft's CancelEdit documentation. For new entry, create or select a new model object through the application's normal binding workflow.

Avoid DataTable.Clear() as a way to blank fields: it removes rows from the in-memory table. Reset only form-specific variables or temporary collections that belong to the entry being discarded.

Resetting values can trigger TextChanged, CheckedChanged and selection events. If those handlers perform validation or calculations, use a form-level Boolean flag to identify a reset in progress. Restore that flag in a Finally block so an error cannot leave normal processing disabled.

If validation prevents users from reaching Clear, consider setting btnClear.CausesValidation = False when discarding input is allowed. Clear obsolete ErrorProvider messages as part of the reset, and verify that normal validation still runs when users submit.

Check the reset against realistic input

Test empty fields, typed combo box text, multiple list selections and inputs on every tab. Check that labels, available choices and saved records remain intact. If clearing discards substantial unsaved work, provide confirmation before resetting.

For related learning, browse the Visual Basic guides or read whether Visual Basic is worth learning when planning further work on an existing application.

Start by adding ResetEntryForm to one form, then verify every field's expected value after clicking Clear.

Other editions for separate workloads

Other editions for separate workloads; prices in AUD, confirm the total on the listing.

How to check this worked

  1. Open Settings, System, Activation and confirm the edition shown.
  2. Enter the key and run activation; expect a green tick within a minute.
  3. Reboot and re-check activation survives the restart.

FAQ

What else should you know before buying?

What do I need before starting?
Read the requirements and confirm the edition first; the steps assume the machine is compatible.
How do I know the licence is genuine?
Activate on Microsoft's servers and check Settings, System, Activation. A working activation is the test.
Is this a subscription?
Keys sold here are one-time lifetime licences. Microsoft 365 plans are billed by Microsoft instead.
What if the key fails?
Email hi@ms.codes with the order number and a screenshot. Replacement or refund applies within 30 days.

Check who prepared and reviewed this guide

Written by
MS.Codes editorial
Reviewed by
Support team

Last verified: (source review; see test status above).

Recent Post