Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Clearing textboxes 1

Status
Not open for further replies.

brews

Technical User
Dec 12, 2007
194
0
0
US
Would like to iterate through a bunch of text boxes and clear them. So far this
Code:
    Private Sub NewRecord()
        For Each ctrl As Control In GroupBox1.Controls
            If TypeOf ctrl Is TextBox Then
                CType(ctrl, TextBox).Text = ""
            End If
        Next
    End Sub
passes over the Ctype expression.
 
This may not be the best way to get there, but has worked well for us so far:
Code:
	Public Sub ClearForm(ByRef frm As Control)
		Try
			For Each c As Control In frm.Controls
				If c.HasChildren Then
					ClearForm(c)
				Else
					Select Case frm.GetType.Name
						Case "DataGridView"
							CType(frm, DataGridView).DataSource = Nothing
							CType(frm, DataGridView).Rows.Clear()
						Case Else
							Select Case c.GetType.Name
								Case "TextBox", "RichtextBox", "MaskedTextBox"
									c.Text = String.Empty
								Case "DateTimePicker"
									CType(c, DateTimePicker).Value = DateAdd(DateInterval.Day, -1, Now())
								Case "NumericUpDown"
									CType(c, NumericUpDown).Value = 0
								Case "RadioButton"
									CType(c, RadioButton).Checked = False
								Case "CheckBox"
									CType(c, CheckBox).Checked = False
								Case "ComboBox"
									'CType(c, ComboBox).Text = String.Empty
									CType(c, ComboBox).SelectedValue = 0
								Case "DataGridView"
									CType(c, DataGridView).DataSource = Nothing
									CType(c, DataGridView).Rows.Clear()
									'Case "Label", "Button"
									'	'do nothing
									'Case Else
									'	Application.DoEvents()
							End Select
					End Select
				End If
			Next
		Catch Ex As Exception
			Ex.Source = System.Reflection.MethodBase.GetCurrentMethod.Name
			ErrorLog(Err.Number, Err.Description, "ModGlobal", Ex.Source, Err.Erl)
		End Try
	End Sub
Simply pass the form or container (in your case a GroupBox) as the parameter for the procedure.

--------------------------------------------------
“Crash programs fail because they are based on the theory that, with nine women pregnant, you can get a baby a month.” --Wernher von Braun
--------------------------------------------------
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top