Dialog has undeclared extra space

I developed a Windows Forms dialog that should be reused in other applications, WPF and Windows Forms. This works fine when I use it in a Windows Forms application, but it causes some layout problems when called in a WPF application. The sizes and sizes are inconsistent when measured from pixels on the screen, from what WinForms API says, and from Spy ++. The window is 10 pixels wider and taller when run without a debugger than Spy ++ says it is, and I say it should be. What is the problem? I cannot find anything, but I will say that this is a very broken .NET Framework.

Here is the form class code:

using System; using System.Drawing; using System.Windows.Forms; namespace DialogTestApp { internal class MyDialog : Form { public MyDialog() { Text = "Title"; Width = 500; // -> actually 510 (Spy++ says 500) Height = 300; // -> actually 310 (Spy++ says 300) Font = SystemFonts.MessageBoxFont; FormBorderStyle = FormBorderStyle.FixedDialog; MaximizeBox = false; MinimizeBox = false; ShowInTaskbar = false; StartPosition = FormStartPosition.CenterScreen; TableLayoutPanel mainLayout = new TableLayoutPanel(); mainLayout.BackColor = Color.FromArgb(255, 171, 255); // pink mainLayout.Dock = DockStyle.Fill; mainLayout.Margin = Padding.Empty; mainLayout.Padding = Padding.Empty; mainLayout.RowStyles.Add(new RowStyle(SizeType.AutoSize)); // Only use minimum required space mainLayout.RowStyles.Add(new RowStyle(SizeType.Percent, 100)); Controls.Add(mainLayout); int row = 0; Label label = new Label(); label.Text = "Hello world. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque suscipit vestibulum gravida."; label.Font = new Font(Font.FontFamily, 12); label.MaximumSize = new Size(mainLayout.Width, 0); label.AutoSize = true; label.Dock = DockStyle.Fill; label.Margin = Padding.Empty; label.BackColor = Color.FromArgb(58, 171, 58); // green label.ForeColor = Color.White; mainLayout.Controls.Add(label, 0, row++); TextBox textBox = new TextBox(); textBox.Dock = DockStyle.Fill; textBox.Margin = Padding.Empty; textBox.Multiline = true; textBox.ScrollBars = ScrollBars.Both; mainLayout.Controls.Add(textBox, 0, row++); } } } 

Just put this file in an empty WPF application project and call it from the application constructor:

 public MainWindow() { InitializeComponent(); new MyDialog().ShowDialog(); Application.Current.Shutdown(); } 

Here's what it looks like with a debugger:

enter image description here

And without:

enter image description here

An extra pink frame is 10 pixels that shouldn't be there. The green label is configured to fill the entire space.

+6
source share
1 answer

Even without TableLayoutPanel , Label and using System.Windows.Forms.Application.Run(new MyDialog()) problem is still happening. The line causing the problem is FormBorderStyle = FormBorderStyle.FixedDialog;

There seems to be the same problem as described here: The form reports the wrong size in Windows 8 - how do I get the actual size?

Workaround:

  mainLayout.SizeChanged += delegate { label.MaximumSize = new Size(mainLayout.Width, 0); //MessageBox.Show("hi"); // called when not ran in debugger }; 
+2
source

All Articles