Link to home
Start Free TrialLog in
Avatar of RIAS
RIASFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Empty textboxes on a form

Hello,

Is there any ways I can  code to get all empty textboxes on a windows form.

Cheers
ASKER CERTIFIED SOLUTION
Avatar of Jeff Darling
Jeff Darling
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi,

You can clear as follows.

1. Write a function as below.

void ClearAllControls(Control con)
{
    foreach (Control c in con.Controls)
    {
      if (c is TextBox)
         ((TextBox)c).Clear();
      else
         ClearAllControls(c);
    }
}

Open in new window


2. Call the function as below on button click

ClearAllControls(this);
Avatar of RIAS

ASKER

Thanks ,don't want to clear but ,want to find empty textboxes
Avatar of RIAS

ASKER

Cheers!
I should point out that while the proposed solution does enumerate textbox controls that are directly on the form, it does not enumerate textbox controls that are on child container controls that are on the form.  You must also check to see if the child control has children; e.g. -

Form1.vb -
Public Class Form1
	Private Sub OnLoad(sender As Object, e As EventArgs) Handles MyBase.Load
		For Each child In Controls.GetChildren(GetType(Label))
			child.Text = child.Parent.Name
		Next

		Label6.Text = String.Format("There is {0} Textbox Control on the Form", Controls.OfType(Of TextBox).Count)
		Label7.Text = String.Format("There are {0} Textbox Controls on the Form and in Containers", Controls.GetChildren(GetType(TextBox)).Count)
	End Sub
End Class

Module Extensions
	<System.Runtime.CompilerServices.Extension()> _
	Public Function GetChildren(source As Control.ControlCollection, Optional ByVal filter As Type = Nothing) As IEnumerable(Of Control)
		Dim children = New List(Of Control)
		For Each child As Control In source
			If child.GetType() Is If(filter IsNot Nothing, filter, GetType(Control)) Then children.Add(child)
			If child.HasChildren() Then children.AddRange(child.Controls.GetChildren(filter))
		Next
		Return children
	End Function
End Module

Open in new window

