0

I have make a usercontrol with one label and one textbox and then add into MDI Form Panel, by the code below

mainpanel.Controls.Clear();
ctrLogin login = new();
mainpanel.Controls.Add(login);

The mainpanel is fill dock in MDI Form, whre ctrLogin is a usercontrol contain on label and one textbox, how can I make the usercontrol central with fewer code As I don't think it need to need on resize event and calculate the screen size. Also I need to the width of label and textbox is fix size as well

Thank you

Emily
  • 103
  • 8
  • [Keep a Control vertically and horizontally at center of its container](https://stackoverflow.com/a/39047133/3110834): The most simple option is using a [`TableLayoutPanel`](https://learn.microsoft.com/en-us/dotnet/framework/winforms/controls/tablelayoutpanel-control-overview?WT.mc_id=DT-MVP-5003235) with 1 column and 1 row as parent of your `Panel`. Set `Dock` of the `TableLayoutPanel` to `Fill` and set `Anchor` of `Panel` to `None`. Host the other controls inside the panel. This way the panel and its content will always be in center vertically and horizontally. – Reza Aghaei Feb 22 '22 at 09:25

1 Answers1

0

something like this :

public Form1()
        {
            InitializeComponent();
            label1.Left = this.Size.Width / 2 - label1.Width / 2;
            label1.Top = this.Size.Height/ 2 - label1.Height / 2;
        }
Filippo
  • 56
  • 6