468,253 Members | 1,286 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,253 developers. It's quick & easy.

Changing properties of a control contained within an array of panel

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

May 30 '06 #1
5 3498

<g6***@yahoo.co.uk> wrote in message
news:11*********************@u72g2000cwu.googlegro ups.com...
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/de...witharrays.asp

http://msdn.microsoft.com/library/de...ollections.asp


May 30 '06 #2
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[i] = new Panel();

Label Name = new Label();

PanelArray[i].Controls.Add(Name);
PanelArray[i].Location = new System.Drawing.Point(14,
128 + 70 * (i + 1));
PanelArray[i].Size = new System.Drawing.Size(581, 50);
PanelArray[i].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[i]);

}

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

PanelArray[i].ResumeLayout(false);
PanelArray[i].PerformLayout();
}

May 30 '06 #3
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";

May 30 '06 #4
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[i].InputFieldTitle = "Title";

or whatever.

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

1. If you add more things to your Panel, PanelArray[i].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.

May 30 '06 #5
Thanks very much Bruce for taking the time to reply in such a detailed
manner and pointing me to User Controls

Regards

Geoff

May 30 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Sam Sungshik Kong | last post: by
1 post views Thread by Rahim | last post: by
3 posts views Thread by Jim Hubbard | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.