David
David

Reputation: 163

Checking if text boxes are empty

I have a winform with a bound datagridview, 4 text boxes and a button. With the click of the button the "fillby" statement is called, the data is loaded on the datagridview and the content of the text boxes become the default value of some fields in the new rows of the datagridview.

I need to check all the text boxes to make sure they're not empty, if any of them is empty then a message should pop up saying which text box is empty and also keeps the datagridview from filling.

This is the code I have so far:

 Private Sub btnCargarInformacion_Click(sender As System.Object, e As System.EventArgs) Handles btnCargar.Click
    Dim emptyTextBoxes =
    From txt In Me.Controls.OfType(Of TextBox)()
    Where txt.Text.Length = 0
    Select txt.Name

    If emptyTextBoxes.Count >= -1 Then
        MessageBox.Show(String.Format("Please fill following textboxes: {0}", String.Join(",", emptyTextBoxes)))
    Else
        Dim PartePersonalTableApt As New PersonalObraDataSetTableAdapters.PartePersonalTableAdapter
        Dim PersonalObTableApt As New PersonalObraDataSetTableAdapters.PersonalObTableAdapter
        PartePersonalTableApt.ClearBeforeFill = True
        PartePersonalTableApt.FillByFecha(PersonalObraDataSet.PartePersonal, txtDate.Text, txtDepartamento.Text, txtTurno.Text)
        PersonalObTableApt.ClearBeforeFill = True
        PersonalObTableApt.Fillby(PersonalObraDataSet.PersonalOb)
    End If
End Sub

I don't get any errors, the message appears even if all textboxes are filled, the message box does not specifying any text boxes as empty and stops the datagridview from filling.

I'm very new to codding so please explain in more detail your solution. Thanks

Upvotes: 1

Views: 5518

Answers (2)

Malcolm Salvador
Malcolm Salvador

Reputation: 1566

You can try to loop at your container (ie form, panel, groupbox) for each textbox without content, then increment whenever there is a textbox that doesn't have content.

Example:

          Dim checkr as integer = 0
          Dim this As Control
          For Each this In that.Controls
           If TypeOf this Is TextBox Then
               If this.text = "" then
                 Checkr += 1
               End if
             End If
           Next

           If checkr > 0 then
              msgbox("Cannot proceed because a textbox has no content")
           Else
              '......(what you were gonna do)
           End If

Upvotes: 0

Louis Ricci
Louis Ricci

Reputation: 21106

.Count >= -1

The count will be 0 if it's empty

If emptyTextBoxes.Count <> 0 Then
    MessageBox.Show(String.Format("Please fill following textboxes: {0}", String.Join(",", emptyTextBoxes)))
Else
    Dim PartePersonalTableApt As New PersonalObraDataSetTableAdapters.PartePersonalTableAdapter
    Dim PersonalObTableApt As New PersonalObraDataSetTableAdapters.PersonalObTableAdapter
    PartePersonalTableApt.ClearBeforeFill = True
    PartePersonalTableApt.FillByFecha(PersonalObraDataSet.PartePersonal, txtDate.Text, txtDepartamento.Text, txtTurno.Text)
    PersonalObTableApt.ClearBeforeFill = True
    PersonalObTableApt.Fillby(PersonalObraDataSet.PersonalOb)
End If

Upvotes: 2

Related Questions