Kreij
Senior Monkey Moderator
- Joined
- Feb 6, 2007
- Messages
- 13,817 (2.05/day)
- Location
- Cheeseland (Wisconsin, USA)
I'm generating controls dynamically based on values retrieved from a database table.
The Names of the controls gets set by concatonating the field name and the Control Type.
There are multiple controls created using the field name, but their names are always unique.
(eg. MyFieldComboBox, MyFieldCheckBox, MyFieldInputTextBox, MyFieldOutputTextBox etc.)
This works fine.
Later in the program I need to parse through the controls to find the specific control I want so that I can retrieve its property values, based on the field name.
This too works fine and fills the Control array with all of the Controls that start with FieldName.
Now I want to find a specific type of control in the array by part of its name, knowing in advance what type the control is ...
Here's where things get funky. The ArrayFind() method finds the control, the MessageBox get displayed and it show the name of the control, but after I press the "OK" button, the program throw a NullReferenceException (Object not set to an instance of an object) on the MessageBox line.
The debug information show that the variable _ChB is null.
I thought maybe it was some kind of internal windows threading issue, but why would the message box see the name, display it, and then throw an NRE.
Thoughts? Comments?
Probably something simple that I am overlooking. It's been one of those days so far. lol
The Names of the controls gets set by concatonating the field name and the Control Type.
There are multiple controls created using the field name, but their names are always unique.
(eg. MyFieldComboBox, MyFieldCheckBox, MyFieldInputTextBox, MyFieldOutputTextBox etc.)
This works fine.
Later in the program I need to parse through the controls to find the specific control I want so that I can retrieve its property values, based on the field name.
Code:
Control[] _Controls = new Control[0];
foreach (Control c in this.Controls)
{
if (c.Name.StartsWith(FieldName)
{
Array.Resize<Control>(ref _Controls, _Controls.Length + 1);
_Controls[_Control.Length - 1] = c;
}
}
This too works fine and fills the Control array with all of the Controls that start with FieldName.
Now I want to find a specific type of control in the array by part of its name, knowing in advance what type the control is ...
Code:
CheckBox _ChB = (CheckBox)Array.Find<Control>(_Controls,
delegate(Control c) { return c.Name.EndWith("CheckBox"); });
MessageBox.Show(_ChB.Name);
Here's where things get funky. The ArrayFind() method finds the control, the MessageBox get displayed and it show the name of the control, but after I press the "OK" button, the program throw a NullReferenceException (Object not set to an instance of an object) on the MessageBox line.

The debug information show that the variable _ChB is null.
I thought maybe it was some kind of internal windows threading issue, but why would the message box see the name, display it, and then throw an NRE.
Thoughts? Comments?
Probably something simple that I am overlooking. It's been one of those days so far. lol
Last edited: