2

I want to change the width of the label as user resize the form.

This is the code that I tried:

    public partial class Form1 : Form
    {
        int lb;

        public Form1()
        {
            InitializeComponent();
            lb = this.Width - label1.Width;
        }

        private void Form1_Resize(object sender, EventArgs e)
        {
            label1.Width = lbl() ;
        }

        private int lbl()
        {
            return this.Width / 2 - lb;
        }
   }
Sayse
  • 42,633
  • 14
  • 77
  • 146
  • You could use the SizeChanged Event available by the Form class – Jens Jun 08 '15 at 08:17
  • You can just anchor the label to the form What does your current code do that is incorrect? – Sayse Jun 08 '15 at 08:18
  • possible duplicate of [Centering controls within a form in .NET (Winforms)?](http://stackoverflow.com/questions/491399/centering-controls-within-a-form-in-net-winforms) – Sayse Jun 08 '15 at 08:23
  • It is just a simple bug, it should be `return this.Width - lb;` Instead, just set the AutoSize property to False and turn on the right Anchor so this works without writing any code. – Hans Passant Jun 08 '15 at 09:40
  • 1
    its just changing the location not the size, i want to change the size of label as user resizing the form, to fit all the control properly – Abhijeet Ghatage Jun 08 '15 at 10:56

1 Answers1

1

Use the Anchor property to link your label with form borders.

UPDATE: You can also use TableLayoutPanel to dynamically lays out its contents: http://bit.ly/1KWypIX

Denis Voituron
  • 288
  • 1
  • 8