How to scale controls on a form to fit the form proportionately?
I have a visual basic 2010 form with various group boxes, buttons, and labels. I want to be able to have the form maximized, but when I do that the controls stay where they are at and they do not resize with the form. I want them to resize proportionately with the form. Any help would 开发者_运维知识库be appreciated.
You can set the position and size of the controls form ResizeEnd or Resize event, based on form's size. You will need to make sure it doesn't crash when the form is minimized or made very small.
I actually ended up using the table layout panel to arrange everything to proportion with the screen size. It works quite nicely if your controls are laid out in a grid.
Let form1 is the form,
gb_check be a group box inside the form
the height, location and width
of the Groupbox
can be make relative to the form size as follows
Private Sub Form1_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
gb_chek.Height = (Me.Height * 30) / 100 'what ever be the form height the group box's height is always 30% of the form size
gb_chek.Width = (Me.Width * 40) / 100 'what ever be the form width the group box's width is always 40% of the form size
gb_chek.Location = New Point((Me.Width) / 18, (Me.Height) / 12)' set the location of the form relative to form size;
End Sub
If you have controls inside the group box its size set relative to the group box
It's the correct way ! In order to have a correct display for every screen resolution use the layouts provided in the .net framework ! You don't need to edit any margin or what so ever.
Dim res As New SizeF(Screen.PrimaryScreen.WorkingArea.Width / Me.Size.Width, Screen.PrimaryScreen.WorkingArea.Height / Me.Size.Height) Me.Scale(res)
精彩评论