Reputation: 167
I wish to define a one time label while adding it to controls, whats the correct syntax of doing so?
for example something like this:
this.Controls.Add(new Label
{
.BorderStyle = label1.BorderStyle,
.BackColor = label1.BackColor,
.Text = "Breaks",
.Font = label1.Font,
});
Upvotes: 0
Views: 57
Reputation: 19618
As you are using Object Initializer for the label control, you don't require the "." to set the values of the properties.
Example : Cat cat = new Cat { Age = 10, Name = "Fluffy" };
from MSDN
Upvotes: 0
Reputation: 635
Object and Collection Initializers
this.Controls.Add(new Label
{
BorderStyle = label1.BorderStyle,
BackColor = label1.BackColor,
Text = "Breaks",
Font = label1.Font,
});
Make sure label1
is exists, so, don't call it before InitializeComponent()
Upvotes: 0
Reputation: 73502
Just remove the .
before the properties
this.Controls.Add(new Label
{
BorderStyle = label1.BorderStyle,
BackColor = label1.BackColor,
Text = "Breaks",
Font = label1.Font,
});
Object initializer in msdn.
Upvotes: 1