简体   繁体   中英

Windows Forms sizes not matching

I've got some very strict requirements on windows form sizes for elements and I've been having some trouble actually matching that up, I figured out the problem but I don't understand why I'm getting it. I have a picture box that needs to be a specific size, when I set it to that size in the properties panel of visual studio, it doesn't actually take that size, but a value smaller than that. It was throwing off my development. I can rectify it by manually setting the size value in the form code, however I'd like to know why the properties tab doesn't automatically do it correctly. I've just got it outputting the precise size value to a message box.

真实大小

属性窗口

I have noticed the exact same behaviour in my project today. The form and all objects on it hat a certain size, but when run, the size was all different.

After checking the InitializeComponents() I found this line

this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;

so I checked the according property, and I changed it to "None". Have never actually witnessed this behaviour before, but this did the trick for me. Now the Forms and everything else has the specified size.

The Size is actually size of whole window including the borders of windows form. You can see that the inner portion has exactly 18 pixels less for both width and height. You might calculate the desired width and height to assign to the form. Like if you want 100 x 100 pixel inner window size, you can assign 118 x 118 pixel for the size.

Any of the following properties of PictureBox could have caused it:

  • Anchor
  • Dock
  • Margin
  • MinimumSize
  • MaximumSize
  • SizeMode

Also need to check the direct parent (container) of your PictureBox . Is it directly put in the Form, or inside another container like GroupBox or Panel ? If latter is the case, you should check properties of the parent object too.

Size of you pictureBox is determined according to size of Form. That is why you picturebox does not fit size you defined.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM