locked
Center Screen not working RRS feed

  • Question

  • hi,

     I have a form with 2 panels. when first panel showing form have some defined size and need to show in the middle of the screen. When i click one button the panel hide and the second panel showed(using visible property) and the form is expanded to the work area. When i click another button again i need to show first panel by hiding second panel and the form needs to resize to the defined size . At the same time the form will display in the center of the screen. All are ok except center screen.

    The form showed topleft only. Can anyone help me to work around for this / why this is happened? The following is my code

            private void Form1_Load(object sender, EventArgs e)
            {
                try
                {
                    this.WindowState = FormWindowState.Normal;
                    this.StartPosition = FormStartPosition.CenterScreen;
                    this.Height = 315;
                    this.Width = 328;
    
                    pnl2.Visible = false;
                    pnl1.Visible = true;
                    this.Text = _assemblyDetails.AssemblyTitle + " " + _assemblyDetails.AssemblyVersion;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK);
                }
            }
            private void Button1_Click(object sender, EventArgs e)
            {
                try
                {
                    this.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
    
                    // set position and size to the Form.  
                    this.Bounds = Screen.PrimaryScreen.WorkingArea;
    
                    pnl1.Visible = false;
                    pnl2.Visible = true;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK);
                }
            }
    
            private void Button2_Click(object sender, EventArgs e)
            {
    
                    this.StartPosition = FormStartPosition.CenterScreen;
                    this.Height = 315;
                    this.Width = 328;
    
                    pnl1.Visible = true;
                    pnl2.Visible = false;
    
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK);
                }
            }


    Wednesday, January 2, 2013 4:24 AM

Answers

  • "this.StartPosition = FormStartPosition.CenterScreen;"

    Since this statement is in a Button.Click event, the Form must already be shown.  You'll have to position the Form using your own code.

    • Marked as answer by Chester Hong Tuesday, January 15, 2013 5:22 AM
    Wednesday, January 2, 2013 6:46 AM