Blocked Up
by Remy Porter
in CodeSOD
on 2026-03-03
Agatha has inherited some Windows Forms code. This particular batch of such code falls into that delightful category of code that's wrong in multiple ways, multiple times. The task here is to disable a few panels worth of controls, based on a condition. Or, since this is in Spanish, "bloquear controles". Let's see how they did it.
private void BloquearControles()
{
bool bolBloquear = SomeConditionTM;
foreach (Control C in this.pnlPrincipal.Controls)
{
if (C.GetType() == typeof(System.Windows.Forms.TextBox))
{
C.Enabled = bolBloquear;
}
if (C.GetType() == typeof(System.Windows.Forms.ComboBox))
{
C.Enabled = bolBloquear;
}
if (C.GetType() == typeof(System.Windows.Forms.CheckBox))
{
C.Enabled = bolBloquear;
}
if (C.GetType() == typeof(System.Windows.Forms.DateTimePicker))
{
C.Enabled = bolBloquear;
}
if (C.GetType() == typeof(System.Windows.Forms.NumericUpDown))
{
C.Enabled = bolBloquear;
}
}
foreach (Control C1 in this.grpProveedor.Controls)
{
if (C1.GetType() == typeof(System.Windows.Forms.TextBox))
{
C1.Enabled = bolBloquear;
}
if (C1.GetType() == typeof(System.Windows.Forms.ComboBox))
{
C1.Enabled = bolBloquear;
}
if (C1.GetType() == typeof(System.Windows.Forms.CheckBox))
{
C1.Enabled = bolBloquear;
}
if (C1.GetType() == typeof(System.Windows.Forms.DateTimePicker))
{
C1.Enabled = bolBloquear;
}
if (C1.GetType() == typeof(System.Windows.Forms.NumericUpDown))
{
C1.Enabled = bolBloquear;
}
}
foreach (Control C2 in this.grpDescuentoGeneral.Controls)
{
if (C2.GetType() == typeof(System.Windows.Forms.TextBox))
{
C2.Enabled = bolBloquear;
}
if (C2.GetType() == typeof(System.Windows.Forms.ComboBox))
{
C2.Enabled = bolBloquear;
}
if (C2.GetType() == typeof(System.Windows.Forms.CheckBox))
{
C2.Enabled = bolBloquear;
}
if (C2.GetType() == typeof(System.Windows.Forms.DateTimePicker))
{
C2.Enabled = bolBloquear;
}
if (C2.GetType() == typeof(System.Windows.Forms.NumericUpDown))
{
C2.Enabled = bolBloquear;
}
}
}