Form1.Designer.vb -
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Form1
    Inherits System.Windows.Forms.Form

    'Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        Try
            If disposing AndAlso components IsNot Nothing Then
                components.Dispose()
            End If
        Finally
            MyBase.Dispose(disposing)
        End Try
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
		Me.TextBox1 = New System.Windows.Forms.TextBox()
		Me.Label1 = New System.Windows.Forms.Label()
		Me.Panel1 = New System.Windows.Forms.Panel()
		Me.Panel2 = New System.Windows.Forms.Panel()
		Me.Panel3 = New System.Windows.Forms.Panel()
		Me.Panel4 = New System.Windows.Forms.Panel()
		Me.Label5 = New System.Windows.Forms.Label()
		Me.TextBox5 = New System.Windows.Forms.TextBox()
		Me.Label4 = New System.Windows.Forms.Label()
		Me.TextBox4 = New System.Windows.Forms.TextBox()
		Me.Label3 = New System.Windows.Forms.Label()
		Me.TextBox3 = New System.Windows.Forms.TextBox()
		Me.Label2 = New System.Windows.Forms.Label()
		Me.TextBox2 = New System.Windows.Forms.TextBox()
		Me.Label6 = New System.Windows.Forms.Label()
		Me.Label7 = New System.Windows.Forms.Label()
		Me.Panel1.SuspendLayout()
		Me.Panel2.SuspendLayout()
		Me.Panel3.SuspendLayout()
		Me.Panel4.SuspendLayout()
		Me.SuspendLayout()
		'
		'TextBox1
		'
		Me.TextBox1.Location = New System.Drawing.Point(301, 10)
		Me.TextBox1.Name = "TextBox1"
		Me.TextBox1.Size = New System.Drawing.Size(100, 20)
		Me.TextBox1.TabIndex = 0
		'
		'Label1
		'
		Me.Label1.AutoSize = True
		Me.Label1.Location = New System.Drawing.Point(13, 13)
		Me.Label1.Name = "Label1"
		Me.Label1.Size = New System.Drawing.Size(28, 13)
		Me.Label1.TabIndex = 1
		Me.Label1.Text = "Text"
		'
		'Panel1
		'
		Me.Panel1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
		Me.Panel1.Controls.Add(Me.Panel2)
		Me.Panel1.Controls.Add(Me.Label2)
		Me.Panel1.Controls.Add(Me.TextBox2)
		Me.Panel1.Location = New System.Drawing.Point(12, 36)
		Me.Panel1.Name = "Panel1"
		Me.Panel1.Size = New System.Drawing.Size(389, 133)
		Me.Panel1.TabIndex = 2
		'
		'Panel2
		'
		Me.Panel2.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
		Me.Panel2.Controls.Add(Me.Panel3)
		Me.Panel2.Controls.Add(Me.Label3)
		Me.Panel2.Controls.Add(Me.TextBox3)
		Me.Panel2.Location = New System.Drawing.Point(6, 29)
		Me.Panel2.Name = "Panel2"
		Me.Panel2.Size = New System.Drawing.Size(380, 98)
		Me.Panel2.TabIndex = 4
		'
		'Panel3
		'
		Me.Panel3.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
		Me.Panel3.Controls.Add(Me.Panel4)
		Me.Panel3.Controls.Add(Me.Label4)
		Me.Panel3.Controls.Add(Me.TextBox4)
		Me.Panel3.Location = New System.Drawing.Point(6, 29)
		Me.Panel3.Name = "Panel3"
		Me.Panel3.Size = New System.Drawing.Size(371, 63)
		Me.Panel3.TabIndex = 5
		'
		'Panel4
		'
		Me.Panel4.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
		Me.Panel4.Controls.Add(Me.Label5)
		Me.Panel4.Controls.Add(Me.TextBox5)
		Me.Panel4.Location = New System.Drawing.Point(6, 29)
		Me.Panel4.Name = "Panel4"
		Me.Panel4.Size = New System.Drawing.Size(362, 29)
		Me.Panel4.TabIndex = 6
		'
		'Label5
		'
		Me.Label5.AutoSize = True
		Me.Label5.Location = New System.Drawing.Point(3, 6)
		Me.Label5.Name = "Label5"
		Me.Label5.Size = New System.Drawing.Size(28, 13)
		Me.Label5.TabIndex = 3
		Me.Label5.Text = "Text"
		'
		'TextBox5
		'
		Me.TextBox5.Location = New System.Drawing.Point(259, 3)
		Me.TextBox5.Name = "TextBox5"
		Me.TextBox5.Size = New System.Drawing.Size(100, 20)
		Me.TextBox5.TabIndex = 2
		'
		'Label4
		'
		Me.Label4.AutoSize = True
		Me.Label4.Location = New System.Drawing.Point(3, 6)
		Me.Label4.Name = "Label4"
		Me.Label4.Size = New System.Drawing.Size(28, 13)
		Me.Label4.TabIndex = 3
		Me.Label4.Text = "Text"
		'
		'TextBox4
		'
		Me.TextBox4.Location = New System.Drawing.Point(268, 3)
		Me.TextBox4.Name = "TextBox4"
		Me.TextBox4.Size = New System.Drawing.Size(100, 20)
		Me.TextBox4.TabIndex = 2
		'
		'Label3
		'
		Me.Label3.AutoSize = True
		Me.Label3.Location = New System.Drawing.Point(3, 6)
		Me.Label3.Name = "Label3"
		Me.Label3.Size = New System.Drawing.Size(28, 13)
		Me.Label3.TabIndex = 3
		Me.Label3.Text = "Text"
		'
		'TextBox3
		'
		Me.TextBox3.Location = New System.Drawing.Point(277, 3)
		Me.TextBox3.Name = "TextBox3"
		Me.TextBox3.Size = New System.Drawing.Size(100, 20)
		Me.TextBox3.TabIndex = 2
		'
		'Label2
		'
		Me.Label2.AutoSize = True
		Me.Label2.Location = New System.Drawing.Point(3, 6)
		Me.Label2.Name = "Label2"
		Me.Label2.Size = New System.Drawing.Size(28, 13)
		Me.Label2.TabIndex = 3
		Me.Label2.Text = "Text"
		'
		'TextBox2
		'
		Me.TextBox2.Location = New System.Drawing.Point(286, 3)
		Me.TextBox2.Name = "TextBox2"
		Me.TextBox2.Size = New System.Drawing.Size(100, 20)
		Me.TextBox2.TabIndex = 2
		'
		'Label6
		'
		Me.Label6.AutoSize = True
		Me.Label6.Location = New System.Drawing.Point(9, 172)
		Me.Label6.Name = "Label6"
		Me.Label6.Size = New System.Drawing.Size(28, 13)
		Me.Label6.TabIndex = 3
		Me.Label6.Text = "Text"
		'
		'Label7
		'
		Me.Label7.AutoSize = True
		Me.Label7.Location = New System.Drawing.Point(9, 198)
		Me.Label7.Name = "Label7"
		Me.Label7.Size = New System.Drawing.Size(28, 13)
		Me.Label7.TabIndex = 4
		Me.Label7.Text = "Text"
		'
		'Form1
		'
		Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
		Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
		Me.ClientSize = New System.Drawing.Size(413, 318)
		Me.Controls.Add(Me.Label7)
		Me.Controls.Add(Me.Label6)
		Me.Controls.Add(Me.Panel1)
		Me.Controls.Add(Me.Label1)
		Me.Controls.Add(Me.TextBox1)
		Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle
		Me.Name = "Form1"
		Me.Text = "Form1"
		Me.Panel1.ResumeLayout(False)
		Me.Panel1.PerformLayout()
		Me.Panel2.ResumeLayout(False)
		Me.Panel2.PerformLayout()
		Me.Panel3.ResumeLayout(False)
		Me.Panel3.PerformLayout()
		Me.Panel4.ResumeLayout(False)
		Me.Panel4.PerformLayout()
		Me.ResumeLayout(False)
		Me.PerformLayout()

	End Sub
	Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
	Friend WithEvents Label1 As System.Windows.Forms.Label
	Friend WithEvents Panel1 As System.Windows.Forms.Panel
	Friend WithEvents Panel2 As System.Windows.Forms.Panel
	Friend WithEvents Panel3 As System.Windows.Forms.Panel
	Friend WithEvents Panel4 As System.Windows.Forms.Panel
	Friend WithEvents Label5 As System.Windows.Forms.Label
	Friend WithEvents TextBox5 As System.Windows.Forms.TextBox
	Friend WithEvents Label4 As System.Windows.Forms.Label
	Friend WithEvents TextBox4 As System.Windows.Forms.TextBox
	Friend WithEvents Label3 As System.Windows.Forms.Label
	Friend WithEvents TextBox3 As System.Windows.Forms.TextBox
	Friend WithEvents Label2 As System.Windows.Forms.Label
	Friend WithEvents TextBox2 As System.Windows.Forms.TextBox
	Friend WithEvents Label6 As System.Windows.Forms.Label
	Friend WithEvents Label7 As System.Windows.Forms.Label
End Class

Open in new window

Which produces the following output -User generated image-saige-
Avatar of RIAS

ASKER

Thanks Sir!