使用列表时检查空文本框

时间:2016-09-07 15:55:34

标签: vb.net list textbox

我有以下代码:

Private Sub EditarToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles EditarToolStripMenuItem.Click
        Dim ctlEditable = {txtCodigo, txtDeudor, txtOportunidad, drpBanca, txtEjecutivo, drpGarantia, txtCIIU, dteFecha, txtAnalista, drpEstatus, drpConcepto, dteUltAct, txtIngresos, drpCumplimiento, txtROA2, txtIE2, txtAnt, txtAct, txtCovenants, drpFaltas, txtOportunidades, txtCostos, txtPMAA}.ToList()
        Dim ctlText = {txtCodigo, txtDeudor, txtOportunidad, drpBanca, txtEjecutivo, drpGarantia, txtCIIU, dteFecha, txtAnalista, drpEstatus, drpConcepto, dteUltAct, txtIngresos, drpCumplimiento, txtROA2, txtIE2, txtAnt, txtAct, txtCovenants, drpFaltas, txtOportunidades, txtCostos, txtPMAA}.ToList()
        Dim ctlPerma = {txtRAS, txtActividad, txtTipo, txtMultaPot, txtPotencial}.ToList()
        Dim control As Control
        For Each control In Me.Controls
            If TypeOf (control) Is TextBox Then
                Dim txtBox As TextBox = control
                If txtBox.Text.Length = 0 Then
                    ctlEditable.ForEach(Sub(c) c.Enabled = False)
                Else
                    ctlEditable.ForEach(Sub(c) c.Enabled = True)
                    Return
                End If
            End If
        Next
    End Sub

我尝试在启用它们进行编辑之前检查文本框是否为空;所以基本上如果它们已经空了,你就无法编辑,但如果有文字,你可以。我在Check for empty TextBox controls in VB.NET尝试了答案,但没有成功。

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

你应该做的是弄清楚是否启用它们,然后执行foreach循环来启用/禁用。

    Dim enable = Me.Controls.OfType(Of TextBox).Any(Function(t) t.Text.Length > 0)
    ctlEditable.ForEach(Sub(c) c.Enabled = enable)

但我怀疑您要使用ctlEditable代替Me.Controls,而Me.Controls仅包含表单的顶级控件。