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

Referencing controls on form

Status
Not open for further replies.

jbehrne

Programmer
Dec 18, 2002
484
US
Hi all,

I am trying to make a function that clears all values in text boxes on a form (a basic 'undo' option). Several textboxes and comboboxes are on different tabs. However, when I call the function and run the function nothing on the form changes... is there something wrong with the code below? Do I need to reference the controls on the tabs differently?

Code:
Private Function UndoForm() As Boolean

UndoForm = True

Dim ctl As Control

Try
            For Each ctl In Me.Controls
                If (TypeOf ctl Is TextBox) Or (TypeOf ctl Is ComboBox) Then
                    ctl.Text = ""
                End If
            Next
        Catch ex As Exception
            UndoForm = False
            Exit Function
        End Try


        MsgBox("Finished.")
End Function


Thanks,

jbehrne

If at first you don't succeed, call in an airstrike. - Murphy's Laws of Combat Operations
 
You need to call the method recursively.

Code:
Private Function ClearControls(ctl as Control) as control
  dim c as control
  For Each c In ctl
    If (TypeOf c Is TextBox) Or (TypeOf c Is ComboBox) Then
      ctl.Text = ""
    Else
      ClearControls(c)
    End If
  Next
end function

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
Thanks for the reply Rick,

However when I used the code you posted the 'ctl' in the line:
Code:
For each c in ctl

is underlined in blue and returns the following message:

"'Expression is of type 'System.Windows.Forms.Control', which is not a collection type"

I'm afraid that I may be getting in a little over my head here...

I am calling the function you posted by the following code:

Code:
Dim ctl As Control

For Each ctl In Me.Controls
    ClearControls(ctl)
Next

I'm using Vb.Net 2005 (don't know if this makes a difference or not). Thanks for your help,

jbehrne

If at first you don't succeed, call in an airstrike. - Murphy's Laws of Combat Operations
 
whoops. That's what I get for posting uncompiled code ;)

try this:
Code:
Private Function ClearControls(ctl as Control) as control
  dim c as control
  For Each c In ctl.controls
    If (TypeOf c Is TextBox) Or (TypeOf c Is ComboBox) Then
      ctl.Text = ""
    Else
      ClearControls(c)
    End If
  Next
end function

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
Rick,

I just tried the code and the results were totally unexpected. The code cleares the text off of the tabs. But not any text or comboboxes.

jbehrne

If at first you don't succeed, call in an airstrike. - Murphy's Laws of Combat Operations
 
Got it:

Code:
If ctl.HasChildren Then
            For Each c As Control In ctl.Controls
                RecurseControls(c)
                If (TypeOf c Is TextBox) Or (TypeOf c Is ComboBox) Then
                    c.Text = Nothing
                ElseIf (TypeOf c Is CheckBox) Then
                    CType(c, CheckBox).Checked = False
                ElseIf (TypeOf c Is DateTimePicker) Then
                    c.Text = Nothing
                End If
            Next c
        End If

If at first you don't succeed, call in an airstrike. - Murphy's Laws of Combat Operations
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top