Click here to Skip to main content
15,890,336 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to set the size of the form half the size of the screen.this is what i have wrote
<pre lang="cs">using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ScreenSize
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
          int ScreenWidth, ScreenHeight;
          ScreenWidth =System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width;
          ScreenHeight = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height;

          MessageBox.Show("ScreenWidth =" + ScreenWidth.ToString(), " ScreenHeight =" + ScreenHeight.ToString());

//none of the following works
        //Form1.ActiveForm.Size = new System.Drawing.Size(ScreenWidth /2, ScreenHeight /2);
        //Form1.Size = new System.Drawing.Size(ScreenWidth /2, ScreenHeight /2);
        //Form1.ActiveForm.Width=ScreenWidth/2;
        //Form1.ActiveForm.Height=ScreenHeight/2;

        }
    }
}

Posted
Updated 15-Jun-11 10:49am
v2

Yeah, but you didn't do this:
this.Size = new Size(ScreenWidth/2, ScreenHeight/2);
 
Share this answer
 
Comments
Khalid Sabtan 15-Jun-11 15:06pm    
To Dave Kreskowiak: the program run and i can see the affect of the form size but the size is not accurate, i tried half size in both width and height but the form appears to be 1/4 in width and 1/5 in height! any suggestion?
Dave Kreskowiak 15-Jun-11 18:25pm    
Did you look at the actual values coming back from PrimaryScreen.Bounds.Width and .Height? Do they match what the resolution settings of your primary monitor?? If not, you're making an assumption you shouldn't be making instead of verifying what you think you know.
You don't need Form1.ActiveForm - you want to affect the current instance.
Try:
private void frmTesting_Load(object sender, EventArgs e)
    {
    int ScreenWidth, ScreenHeight;
    ScreenWidth = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width;
    ScreenHeight = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height;
    Width = ScreenWidth / 2;
    Height = ScreenHeight / 2;
    }
 
Share this answer
 
this should fix your problem
C#
int ScreenWidth = Screen.GetWorkingArea(this).Width / 2;
            int ScreenHeight = Screen.GetWorkingArea(this).Height / 2;
this.Size = new Size(ScreenWidth, ScreenHeight);


if you want the form to be centered try to use this

C#
this.StartPosition = FormStartPosition.Manual;            
            int ScreenWidth = Screen.GetWorkingArea(this).Width / 4;
            int ScreenHeight = Screen.GetWorkingArea(this).Height / 4;
            this.Location = new Point(ScreenWidth, ScreenHeight);
            this.Size = new Size(ScreenWidth*2, ScreenHeight*2);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900