As for the form, the user enters the data, but if they need to add additional items (i.e. more people, property vehicles), I need the data to save to an excel file, but clear the form. I have been able to get the textboxes and rich textboxes to clear, but I am unsure how to have combo boxes clear out (go blank). Here is a snippet of what I've written for the text and rich textboxes. Intellisense won't let me select selectindex and I'm currently stuck.

For Each Me.cControl In Person.Controls
If (TypeOf cControl Is TextBox) Then
cControl.Text = ""
End If
If (TypeOf cControl Is RichTextBox) Then
cControl.Text = ""
End If
Next cControl

Dani AI

Generated

had the right idea looping controls; the reason Intellisense didn’t show combo-only members is the loop variable is a generic Control. ’s follow-up (unselecting rather than nuking the list) is usually what you want — Items.Clear() removes the combo’s choices. The safe approach is to cast each control to a ComboBox and clear only the selection/text, not the Items collection.

A reusable routine that handles nested containers and only touches ComboBoxes (without removing their Items) looks like this:

Private Sub ClearInputs(parent As Control)
    For Each ctl As Control In parent.Controls
        Dim cb = TryCast(ctl, ComboBox)
        If cb IsNot Nothing Then
            cb.SelectedItem = Nothing
            cb.ResetText()
        ElseIf ctl.HasChildren Then
            ClearInputs(ctl)
        End If
    Next
End Sub

If you prefer a simple non-recursive loop and know all combos are in one container, you can iterate only ComboBox objects:

For Each cb As ComboBox In Person.Controls.OfType(Of ComboBox)()
    cb.SelectedItem = Nothing
    cb.ResetText()
Next

Notes and troubleshooting

  • Use SelectedItem = Nothing / resetting the text to clear the displayed selection while keeping the Items intact. That matches ’s corrected advice (unselect instead of clearing items).
  • Avoid Items.Clear() unless you truly want to drop every entry added at runtime (that will remove design-time items too).
  • For data-bound ComboBoxes, clear the bound value or set the control’s selected value/item appropriately instead of clearing Items; behaviour can differ when a DataSource is used.
  • See Microsoft docs for more on ComboBox selection and ResetText: ComboBox.SelectedItem and Control.ResetText.

Recommended Answers

All 3 Replies

Try cmbMyCombo.Items.Clear()

Try this :

Private Sub ClearCombo(ByVal MyCombo As ComboBox)
    MyCombo.Items.Clear()
End Sub

For Each ccontrol In Me.Controls
    If TypeOf ccontrol Is ComboBox Then
        ccontrol.Text = ""
        ClearCombo(ccontrol)
    End If
Next ccontrol

Sorry. Misunderstood. You could set

cmbMyCombo.SelectedIndex = -1

and that will unselect all

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.