Changing properties of a control contained within an array of panel

G

g6023

Hi All

Newbie to C# I am afraid

The array of panel dispays OK but I am having problems with accessing
the controls within.

My panels each have a number of label controls. How do I set, say, the
Name.Text property in List<Panel> ListPanelArray

when I put var. into the following does it does not give me the Name
option for the label contol Name.

foreach (Panel var in ListPanelArray)
{
var.
}

Any ideas please

Regards

Geoff
 
P

pvdg42

Hi All

Newbie to C# I am afraid

The array of panel dispays OK but I am having problems with accessing
the controls within.

My panels each have a number of label controls. How do I set, say, the
Name.Text property in List<Panel> ListPanelArray

when I put var. into the following does it does not give me the Name
option for the label contol Name.

foreach (Panel var in ListPanelArray)
{
var.
}

Any ideas please

Regards

Geoff
First, foreach is a read only repetition structure.

From the C# Programmer's Reference:

The foreach statement repeats a group of embedded statements for each
element in an array or an object collection. The foreach statement is used
to iterate through the collection to get the desired information, but should
not be used to change the contents of the collection to avoid unpredictable
side effects.

Here are a couple of articles that may be of interest:

http://msdn.microsoft.com/library/d...us/csref/html/vclrfusingforeachwitharrays.asp

http://msdn.microsoft.com/library/d...ref/html/vclrfusingforeachwithcollections.asp
 
G

g6023

thanks very much for the feedback on the foreach.

Using array notation so I have:

PanelArray[0] with a label called Name - how do change the label
Name.Text property ?

Thanks very much

The following code displays Name 0 and Name 1

Panel[] PanelArray = new Panel[3];
for (int i = 0; i < 2; i++)
{
PanelArray = new Panel();

Label Name = new Label();

PanelArray.Controls.Add(Name);
PanelArray.Location = new System.Drawing.Point(14,
128 + 70 * (i + 1));
PanelArray.Size = new System.Drawing.Size(581, 50);
PanelArray.TabIndex = 66;

Name.AutoSize = true;
Name.Location = new System.Drawing.Point(15, 15);
Name.Name = "Name";
Name.Size = new System.Drawing.Size(80, 13);
Name.TabIndex = 0;
Name.Text = "Name " + i.ToString();

this.Controls.Add(PanelArray);

}

for (int i = 0; i < 2; i++)
{

PanelArray.ResumeLayout(false);
PanelArray.PerformLayout();
}
 
G

g6023

Hi All

The answer was the ControlCollection Class

So in the first Panel, the first control's properties were set as
follows:

PanelArray[0].Controls[0].Text = "New Text";
 
B

Bruce Wood

A much better way to do this is to use a UserControl instead of a
Panel, and then add public properties to the UserControl for your label
text:

public string InputFieldTitle
{
get { return this.FieldLabel.Text; }
set { this.FieldLabel.Text = value; }
}

Then place the user control multiple times on your form, and you can
set the label text like this:

UserControlArray.InputFieldTitle = "Title";

or whatever.

This offers several improvements over the code you posted, including:

1. If you add more things to your Panel, PanelArray.Controls[0] may
change to be something else, then your code breaks. If your UserControl
has a dedicated property for setting this text, then it will never
break.

2. If you decide to change where the text is displayed from a Label to
something else that needs the text set by some other property (rather
than Text), then you don't need to worry: you just fix the public
property on the UserControl and you don't need to change anything about
the forms on which the control is used. It is, in general, good
practice to hide the internal structure of complex controls that you
use over and over again so that a tweak to the control doesn't mean you
have to find everywhere it's used and make fixes in all of those
places.

3. You get to name the public property on your UserControl in terms of
what it's actually representing, rather than "label text" or something
equally lame. In almost all cases you're not wanting to set "label
text", but something like "CustomerName", "Title", "InputTypeName" or
some such thing. This way you get to call it what it is, not call it by
how it's implemented.
 
G

g6023

Thanks very much Bruce for taking the time to reply in such a detailed
manner and pointing me to User Controls

Regards

Geoff
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top