Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I want to make make C# winform width 100% of pc screen and height customize.

I have done several way but not able to do..

please help any one

What I have tried:

......................................................
Posted
Updated 28-Jul-20 4:08am

In the Shown event handler, set the Width and Height properties:
C#
Rectangle screen = Screen.FromControl(this).Bounds;
Width = screen.Width;
Height = 200;


[edit]
Hi Luc!
Generally, I use the Load event rather than the constructor - it'll work in the Shown, but lats less efficient as you said.
It's actually really easy: Windows treats multiple monitors as a single large desktop.
So this is my default project startup code:
C#
        /// <summary>
        /// Restore size and location (if the user doesn't
        /// override it)
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmMain_Load(object sender, EventArgs e)
            {
            if ((ModifierKeys & Keys.Shift) == 0)
                {
                this.LoadLocation();
                }

            }
        /// <summary>
        /// Save the size and location (if the used doesn't
        /// override it)
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmMain_FormClosing(object sender, FormClosingEventArgs e)
            {
            if ((ModifierKeys & Keys.Shift) == 0)
                {
                this.SaveLocation();
                }
            }
...
        /// <summary>
        /// Save the control location
        /// (Because a Form is derived from Control, it works
        /// for them as well)
        /// </summary>
        /// <param name="control">Form to save location of</param>
        /// <param name="instance">Instance identifier (for use with multiple instances)</param>
        public static void SaveLocation(this Control control, string instance = null)
            {
            string controlName = control.GetType().Name;
            if (!File.Exists(LocationsStorageFile))
                {
                CreateBlankLocationFile();
                }
            if (!(control is Form f) || (f.Visible && f.WindowState == FormWindowState.Normal))
                {
                // For Forms, we don't want to save if hidden, or if maximized / minimized
                // If we did, it causes the form to be misplaced, or far too small to see.
                DataTable dt = ReadXML(LocationsStorageFile);
                if (dt.Columns.Count >= 6)
                    {
                    bool ignoreInstance = string.IsNullOrWhiteSpace(instance);
                    DataRow current = dt.NewRow();    // Assume new row or instance.
                    current["ControlName"] = controlName;
                    current["Instance"] = instance ?? "";
                    foreach (DataRow row in dt.Rows)
                        {
                        if (row["ControlName"] as string == controlName && (ignoreInstance || row["Instance"] as string == instance))
                            {
                            // Found it!
                            //current = row;
                            dt.Rows.Remove(row);
                            break;
                            }
                        }
                    current["LocationX"] = control.Location.X;
                    current["LocationY"] = control.Location.Y;
                    current["SizeW"] = control.Size.Width;
                    current["SizeH"] = control.Size.Height;
                    dt.Rows.Add(current);
                    WriteXML(dt, LocationsStorageFile);
                    }
                }
            }
        /// <summary>
        /// Load the Control location
        /// (Because a Form is derived from Control, it works
        /// for them as well)
        /// </summary>
        /// <param name="control">Form to load location of</param>
        /// <param name="instance">Instance identifier (for use with multiple instances)</param>
        public static void LoadLocation(this Control control, string instance = null)
            {
            string controlName = control.GetType().Name;
            if (!File.Exists(LocationsStorageFile))
                {
                CreateBlankLocationFile();
                }
            DataTable dt = ReadXML(LocationsStorageFile);
            if (dt.Columns.Count >= 6)
                {
                bool ignoreInstance = string.IsNullOrWhiteSpace(instance);
                DataRow current = dt.NewRow();    // Assume new row or instance.
                current["ControlName"] = controlName;
                current["Instance"] = instance ?? "";
                current["LocationX"] = control.Location.X;
                current["LocationY"] = control.Location.Y;
                current["SizeW"] = control.Size.Width;
                current["SizeH"] = control.Size.Height;
                foreach (DataRow row in dt.Rows)
                    {
                    if (row["ControlName"] as string == controlName && (ignoreInstance || row["Instance"] as string == instance))
                        {
                        // Found it!
                        current = row;
                        if (int.TryParse(current["LocationX"].ToString(), out int x) &&
                            int.TryParse(current["LocationY"].ToString(), out int y) &&
                            int.TryParse(current["SizeW"].ToString(), out int w) &&
                            int.TryParse(current["SizeH"].ToString(), out int h))
                            {
                            control.Location = new Point(x, y);
                            control.Size = new Size(w, h);
                            }
                        break;
                        }
                    }
                }
            }

[/edit]
 
Share this answer
 
v3
Comments
Luc Pattyn 28-Jul-20 13:55pm    
Hi OG, I'm a bit surprised you do this in the Shown event handler, as this would result in a second layout (recalculating docks, anchors, etc) and a visible size change.

I tend to do such things in the Forms constructor, right after calling InitializeComponents; and typically based on SystemInformation.WorkingArea

This has served me well, BTW my systems are single screen, maybe you are handling it differently in order to support multiple screens?

:)
OriginalGriff 28-Jul-20 16:10pm    
Hi Luc! Long time no speak ... I run three screens these days: a 22" portrait, a 22" landscape, and a 19" square. The last one is old, but email and a few other bits fill it nicely - and it is colour matched to my printer, so it's my "overview" screen for Paint Shop Pro. Getting that match was a load of fun, so I don't want to repeat it on a new monitor in a hurry! :laugh:
Luc Pattyn 28-Jul-20 17:27pm    
Yeah, I avoid this QA section as much as I can, i.e. I read stuff here but I avoid getting in myself; that is, until something pops up that really interests me or puzzles me. And then I try to gain some insight in what is being discussed, which often is hard...

Multiple screens triggers a number of questions (last time I did that is maybe 20 years ago):
1. how do you get your app to automatically start on the screen of your choice? (if you have to move it around manually the shown handler code wouldn't be much help, would it?)
2. if, in the code you've shown, the Form ("this") initially isn't flush left, wouldn't setting its width to screen.width make it extend beyond its screen, flowing into the next screen if any? unless you also do Left=screen.Left; of course...
3. and if you somehow took precautions to get it flush left from the start, couldn't you apply the same trick to also get the width correct immediately, rather than fixing it once shown?

:)
OriginalGriff 29-Jul-20 5:28am    
See modified solution - I hate code in comment boxes! :laugh:
Luc Pattyn 29-Jul-20 7:35am    
Thanks, you improved your solution so much I had to five it

D)
An alternative: Screen.PrimaryScreen.WorkingArea gives you the bounds of the screen exclusive of taskbars, docked windows, docked toolbars. Use it to not interfere with the user's screen configuration.
public void SetScreenRect(Form frm, bool fullwide, bool fullheight, int x = 0, int y = 0, int width = 0, int height = 0)
{
    Rectangle scrn = Screen.PrimaryScreen.WorkingArea;

    width = fullwide ? scrn.Width : width;
    width -= x;

    height = fullheight ? scrn.Height : height;
    height -= y;

    frm.Bounds = new Rectangle(x, y, width, height);
}
Sample use: SetScreenRect(this, true, true, y: 100);

This would use the full screen, but offset the top of the Form by #100, and reduce the height the same amount.

This code is an example; I'd implement argument error checking in practice.
 
Share this answer
 

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



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