0

I'm having a problem that when i debug my application it doesn't match the size i've set in the designer. i've tried to set form minimum size to the desirable values.

The form is built up with a splitpanel, with a panel docked as fill on each side. The labels have default anchors. Textboxes have anchors left,top,right. Buttons have anchors left, top.

if i drag the bottom down during runtime i get the size/design i want, but why doesn't it start like that and how can i fix it?

Forms

Tangooe
  • 455
  • 1
  • 4
  • 15

1 Answers1

2

Looks like you're working on quite a high resolution screen. Windows Forms isn't very good with scaling the content and has all kinds of quirks that you need to be aware of. I would move to WPF if possible, but if you really need to continue using Windows Forms, here's what you should do.

  • Use AutoScaleMode.Dpi on your main form. It'll scale and relocate the controls to match your design when the DPI of the monitor is higher than the default 96 (100%). You could also try AutoScaleMode.Font but it might not work well if you use fonts other than the default (Tahoma 8,25 pt or something like that).
  • Use TableLayoutPanel or FlowLayoutPanel to make positioning controls easier. FlowLayoutPanel dynamically lays down your controls horizontally or vertically. If you're familiar with WPF or Windows Phone development, it's basically a StackPanel control.
  • Make sure your screen DPI is 96 (100%) and keep it the same throughout the development. You'll still have to make sure to test the application on other DPI's so that users with different settings will be able to use your application.

Here's more information about DPI scaling:

Edit: Visual Studio's Windows Forms designer shows the form using a different theme than your Windows version so that's one reason why your form looks different in runtime. In runtime the form uses the theme of the operating system currently running the application.

Community
  • 1
  • 1
Timo Salomäki
  • 7,099
  • 3
  • 25
  • 40
  • 1
    I found that using TableLayoutPanel or FlowLayoutPanel can mess up your form in design time when opening the project on a computer with another resolution than where is was first created. In runtime the form is OK again but in designtime it looks really ugly. I solved that by using lots of normal panels and the dock property (not anchors !). It requires more work and yes its old school but it worked – GuidoG Sep 07 '16 at 08:01
  • @GuidoG You're right, Windows Forms applications can really throw some surprises to your face and that's one of them. I guess the takeaway for OP is that there are many ways to try remedy the situation but with the wide array of supported Windows versions and different displays, it's really hard to find the perfect solution that works everywhere. Luckily we have WPF! – Timo Salomäki Sep 07 '16 at 08:05
  • Hankide Yes there is WPF but I am maintaining a project with over 50 forms and a requirement for MDI. In those cases WPF is not really much usefull. Unless it has finaly evolved I have not looked at it since VS 2013 ? – GuidoG Sep 07 '16 at 08:10
  • Okay, thanks for the input! i will try. Sadly i'd love to use WPF but since this is a school project and windows forms is mandatory to use – Tangooe Sep 07 '16 at 08:14
  • @GuidoG It would certainly be a lot of work to switch to WPF with that project. WPF still doesn't support MDI forms out of the box but there are some commercial and open source implementations available. I really like MVVM and the easiness of data manipulation (I have an app that heavily uses DataGrid with millions of rows), that's something WPF is really good at. – Timo Salomäki Sep 07 '16 at 08:18
  • Autoscale Font is the default, i tried to switch it to DPI but it's almost exactly the same, the only noticable difference is that there's a few pixles overlay beetween the textboxes and the labels – Tangooe Sep 07 '16 at 08:20
  • @EmilEkman What's your screen's DPI setting right now? The form contents are blurry which makes me think it's something else than the default. – Timo Salomäki Sep 07 '16 at 08:26
  • it's at default 250%, 100% is completely unreadable EDIT: at 100% the app displays correctly but way too small – Tangooe Sep 07 '16 at 08:37
  • at 200% it works too, it's usable, weird though that's this huge difference between 200% and 250% – Tangooe Sep 07 '16 at 08:44
  • @EmilEkman 250% is such a high DPI setting that I'm not surprised the design breaks down. Although, with all the correct settings the app should still be usable even with that DPI. There's still one useful answer that might help you find a way to make it work properly on the highest setting: http://stackoverflow.com/questions/22735174/how-to-write-winforms-code-that-auto-scales-to-system-font-and-dpi-settings – Timo Salomäki Sep 07 '16 at 08:46