0

I am a beginner. I would like to ask how can I center Panel for all resolutions. I have panel which includes other panels and its called ParentPanel. I have tryed to add to designer this stuff

ParentPanel.Location = new Point(
this.ClientSize.Width / 2 - ParentPanel.Size.Width / 2,
this.ClientSize.Height / 2 - ParentPanel.Size.Height / 2);
ParentPanel.Anchor = AnchorStyles.None;

but it said : the type of namescape "point" could not be found, so I used help and it changed New point to New System.Drawing.Point but still it didnt work :/

My Form is fullscreen and its not sizebale so, WindowsState is Maximazed and StartPosition is CenterScreen.

Can you help me please?

Kuri
  • 41
  • 7
  • Welcome to StackOverflow! Please see ["Should questions include “tags” in their titles?"](http://meta.stackexchange.com/questions/19190/should-questions-include-tags-in-their-titles), where the consensus is "no, they should not". –  Dec 02 '14 at 15:26
  • See this answer: http://stackoverflow.com/questions/491399/c-centering-controls-within-a-form-in-net-winforms – Alex Zhukovskiy Dec 02 '14 at 15:33

2 Answers2

0

You should not place this code in designer because designer's code is called from constructor, and form is not visible at that moment yet.

Instead, handle SizeChanged event of your Form (or override OnSizeChanged method), and set position of ParentPanel there.

Andrey Korneyev
  • 26,353
  • 15
  • 70
  • 71
  • I forgot to say that I have form which starts Fullscreen and its not sizebale. WindowsState is Maximazed and StartPosition is CenterScreen. – Kuri Dec 02 '14 at 15:36
  • @Kuri, anyway - constructor is not a right place to calculate width of maximized form because form is not visible at that moment and form width is actually the value you set in designer, not the value it will be when maximized form will become visible. Handle `VisibleChanged` or override `protected override void OnSizeChanged(EventArgs e)` (as it will be called on form become visible too) and place your code there. – Andrey Korneyev Dec 02 '14 at 15:43
0

Your maths isn't quite right. Try this:

ParentPanel.Location = new Point(
    (this.ClientSize.Width - ParentPanel.Size.Width) / 2,
    (this.ClientSize.Height - ParentPanel.Size.Height) / 2);
ParentPanel.Anchor = AnchorStyles.None;
Cold Machine
  • 126
  • 7
  • Thank you but I will still get the error "namespace point could not be found" – Kuri Dec 02 '14 at 15:41
  • I thought we had already established that you shouldn't be putting this code in the designer file? Ideally handle the form's Loading event and put the code in there (you won't get the namespace error). Or, at a push, put it in the form's constructor AFTER the call to InitializeComponent(). – Cold Machine Dec 02 '14 at 15:51
  • But my Math is not bad, when I tryed your and my code, there is no different :) – Kuri Dec 02 '14 at 15:59