Change Source to several Labels at the same Time, C #

3

I want to change the font to labels (Label1 to Label10) at the same time, I 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 the UserControls but they are above the UserControl added in them from the form.

  

This image to illustrate, The Panel is the Gris Oscuro of 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 the user controls are the internal labels that can be observed.

But when executing the function it also changes the source to the titles of UserControls that are also Labels but do not belong to the form as such, but come from the UserControl, I do it this way, with a function recursive :

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 source only of the labels (1 to 10) that belong to the form as such?

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); the other labels continue ...

Environment: Visual Studio 2010 & .NET Netframework 4.

    
asked by J. Rodríguez 19.01.2018 в 21:07
source

3 answers

5

You could create a read-only property in 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 would be something like that

 public IEnumerable<Control> PanelControls
{
    get
    {
        var panelControls = new List<Control>();
        foreach (Control control in Controls)
        {
            if (control != titlePanel) panelControls.Add(control);
        }
        return panelControls;
    }
}

In this way the PanelControls property only returns the controls added to the user control, not your own, and you could go through them and change the source or any other property you want.

The cambiar_fuentes method could be changed by a generic method in which you indicate the type of control to which you want to apply the change, and pass the source:

private void CambiarFuentes<T>(IEnumerable<Control> controls, Font newFont) where T: Control
{
    var controlList = controls as Control[] ?? controls.ToArray();
    if (!controlList.Any()) return;
    foreach (var toChangeControl in controlList.OfType<T>())
    {
        toChangeControl.Font = newFont;
    }
    CambiarFuentes<T>(controlList.SelectMany(x => x.Controls.OfType<Control>()), newFont);
}

In this way to change all the sources of the Labels included in the controls CollapsiblePanel you could do:

foreach (var collapsiblePanel in Controls.OfType<CollapsiblePanel>())
{
    CambiarFuentes<Label>(collapsiblePanel.PanelControls, new Font("Arial", 10, FontStyle.Regular));
}

If you prefer to keep the current format you can check in the cambiar_fuentes method if the container control is a type control CollapsiblePanel . If so, you go through the collection PanelControls and if not the collection Controls :

private void cambiar_fuentes(Control contenedor)
{
    var panel = contenedor as CollapsiblePanel;
    var controlList = panel != null
        ? panel.PanelControls
        : contenedor.Controls.OfType<Control>();
    foreach (Control control in controlList)
    {
        if (control.Controls.Count > 0)
            cambiar_fuentes(control);
        else
        {
            if (control is Label) ((Label)control).Font = new Font("Arial", 10, FontStyle.Regular);
        }
    }
}

This way you could continue making the same call:

cambiar_fuentes(panel1);
    
answered by 20.01.2018 / 13:52
source
3

It occurs to me that you could discriminate the usercontrols when you evaluate whether the control has internal controls in the following way:

private void cambiar_fuentes(Control contenedor)
{
    foreach (Control control in contenedor.Controls)
    {
        if (control.Controls.Count > 0 && !(control is usercontrol))
           cambiar_fuentes(control);
        else
        {
           if (control is Label) ((Label)control ).Font = new Font("Arial", 10, FontStyle.Regular);
        }
   }
}

Keep in mind that you have to replace usercontrol with the name of the class in your user control.

This way you avoid having user controls inspected.

    
answered by 19.01.2018 в 21:35
3

Hello, you can do the following Recover from a control, in this case your form those of type Label in the following way

private void cambiar_fuentes(Control contenedor)
{
  foreach (var c in contenedor.Controls.OfType<Label>())
  {
    c.Font = new Font("Arial", 10, FontStyle.Regular);

  } 
}

Then you would call it that in your form

cambiar_fuentes(this);
    
answered by 19.01.2018 в 21:40