Full size Windows Forms .NET 6.0 app has issues running on Windows 11

LaSaltena 1 Reputation point
2022-10-10T03:31:35.947+00:00

I have developed a full screen application for Windows using .NET 6.0. It works fine on Windows 10 but there are two problems when running on Windows 11. The first one is that the first time there is a come back to a previous form using

BackForm.Show()  
Hide()  

The application hides itself. After that, it does not happen again, until the application is closed and started again.

The other problem on Windows 11 is that there is a form that does not grows well to cover the full screen. It is the only form, and it is characterized for having a DateTimePicker, a ComboBox, and a ListView. All forms inherit from FullSizeForm, which includes the following code:

public partial class FullSizeForm : Form {  
    private Rectangle LastFormSize;  
    protected FullSizeForm() {  
        AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);  
        AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;  
        ClientSize = new System.Drawing.Size(800, 450);  
        LastFormSize = new Rectangle(Location.X, Location.Y, Width, Height);  
    }  
    protected void FullSizeForm_Load(object sender, EventArgs e) {  
        MaximizeBox = false;  
        MinimizeBox = false;  
        ControlBox = false;  
        VerticalScroll.Visible = false;  
        VerticalScroll.Enabled = false;  
        FormBorderStyle = FormBorderStyle.None;  
        WindowState = FormWindowState.Maximized;  
    }  
    protected void FullSizeForm_Resize(object sender, EventArgs e) {  
        float xRatio = (float)Width / LastFormSize.Width;  
        float yRatio = (float)Height / LastFormSize.Height;  
        float ratio = xRatio < yRatio ? xRatio : yRatio;  
        foreach (Control c in Controls) {  
            ResizeControl(c, ratio);  
        }  
        float newWidth = LastFormSize.Width * ratio;  
        float newHeight = LastFormSize.Height * ratio;  
        LastFormSize = new Rectangle(Location.X, Location.Y, (int)newWidth, (int)newHeight);  
    }  
    protected void ResizeControl(Control control, float ratio) {  
        int newX = (int)(control.Location.X * ratio);  
        int newY = (int)(control.Location.Y * ratio);  
        int newWidth = (int)(control.Width * ratio);  
        int newHeight = (int)(control.Height * ratio);  
        control.Location = new Point(newX, newY);  
        control.Size = new Size(newWidth, newHeight);  
        float newFSize = (control.Font.Size * ratio);  
        control.Font = new Font(control.Font.Name, newFSize, control.Font.Style);  
        if (control is ListView listView) {  
            foreach (ColumnHeader header in listView.Columns) {  
                int width = header.Width;  
                width = (int)(width * ratio);  
                header.Width = width;  
            }  
        }  
        foreach (Control c in control.Controls)  
            ResizeControl(c, ratio);  
    }  
}  

As I said before, the code works fine on Windows 10, but fails on Windows 11 (some buttons lose their texts, as I guess it gets too large) on that particular form.

Any help would be greatly appreciated.

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,873 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,648 questions
{count} votes

1 answer

