Check for empty TextBox controls in VB.NET

前端 未结 7 1847
离开以前
离开以前 2020-12-06 13:01

Ive got a Form application in VB.NET.

I have many text boxes on one form (about 20). Is there anyway to check them all at once to see if they are empty instead of wr

7条回答
  •  攒了一身酷
    2020-12-06 13:21

    You could also use LINQ:

    Dim empty =
        Me.Controls.OfType(Of TextBox)().Where(Function(txt) txt.Text.Length = 0)
    If empty.Any Then
        MessageBox.Show(String.Format("Please fill following textboxes: {0}",
                        String.Join(",", empty.Select(Function(txt) txt.Name))))
    End If
    

    The interesting method is Enumerable.OfType

    The same in query syntax(more readable in VB.NET):

    Dim emptyTextBoxes =
        From txt In Me.Controls.OfType(Of TextBox)()
        Where txt.Text.Length = 0
        Select txt.Name
    If emptyTextBoxes.Any Then
        MessageBox.Show(String.Format("Please fill following textboxes: {0}",
                        String.Join(",", emptyTextBoxes)))
    End If
    

提交回复
热议问题