how to change label size dynamically using numericupdown in C#
I want to know how to change label size using current value in numeric up-down list u开发者_Go百科sing C#
Label sample;
public Form1()
{
InitializeComponent();
sample = new Label();
sample.Location = new Point(numericUpDown1.Left, numericUpDown1.Bottom);
sample.Size = numericUpDown1.Size;
sample.BackColor = Color.Blue;
Controls.Add(sample);
numericUpDown1.Value = sample.Width;
}
private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
sample.Width = (int)numericUpDown1.Value;
}
If you mean font size then here's another sample:
Label sample;
Font sampleFont;
public Form1()
{
InitializeComponent();
sample = new Label();
sample.Text = "Sample Text";
sample.AutoSize = true;
sample.Location = new Point(numericUpDown1.Left, numericUpDown1.Bottom);
sample.Size = numericUpDown1.Size;
sampleFont = (Font)Font.Clone();
sample.Font = sampleFont;
Controls.Add(sample);
numericUpDown1.Value = (decimal)sampleFont.Size;
}
private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
Font newFont = new Font(sampleFont.FontFamily, (float)numericUpDown1.Value);
sample.Font = newFont;
sampleFont.Dispose();
sampleFont = newFont;
}
By default, Labels have the AutoSize
property set to true
. If you explicitly change it to false
, you will be able to take control over the size.
精彩评论