在控件内定义标签。外接程序C#

在控件内定义标签。外接程序C#,c#,label,controls,definition,C#,Label,Controls,Definition,我希望在将一次性标签添加到控件时定义一次性标签,这样做的正确语法是什么 例如,类似这样的事情: this.Controls.Add(new Label { .BorderStyle = label1.BorderStyle, .BackColor = label1.BackColor, .Text = "Breaks", .Font = label1.Font, }); 只需删除属性之前的 this.Controls.Add(new Label {

我希望在将一次性标签添加到控件时定义一次性标签,这样做的正确语法是什么

例如,类似这样的事情:

this.Controls.Add(new Label
{ 
    .BorderStyle = label1.BorderStyle,
    .BackColor = label1.BackColor,
    .Text = "Breaks",
    .Font = label1.Font,
});

只需删除属性之前的

this.Controls.Add(new Label
{ 
    BorderStyle = label1.BorderStyle,
    BackColor = label1.BackColor,
    Text = "Breaks",
    Font = label1.Font,
});
在msdn中。


确保
label1
存在,因此在
InitializeComponent()

之前不要调用它,因为您正在为label控件使用对象初始值设定项,所以不需要使用来设置属性值


示例:
Cat=newcat{Age=10,Name=“Fluffy”}来自

感谢您的快速回复!工作很有魅力。
this.Controls.Add(new Label
{ 
    BorderStyle = label1.BorderStyle,
    BackColor = label1.BackColor,
    Text = "Breaks",
    Font = label1.Font,
});