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 Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

More Clear A Form Code 1

Status
Not open for further replies.

Christineeve

Programmer
Feb 12, 2005
104
US
I'm referencing this thread:

This thread and the answers has helped me finally clear all controls in a groupbox on my form.

However, I need help with clearing more than one groupbox on a form. To further complicate the issue:
I have a form with a tab control. Each tab control has multiple group boxes, with mulitple controls including radial/radio buttons, checkboxes, combo boxes and text boxes.

Main Form
-SuperTab Control
--Panels
---GroupBoxes
----Comboboxes, TextBox, Radial Button, etc.

I need to be able to do two things.

One,Clear all fields (which this code does but only one group box not all)

Two, detect if the contents were changed (which I plan on working on after I can clear them all).

I have adapted the owner's code as follows (not very much):


'Calling from the button click
Code:
    Private Sub cmdCheck_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCheck.Click
        'User clicks Clear, so clear all the controls within this panel
        Try
            clearForm(Panel2)
        Catch ex As Exception

        End Try
    End Sub

'Code clears all the controls within the groupbox. I have two group boxes and only one cleared.
Code:
 Private Sub clearForm(ByRef frm As Control)
        For Each c As Control In frm.Controls
            If c.HasChildren Then
                clearForm(c)
            Else
                Select Case c.GetType.Name
                    Case "TextBox", "RichtextBox"
                        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
                End Select
            End If
        Next

    End Sub

I'll continue working with this until I can figure out how to sort through each panel and clear them. Your help is appreciated. Thank you.
 
WOOT! This worked, this cleared all the controls in all the groupboxes/panels on my form. So simple I almost missed it.

I'll move the code over to my actual project to see if I can get it work.

Code:
Private Sub cmdCheck_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCheck.Click
        'User clicks Clear, so clear all the controls within this panel
        Try
            clearForm(Me)
        Catch ex As Exception

        End Try

I LOVE this site.
 
found a caveat to it. DataGridView IS a container, but the Container is what needs to be cleared. The following is an adaptation to support this and easily add other objects with this same structure:
Code:
	Public Sub ClearForm(ByRef frm As Control)
		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
						End Select
				End Select
			End If
		Next
	End Sub

--------------------------------------------------
“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
--------------------------------------------------
 
Thank you. I will be adding one of those to my program and this is helpful. A star for you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top