0

The following problem: I need to center a button in my Form. How do I do this?

Current code:

static void Main(string[] args) {
    Form scherm;
    scherm = new Form();
    scherm.WindowState = FormWindowState.Maximized;
    button1 = new Button();
} 
Héctor
  • 24,444
  • 35
  • 132
  • 243
  • What have you tried? Because the code you show us does not do anything with location or add the button to the form. – EpicKip Nov 17 '17 at 12:18
  • Possible duplicate of [Centering controls within a form in .NET (Winforms)?](https://stackoverflow.com/questions/491399/centering-controls-within-a-form-in-net-winforms) – EpicKip Nov 17 '17 at 12:18
  • scherm.Controls.Add(button1); Added button And tried to center it just guessing the location but couldn't do it. Using scherm.Locaiton = new Point(x,y); Would love to get the width and height of my windowstate. But when I divided it by 2 it didn't center it... – Caspar Claessen Nov 17 '17 at 12:20
  • Look at the duplicated i proposed, the answer you need is in there (2nd answer) – EpicKip Nov 17 '17 at 12:21
  • Oh btw little tip, I see you're programming in Dutch and I suggest you change the variable names to English (as the method names of the .Net framework are English too) for consistency. Also all employers in the Netherlands want you to program in English trust me:) (I'm in the field) Its not good/bad now but getting used to doing it "right" can help you in the future – EpicKip Nov 17 '17 at 12:25
  • The "center" is going to depend on the size of the window. You don't know what it will be yet until you Show() the form. Consider using its Load event or derive your own class from Form, the normal way. – Hans Passant Nov 17 '17 at 14:20

0 Answers0