Solved

Empty textboxes on a form

Posted on 2016-08-24
6
39 Views
Last Modified: 2016-08-24
Hello,

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

Cheers
0
Comment
Question by:RIAS
6 Comments
 
LVL 12

Accepted Solution

by:
Jeff Darling earned 500 total points
ID: 41768692
something like this

For Each tb As TextBox In Me.Controls.OfType(Of TextBox)()
  if tb.Text = "" then
   ' do something
  end if
Next

Open in new window

0
 
LVL 14

Expert Comment

by:Lokesh B R
ID: 41768697
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);
0
 

Author Comment

by:RIAS
ID: 41768702
Thanks ,don't want to clear but ,want to find empty textboxes
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

Author Closing Comment

by:RIAS
ID: 41768703
Cheers!
0
 
LVL 33

Expert Comment

by:it_saige
ID: 41769030
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 -Capture.JPG-saige-
0
 

Author Comment

by:RIAS
ID: 41769724
Thanks Sir!
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question