简体   繁体   English

定义所有标签的样式

[英]Define style for all labels

I'm working in ac# application in winform. 我正在Winform中的ac#应用程序中工作。

I saw that there is no Style for element (unlike WPF). 我看到元素没有样式(与WPF不同)。 But is there a way to simply set all the labels to a specific design ? 但是,有没有一种方法可以简单地将所有标签设置为特定设计?

Actually I do : 其实我是:

public partial class myControl : UserControl
{
    private Color LabelColor = Color.Indigo;
    private Color LabelFont = new System.Drawing.Font("Arial",
        18F,
        System.Drawing.FontStyle.Regular,
        System.Drawing.GraphicsUnit.Point,
        ((byte)(0)));

    public myControl()
    {
        InitializeComponent();

        //Set design
        designLabels();
    }

    private void designLabels()
    {
        List<Label> labelsToStyle = new List<Label>();
        labelsToStyle.Add(labelName);
        labelsToStyle.Add(labelAge);
        labelsToStyle.Add(labelSize);

        foreach (Label l in labelsToStyle)
        {
            l.ForeColor = LabelColor;
            l.Font = LabelFont;
            l.Dock = DockStyle.Fill;
        }
    }
}

It works but it doesn't display correctly in designer (I have to run application to see my design). 它可以工作,但不能在设计器中正确显示(我必须运行应用程序才能查看我的设计)。 And maybe it exists a simplest way ? 也许它存在最简单的方法?

As per my comment the easiest is to create a custom control and use that one in your windows. 根据我的评论,最简单的方法是创建一个自定义控件并在您的Windows中使用该控件。

here how simple it is. 这很简单。 Simply override the label and set in constructor the default value you want 只需覆盖标签并在构造函数中设置所需的默认值

public class DesignLabel : Label
    {
        public DesignLabel()
        {
            ForeColor = Color.Indigo;
            Font = new System.Drawing.Font("Arial", 18F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        }
    }

then compile once and go to your design view, open your toolbox and you will see the "DesignLabel", simply drag drop on the window and that's it. 然后编译一次并转到设计视图,打开工具箱,您将看到“ DesignLabel”,只需在窗口上拖放即可。 If you change default in the class it will be changed all over the place. 如果您在班级中更改了默认设置,它将在所有地方进行更改。

If you want to change the style already at design time so that you can see in in the Form.cs[design] you need to edit the Form.Designer.cs file! 如果要在设计时已经更改样式,以便可以在Form.cs [design]中看到,则需要编辑Form.Designer.cs文件! But this you have to do label for label by hand. 但是,您必须手动为标签做标签。 I saved in this example the Font and Color in the project properties (sorry for the german version): 在此示例中,我将FontColor保存在项目属性中(抱歉,德语版本):

在此处输入图片说明

in my example I have 3 Label s. 在我的示例中,我有3个Label In the Form.Designer.cs file you can add the properties: 在Form.Designer.cs文件中,可以添加属性:

// label1
// 
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(12, 9);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(41, 15);
this.label1.TabIndex = 0;
this.label1.Text = "label1";
this.label1.Font = Properties.Settings.Default.LabelFont;
this.label1.ForeColor = Properties.Settings.Default.LabelColor;

// 
// label2
// 
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(12, 68);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(41, 15);
this.label2.TabIndex = 1;
this.label2.Text = "label2";

this.label2.Font = Properties.Settings.Default.LabelFont;
this.label2.ForeColor = Properties.Settings.Default.LabelColor;
// 
// label3
// 
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(12, 122);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(41, 15);
this.label3.TabIndex = 2;
this.label3.Text = "label3";

this.label3.Font = Properties.Settings.Default.LabelFont;
this.label3.ForeColor = Properties.Settings.Default.LabelColor;

The result looks like this: 结果看起来像这样:

在此处输入图片说明

DISCLAIMER 免责声明

I would not recommend this approach! 我不推荐这种方法! Editing the Form.Desginer.cs file is never a good idea! 编辑Form.Desginer.cs文件绝不是一个好主意! I would stick to the run time changes. 我会坚持运行时间的变化。 If you want to change all Label s just filter the this.Controls for it and foreach through the collection like this: 如果要更改所有Label只需过滤this.Controls并遍历整个集合,如下所示:

this.Controls.OfType<Label>().ToList().ForEach(lbl =>
{
    lbl.Font = LabelFont;
    lbl.ForeColor = LabelColor;
    //lbl.Dock = DockStyle.Fill; // uncommented, because I could see only 1 Label
});

The result will be the same. 结果将是相同的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM