473,769 Members | 1,632 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3792

<g6***@yahoo.co .uk> wrote in message
news:11******** *************@u 72g2000cwu.goog legroups.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(N ame);
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.A dd(PanelArray[i]);

}

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

PanelArray[i].ResumeLayout(f alse);
PanelArray[i].PerformLayout( );
}

May 30 '06 #3
Hi All

The answer was the ControlCollecti on 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:

UserControlArra y[i].InputFieldTitl e = "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 "CustomerNa me", "Title", "InputTypeN ame" 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
1611
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 guess it *has* then behind even if they are not meaningful. However, the code complete tool doesn't show them when I type panel1.(code complete list). Actually I can type panel1.Focus() without any compilation error.
1
1469
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 can't remember each control name on the form. thanks help me
3
1292
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 public properties, methods and functions up to the usercontrol interface?
4
1235
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 through Channel7, and the combobox controls are sequentially named Ch0 through Ch7, accordingly. Each combobox contains four items out of which one must be selected. For example, lets say we choose Channel3 and the corresponding combox
2
1554
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 several panels. Project B contains a form with usercontrol1 I would like to be able to click on a panel in the instance of
3
2815
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... Is there a way to do this, or do I need to copy each node into an interim handle of the correct class before manipulating it? ie,
2
2827
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 works quite welll in order to reference the control. Public Property TabControlAvailable() As TabControl
2
1566
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 anyone aware of a workaround to this problem?
7
6669
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 tabcontainer which has 1 panel already, however I want to try create the TabPanels dynamically. I followed the advice here: http://www.asp.net/learn/ajax-videos/video-156.aspx (3rd comment - Joe Stagner)
0
10211
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10045
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9994
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8870
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6673
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5298
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3958
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3561
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.