Sort by: Most helpful
  1. LaSaltena 1 Reputation point
    2022-10-19T18:04:12.357+00:00

    Dear JackJun,

    Thank you for you response. I have kind of isolated the issue. I have a very basic desktop application developed with Windows Forms and C# (.NET 6.0). It was created using Visual Studio 2022 17.3.4 with that options. The application has three relevant files (others have been untouched): Forms.cs, Form1.cs and Form1.Designer.cs. Here it is the content of them.

    Forms.cs

    using System.Media;  
    
    namespace FullSizeApp {  
        public partial class FullSizeForm : Form {  
            private Rectangle LastFormSize;  
            protected Dictionary<object, System.Media.SoundPlayer> SoundMapping = new();  
    
            protected FullSizeForm() {  
                AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);  
                AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;  
                ClientSize = new System.Drawing.Size(800, 450);  
                LastFormSize = new Rectangle(Location.X, Location.Y, Width, Height);  
            }  
    
            protected void FullSizeForm_Load(object sender, EventArgs e) {  
                MaximizeBox = false;  
                MinimizeBox = false;  
                ControlBox = false;  
                VerticalScroll.Visible = false;  
                VerticalScroll.Enabled = false;  
                FormBorderStyle = FormBorderStyle.None;  
                WindowState = FormWindowState.Maximized;  
            }  
    
            protected void FullSizeForm_Resize(object sender, EventArgs e) {  
                float xRatio = (float)Width / LastFormSize.Width;  
                float yRatio = (float)Height / LastFormSize.Height;  
                float ratio = xRatio < yRatio ? xRatio : yRatio;  
                foreach (Control c in Controls) {  
                    ResizeControl(c, ratio);  
                }  
                float newWidth = LastFormSize.Width * ratio;  
                float newHeight = LastFormSize.Height * ratio;  
                LastFormSize = new Rectangle(Location.X, Location.Y, (int)newWidth, (int)newHeight);  
            }  
    
            protected void ResizeControl(Control control, float ratio) {  
                int newX = (int)(control.Location.X * ratio);  
                int newY = (int)(control.Location.Y * ratio);  
                int newWidth = (int)(control.Width * ratio);  
                int newHeight = (int)(control.Height * ratio);  
                control.Location = new Point(newX, newY);  
                control.Size = new Size(newWidth, newHeight);  
                float newFSize = (control.Font.Size * ratio);  
                control.Font = new Font(control.Font.Name, newFSize, control.Font.Style);  
                if (control is ListView listView) {  
                    foreach (ColumnHeader header in listView.Columns) {  
                        int width = header.Width;  
                        width = (int)(width * ratio);  
                        header.Width = width;  
                    }  
                }  
                foreach (Control c in control.Controls)  
                    ResizeControl(c, ratio);  
            }  
    
            public virtual void Clear() { }  
        }  
    }  
    

    Form1.cs

    namespace FullSizeApp {  
        public partial class Form1 : FullSizeForm {  
            public Form1() : base() {  
                InitializeComponent();  
            }  
    
            private void Form1_Load(object sender, EventArgs e) {  
                FullSizeForm_Load(sender, e);  
            }  
    
            private void Form1_Resize(object sender, EventArgs e) {  
                FullSizeForm_Resize(sender, e);  
            }  
    
            private void button3_Click(object sender, EventArgs e) {  
                Application.Exit();  
            }  
        }  
    }  
    

    Form1.Designer.cs

    namespace FullSizeApp {  
        partial class Form1 : FullSizeForm {  
            /// <summary>  
            ///  Required designer variable.  
            /// </summary>  
            private System.ComponentModel.IContainer components = null;  
    
            /// <summary>  
            ///  Clean up any resources being used.  
            /// </summary>  
            /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>  
            protected override void Dispose(bool disposing) {  
                if (disposing && (components != null)) {  
                    components.Dispose();  
                }  
                base.Dispose(disposing);  
            }  
    
            #region Windows Form Designer generated code  
    
            /// <summary>  
            ///  Required method for Designer support - do not modify  
            ///  the contents of this method with the code editor.  
            /// </summary>  
            private void InitializeComponent() {  
                this.dateTimePicker1 = new System.Windows.Forms.DateTimePicker();  
                this.groupBox1 = new System.Windows.Forms.GroupBox();  
                this.button1 = new System.Windows.Forms.Button();  
                this.button2 = new System.Windows.Forms.Button();  
                this.comboBox1 = new System.Windows.Forms.ComboBox();  
                this.listView1 = new System.Windows.Forms.ListView();  
                this.button3 = new System.Windows.Forms.Button();  
                this.groupBox1.SuspendLayout();  
                this.SuspendLayout();  
                //   
                // dateTimePicker1  
                //   
                this.dateTimePicker1.Location = new System.Drawing.Point(320, 45);  
                this.dateTimePicker1.Name = "dateTimePicker1";  
                this.dateTimePicker1.Size = new System.Drawing.Size(200, 23);  
                this.dateTimePicker1.TabIndex = 0;  
                //   
                // groupBox1  
                //   
                this.groupBox1.Controls.Add(this.button2);  
                this.groupBox1.Controls.Add(this.button1);  
                this.groupBox1.Location = new System.Drawing.Point(269, 83);  
                this.groupBox1.Name = "groupBox1";  
                this.groupBox1.Size = new System.Drawing.Size(292, 100);  
                this.groupBox1.TabIndex = 1;  
                this.groupBox1.TabStop = false;  
                this.groupBox1.Text = "Group";  
                //   
                // button1  
                //   
                this.button1.Location = new System.Drawing.Point(13, 32);  
                this.button1.Name = "button1";  
                this.button1.Size = new System.Drawing.Size(75, 23);  
                this.button1.TabIndex = 0;  
                this.button1.Text = "button1";  
                this.button1.UseVisualStyleBackColor = true;  
                //   
                // button2  
                //   
                this.button2.Location = new System.Drawing.Point(199, 32);  
                this.button2.Name = "button2";  
                this.button2.Size = new System.Drawing.Size(75, 23);  
                this.button2.TabIndex = 1;  
                this.button2.Text = "button2";  
                this.button2.UseVisualStyleBackColor = true;  
                //   
                // comboBox1  
                //   
                this.comboBox1.FormattingEnabled = true;  
                this.comboBox1.Location = new System.Drawing.Point(269, 189);  
                this.comboBox1.Name = "comboBox1";  
                this.comboBox1.Size = new System.Drawing.Size(292, 23);  
                this.comboBox1.TabIndex = 2;  
                //   
                // listView1  
                //   
                this.listView1.Location = new System.Drawing.Point(269, 218);  
                this.listView1.Name = "listView1";  
                this.listView1.Size = new System.Drawing.Size(292, 135);  
                this.listView1.TabIndex = 3;  
                this.listView1.UseCompatibleStateImageBehavior = false;  
                //   
                // button3  
                //   
                this.button3.Location = new System.Drawing.Point(384, 378);  
                this.button3.Name = "button3";  
                this.button3.Size = new System.Drawing.Size(75, 23);  
                this.button3.TabIndex = 4;  
                this.button3.Text = "Exit";  
                this.button3.UseVisualStyleBackColor = true;  
                this.button3.Click += new System.EventHandler(this.button3_Click);  
                //   
                // Form1  
                //   
                this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);  
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;  
                this.ClientSize = new System.Drawing.Size(800, 450);  
                this.Controls.Add(this.button3);  
                this.Controls.Add(this.listView1);  
                this.Controls.Add(this.comboBox1);  
                this.Controls.Add(this.groupBox1);  
                this.Controls.Add(this.dateTimePicker1);  
                this.Name = "Form1";  
                this.Text = "Form1";  
                this.Load += new System.EventHandler(this.Form1_Load);  
                this.Resize += new System.EventHandler(this.Form1_Resize);  
                this.groupBox1.ResumeLayout(false);  
                this.ResumeLayout(false);  
    
            }  
    
            #endregion  
    
            private DateTimePicker dateTimePicker1;  
            private GroupBox groupBox1;  
            private Button button2;  
            private Button button1;  
            private ComboBox comboBox1;  
            private ListView listView1;  
            private Button button3;  
        }  
    }  
    

    Now, this simple app shows itself fullscreen on Windows 10 without problems, all controls scaled proportionately. However, on Windows 11 the text of the buttons in the GroupBox get much larger, as if double or triple scalled.

    0 comments No comments