1

我有一个带有绑定 datagridview、4 个文本框和一个按钮的 winform。通过单击按钮调用“fillby”语句,数据加载到 datagridview 上,文本框的内容成为 datagridview 新行中某些字段的默认值。

我需要检查所有文本框以确保它们不为空,如果其中任何一个为空,则应弹出一条消息,说明哪个文本框为空,并阻止 datagridview 填充。

这是我到目前为止的代码:

 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

我没有收到任何错误,即使所有文本框都已填充,消息也会出现,消息框未将任何文本框指定为空并停止填充 datagridview。

我对编码很陌生,所以请更详细地解释您的解决方案。谢谢

4

2 回答 2

2
.Count >= -1

如果为空,则计数将为 0

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
于 2013-07-24T17:33:19.050 回答
0

您可以尝试在您的容器(即表单、面板、组框)处为每个没有内容的文本框循环,然后在有没有内容的文本框时递增。

例子:

          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
于 2013-11-20T09:28:05.100 回答