Reputation: 31
I have a list of about 100 sets of controls (Label, Checkbox, Textbox) on a form. They are all labeled the default names, (textbox1, textbox2, ... textbox101) They are normally hidden unless needed. When I run:
string msg = "";
foreach (Control child in Controls)
msg = msg + child.Name + ", ";
MessageBox.Show(msg);
only the first 90 appear to be in the Controls collection (as evidenced in the Messagebox output). It's as though I hit a limit on the form, but I know I have not.
They all were created in the .Designer.cs file and they all look the same. They were created the same way.
...
private System.Windows.Forms.CheckBox checkBox88;
private System.Windows.Forms.Label label88;
private System.Windows.Forms.TextBox textBox88;
private System.Windows.Forms.CheckBox checkBox89;
private System.Windows.Forms.Label label89;
private System.Windows.Forms.TextBox textBox89;
private System.Windows.Forms.CheckBox checkBox90;
private System.Windows.Forms.Label label90;
private System.Windows.Forms.TextBox textBox90;
private System.Windows.Forms.CheckBox checkBox91;
private System.Windows.Forms.Label label91;
private System.Windows.Forms.TextBox textBox91;
private System.Windows.Forms.CheckBox checkBox92;
private System.Windows.Forms.Label label92;
private System.Windows.Forms.TextBox textBox92;
private System.Windows.Forms.CheckBox checkBox93;
private System.Windows.Forms.Label label93;
private System.Windows.Forms.TextBox textBox93;
...
Is there some underlying location C# keeps information about controls on a form? Thank You!
Upvotes: 2
Views: 2762
Reputation: 116498
Check the designer file and make sure they are actually added to the form. That is, look for a this.Controls.Add(label91)
line.
Sometimes the designer file ends up with orphaned controls where perhaps you have deleted something in the past. At first glance in the code it looks like there ought to be a control there, but it's just an unused (leftover) variable.
Barring that, make sure the controls are not in a container (e.g. a Panel
or GroupBox
). They will be a child of a child and not show up directly in Form.Controls
.
Otherwise, all controls that are part of the form will be in Form.Controls
, as this is the underlying location (and only location) for child controls.
Upvotes: 1