Dim ctrl As Control
For Each ctrl In Me.Controls
If TypeOf ctrl Is TextBox Then
DirectCast(ctrl, TextBox).Clear()
End If
Next
if (c is TextBox)
((TextBox)c).Clear();
Private Sub GetControlHashCodes()
obtain the name and a unique hashcode of each control for reference purposes when updating the
form with job data
Dim pnlPanel, tbcTabControl, tbpTabPage, ctlControl As Object
Dim intX As Integer
Try
cycle through panel controls, only one in this instance
For Each pnlPanel In Me.Controls
cycle through tabcontrol controls, only one in this instance
For Each tbcTabControl In pnlPanel.Controls
cycle through tabpages of tabcontrol
For Each tbpTabPage In tbcTabControl.Controls
cycle through controls on each tabpage
For Each ctlControl In tbpTabPage.Controls
store the controls name and hashcode in table, filter required controls only
Select Case Microsoft.VisualBasic.Left(ctlControl.name, 3)
Case "txt", "rtb", "cbo", "dtm", "tdm", "chk", "pic"
m_htControlsHashTable.Add(ctlControl.Name, ctlControl)
End Select
Next
Next
Next
Next
Catch objException As Exception
ShowError("Location: Class frmJob" & ControlChars.CrLf & ControlChars.CrLf & _
"Procedure: GetControlHashCodes()" & ControlChars.CrLf & _
ControlChars.CrLf & "Error Text: " & objException.Message)
End Try
End Sub
Private Sub ClearTabPages()
Dim intX As Integer, intPageIndex As Integer = 11
Try
cycle through the hashtable and clear/initialise referenced controls
For intX = 0 To intPageIndex
DirectCast(m_htControlsHashTable("txtDueDate" & intX + 1), TextBox).Text = String.Empty
DirectCast(m_htControlsHashTable("txtSupplier" & intX + 1), TextBox).Text = String.Empty
DirectCast(m_htControlsHashTable("tdmJobTime" & intX + 1), DateTimePicker).Text = "00:00"
DirectCast(m_htControlsHashTable("txtJobRef" & intX + 1), TextBox).Text = String.Empty
DirectCast(m_htControlsHashTable("txtInvoiceNo" & intX + 1), TextBox).Text = String.Empty
DirectCast(m_htControlsHashTable("txtInvoiceAmount" & intX + 1), TextBox).Text = 0
DirectCast(m_htControlsHashTable("txtEngineer" & intX + 1), TextBox).Text = String.Empty
DirectCast(m_htControlsHashTable("txtGRN" & intX + 1), TextBox).Text = String.Empty
DirectCast(m_htControlsHashTable("rtbComments" & intX + 1), RichTextBox).Text = String.Empty
DirectCast(m_htControlsHashTable("dtmJobDate" & intX + 1), DateTimePicker).Text = Today
DirectCast(m_htControlsHashTable("dtmInvoiceDate" & intX + 1), DateTimePicker).Text = Today
DirectCast(m_htControlsHashTable("cboType" & intX + 1), ComboBox).Text = String.Empty
DirectCast(m_htControlsHashTable("picCompleted" & intX + 1), PictureBox).Visible = False
Next
DirectCast(m_htControlsHashTable("txtTotalJobs"), TextBox).Text = String.Empty
DirectCast(m_htControlsHashTable("txtTotalJobsCompleted"), TextBox).Text = String.Empty
DirectCast(m_htControlsHashTable("txtPOCost"), TextBox).Text = String.Empty
DirectCast(m_htControlsHashTable("txtInvoicedToDate"), TextBox).Text = String.Empty
DirectCast(m_htControlsHashTable("txtOverCharged"), TextBox).Text = String.Empty
DirectCast(m_htControlsHashTable("txtTotalHours"), TextBox).Text = String.Empty
Catch objException As Exception
ShowError("Location: Class frmJob" & ControlChars.CrLf & ControlChars.CrLf & _
"Procedure: ClearTabPages()" & ControlChars.CrLf & _
ControlChars.CrLf & "Error Text: " & objException.Message)
End Try
End Sub
But in order to loop through all controls on a form you have to recurse on the Controls collection of the Form, then the Controls collection of every control you find from then on.