Converting string to control I have an up-down numeric control on my form (attEdgeUpDown), which has value 0-8. I have eight checkboxes (attEdgeBox1 to attEdgeBox8). I want to display as many boxes as the numeric control shows.

Can this be done more efficiently?

private void attEdgeUpDown_ValueChanged(object sender, EventArgs e)
{
int edgeValue = Convert.ToInt32(attEdgeUpDown.Value);
    for (int i = 1; i &lt;= 8; i++)
    {
    string checkboxName = "attEdgeBox" + Convert.ToString(i);
    Control[] checkboxControl = this.Controls.Find(checkboxName, true);
        if (i &lt;= edgeValue)
            {
                (checkboxControl[0] as CheckBox).Visible = true;
            }
            else
            {
                (checkboxControl[0] as CheckBox).Visible = false;
                (checkboxControl[0] as CheckBox).Checked = false;
            } 
    }
}
Unless you're working with an ancient version of .NET, Convert.ToString(i); is superfluous. You should be able to just do "attEdgeBox" + i;.



If you're certain that those checkboxes exist, you can do var checkBox = this.Controls.Find(checkboxName, true).FirstOrDefault() as CheckBox; and then work with checkBox instead of doing (checkboxControl[0] as CheckBox) repeatedly.



Note that CheckBox is a compound name, so checkboxName as a variable name is incorrect; it should be checkBoxName.



What type is attEdgeUpDown, and why does it return a value that isn't an int? Seems odd to me that a control that returns a value from 1 to 8 would return this in a non-numeric format.