rodrigortz
rodrigortz

Reputation: 3

Check for empty text boxes when using lists

I have the following code:

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

I'm trying to check if the text boxes are empty before enabling them for edit; so basically if they're empty you can't edit, but if there's text you can. I tried the answer in: Check for empty TextBox controls in VB.NET but didn't work.

Any ideas?

Upvotes: 0

Views: 73

Answers (1)

FloatingKiwi
FloatingKiwi

Reputation: 4506

What you should do is work out whether or not to enable them and only then execute the foreach loop to enable / disable.

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

However I suspect you'll want to use ctlEditable instead of Me.Controls as Me.Controls only contains the top level controls of a form.

Upvotes: 1

Related Questions