For each textbox loop

vb.net

Solution

You might try something like this instead:

  Dim ctrl As Control
  For Each ctrl In Panel1.Controls
  If (ctrl.GetType() Is GetType(TextBox)) Then
      Dim txt As TextBox = CType(ctrl, TextBox)
      txt.BackColor = Color.LightYellow
  End If

Problem

I'm trying to make a foreach loop that checks every TextBox in a panel and changes BackColor if its Text is nothing. I've tried the following: ``` Dim c As TextBox For Each c In Panel1.Controls if c.Text = "" Then c.BackColor = Color.LightYellow End If Next ``` but I'm getting the error: Unable to cast object of type System.Windows.Forms.Label to type System.windows.forms.textbox

Original source