473,387 Members | 1,897 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 3768

<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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Sam Sungshik Kong | last post by:
Hello! While using panel control, I wondered a thing. Panel class is subclass of Control class. Control class has KeyPress event and Focus() method, etc... Then Panel class must have them. I...
1
by: Rahim | last post by:
i want to change all the label control style Properties, server control properties at runtime how should i call all the label at runtime, which is present at webform, any collections???? i...
3
by: Jim Hubbard | last post by:
If I create a usercontrol that is a tab with a webbrowser control on it, by default the new control exposes no properties of the tab or webbrowser control. What is the easiest way to bubble the...
4
by: ally | last post by:
In VB 2K5 Express I've created a panel named "Configure" with eight label, and eight combobox controls; and finally a button named "Apply." The label controls are sequentially named Channel0...
2
by: p_cranfield | last post by:
Hi I am trying to work out how to move controls contained within a user controls at design time using the mouse in the IDE. Consider the following: Project A contains usercontrol1 with...
3
by: Alex | last post by:
Hi all, I'm trying to create an arraylist of a user control class... I'm able to define the list and add objects (panels) to it, but I can access and of the panel properties using an index... ...
2
by: Rob | last post by:
I am not sure if this can be implemented.... There exists a TabContol (added at design time) to a form.. Tabs for this TabContol get added at run time. Usage of the following 2 properties...
2
by: gilak | last post by:
In VS2005, when changing the propery of any panels containing a TreeView or Menu I get the following error: Error parsing control: Object reference not set to an instance of an objec .. Does...
7
by: RichB | last post by:
I am trying to get to grips with the asp.net ajaxcontrol toolkit, and am trying to add a tabbed control to the page. I have no problems within the aspx file, and can dynamically manipulate a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.