I want to change the font to the labels (Label1 to Label10) at the same time, explain:
I have a Form WindowsForms
in which I have a panel
within that panel I have added 2 user controls UserControls
, within both UserControls
I have several labels
, which do not belong to them UserControls
but are above the UserControl
ones added in them from the form.
This image to illustrate, The
Panel
is theGris Oscuro
background, the UserControl contains 1 icon and a title that belongs to the user control including the part of the light gray background color. The only objects that do not belong to user controls are the internal labels that can be observed.
But when executing the function it also changes the font to the titles of UserControls
which are also Labels but do not belong to the form as such, but come from the UserControl, I do it this way, with a recursive function :
private void cambiar_fuentes(Control contenedor)
{
foreach (Control control in contenedor.Controls)
{
if (control.Controls.Count > 0)
cambiar_fuentes(control);
else
{
if (control is Label) ((Label)control ).Font = new Font("Arial", 10, FontStyle.Regular);
}
}
}
Calling the function:
cambiar_fuentes(panel1);
How can I change only the font of the labels (1 to 10) that belong to the form itself?
EDITED
Designer.cs Code Snippet:
this.panel1.BackColor = System.Drawing.Color.Transparent;
this.panel1.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.panel1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.panel1.Controls.Add(this.flowLayoutPanel1);
this.panel1.Location = new System.Drawing.Point(1, 1);
this.panel1.MaximumSize = new System.Drawing.Size(295, 2);
this.panel1.MinimumSize = new System.Drawing.Size(2, 732);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(295, 732);
this.panel1.TabIndex = 5;
this.flowLayoutPanel1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)));
this.flowLayoutPanel1.AutoScroll = true;
this.flowLayoutPanel1.BackColor = System.Drawing.Color.Transparent;
this.flowLayoutPanel1.Controls.Add(this.userControl1);
this.flowLayoutPanel1.Controls.Add(this.userControl2);
this.flowLayoutPanel1.FlowDirection = System.Windows.Forms.FlowDirection.TopDown;
this.flowLayoutPanel1.Location = new System.Drawing.Point(1, 34);
this.flowLayoutPanel1.Name = "flowLayoutPanel1";
this.flowLayoutPanel1.Size = new System.Drawing.Size(292, 684);
this.flowLayoutPanel1.TabIndex = 29;
this.flowLayoutPanel1.WrapContents = false;
this.userControl1.Controls.Add(this.label1);
this.userControl1.Controls.Add(this.label2);
Here
this.uersControl1.Controls.Add(this.label1);
continue the other labels...
Environment: Visual Studio 2010 & .NET Netframework 4.
You could create a read-only property on your
UserControl
that returns the list of controls added to it. Not including those of the user control.According to the code of the control on which you have based it, it would be something like this
In this way, the property
PanelControls
only returns the controls added to the user control, not its own, and you could go through them and change the font or any other property you want.cambiar_fuentes
You could change the method to a generic method in which you indicate the type of control to which you want to apply the change, and pass the source :In this way to change all the fonts of the Labels included in the controls
CollapsiblePanel
you could do:If you prefer to keep the current format you can check in the method
cambiar_fuentes
if the containing control is a control of typeCollapsiblePanel
. If so you loop through the collectionPanelControls
and if not the collectionControls
:This way you could keep making the same call:
It occurs to me that you could discriminate between usercontrols when evaluating whether the control has inner controls like so:
Note that you have to substitute
usercontrol
with the class name of your user control.This way you prevent user controls from being inspected.
Hello, you can do the following Recover from a control, in this case your form those of type
Label
in the following wayThen you would call it like this in your form