dougconran

Here's the problem. I have a panel with 50 input controls of one type or another, each with an associated text descriptor.

On validation I need to check each one of those inputs to ensure that they contain data, if one is empty I need to highlight the corresponding text. Each text control has the same name as the input control preceded by an x eg xfld1 --> fld1.

Is there any way I can loop through the input controls such that if it is empty I can highlight the corresponding text:

Conceptually the code would be something along the following lines:-

foreach (input)
{
if (input.Text == "")
{
xinput.Backcolor = color.red;
}
}

The only 2 ways I've found of achieving the requirement are either to re-declare every control as an array and iterating through the array (this is a bit complex with multiple input types) or individually checking every input control (VERY tedious).

TIA

Doug

By the way, I'm programming for the Compact Framework which is missing a chunk of functionality.


Re: Windows Forms General Programmatically Changing Control Properties

Brendan Grant

I take it that the device you are developing for does not have the ErrorProvider class (ie Windows CE 5)






Re: Windows Forms General Programmatically Changing Control Properties

Brendan Grant

A more automated way you can do this is with reflection to find each corresponding label from a centralized location:

Code Snippet

foreach (Control c in this.Controls)

{

if (c is TextBox)

{

//Get label

Label l = GetAssociatedLabel(c);

//If no text, set backcolor to red

if (c.Text == "")

{

l.BackColor = Color.Red;

}

else //If text, set backcolor to blue (to undo previous color (as an ugly example))

{

l.BackColor = Color.Blue;

}

}

}

Label GetAssociatedLabel(Control c)

{

//Get type of current form

Type t = this.GetType();

//Get FieldInfo instance about label based on defined name

FieldInfo field = t.GetField("x" + c.Name, BindingFlags.NonPublic | BindingFlags.Instance);

if (field != null)

{

//Return reference to label

return field.GetValue(this) as Label;

}

return null;

}






Re: Windows Forms General Programmatically Changing Control Properties

KJacquemin

I am assuming that by "input" you are talking about a TextBox control, and for the "text" you mean a Label control.

You can use the Tag property to store any object you need to associate with a control.

Then you can use code like this to do what you want:

Code Snippet

private void Form1_Load(object sender, EventArgs e)

{

// associate the text with the input control

textBox1.Tag = label1;

}

private void button1_Click(object sender, EventArgs e)

{

foreach (Control ctrl in this.Controls)

{

if (ctrl is TextBox)

{

Label lb = ctrl.Tag as Label;

lb.ForeColor = Color.Red;

}

}

}





Re: Windows Forms General Programmatically Changing Control Properties

dougconran

Many thanks to both Brendan Grant and KJacquemin for lightening fast replies.

1. You are right, there is no ErrorProvider

2. Both answers look pretty good to me with KJacquemin's being slightly simpler. I've gone for the first answer because I don't need to add the field name into the tag field.

Many thanks,

Doug